Browse Source

MTCNN 人脸检测

yufeng0528 7 months ago
parent
commit
84264b2520
3 changed files with 87 additions and 0 deletions
  1. 26 0
      draw/MTCNN_person.py
  2. 4 0
      draw/requirement.txt
  3. 57 0
      draw/web_index.py

+ 26 - 0
draw/MTCNN_person.py

@@ -0,0 +1,26 @@
1
+#coding=utf-8
2
+from mtcnn.mtcnn import MTCNN
3
+import cv2
4
+
5
+# 初始化MTCNN人脸检测器
6
+face_detector = MTCNN()
7
+
8
+def detector(file):
9
+    # 读取图像
10
+    img = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
11
+    # 进行人脸检测
12
+    faces = face_detector.detect_faces(img)
13
+
14
+    print(len(faces))
15
+    # # 在图像中绘制人脸框和关键点
16
+    for face in faces:
17
+        x, y, w, h = face['box']
18
+        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
19
+        for key, value in face['keypoints'].items():
20
+            cv2.circle(img, value, 2, (0, 255, 0), -1)
21
+    return faces
22
+
23
+# # 显示结果
24
+# cv2.imshow('Detected Faces', img)
25
+# cv2.waitKey(0)
26
+# cv2.destroyAllWindows()

+ 4 - 0
draw/requirement.txt

@@ -0,0 +1,4 @@
1
+opencv-python-headless
2
+tensorflow
3
+opencv-python
4
+mtcnn

+ 57 - 0
draw/web_index.py

@@ -0,0 +1,57 @@
1
+import time
2
+
3
+from flask import Flask, request
4
+import json
5
+import MTCNN_person
6
+
7
+app = Flask(__name__)
8
+
9
+@app.route('/')
10
+def hello_world():
11
+    return 'Hello World!'
12
+
13
+import requests
14
+#图片链接
15
+headers ={
16
+    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
17
+}
18
+dir = 'C:\\Users\\yufen\\Pictures\\face\\'
19
+def do_download(url):
20
+    url1 = url.split('?')[0]
21
+    name = url1.split('/')[-1]
22
+    r = requests.get(url, headers=headers)
23
+    # 下载图片
24
+    # 二进制数据需要用r.content 进行提取
25
+    # 将图片放在‘图库’文件夹下,‘图库’是文件夹的名称,将图片放入该文件夹中,该文件夹与py文件在同一目录下
26
+    f = open(dir + name, 'wb')
27
+    f.write(r.content)
28
+    f.close()
29
+    return name
30
+
31
+@app.route('/download')
32
+def download():
33
+    url = request.args.get('url')
34
+    time1 = int(round(time.time() * 1000))
35
+    print('info=%s' % url)
36
+    do_download(url)
37
+    time2 = int(round(time.time() * 1000))
38
+    print('over %s from %s' % (time2 - time1, time1))
39
+    return 'Hello World!'
40
+
41
+@app.route('/face')
42
+def face():
43
+    info = request.args.get('url')
44
+    time1 = int(round(time.time() * 1000))
45
+    print('info=%s' % info)
46
+    name = do_download(info)
47
+    time2 = int(round(time.time() * 1000))
48
+    faces = MTCNN_person.detector(dir + name)
49
+    time3 = int(round(time.time() * 1000))
50
+    print('over %s from %s' % (time3 - time1, time1))
51
+    return json.dumps(faces)
52
+
53
+
54
+if __name__ == '__main__':
55
+    app.run(port=5000)
56
+    # do_download(url='https://img.bbztx.com/article/live150178/team217182/20230913192631_tmp_9fbaaecf98fd1a3e91a6571f744f4b34511300d6c5a5b358.jpg?x-oss-process=image/resize,h_1000,m_lfit')
57
+    # MTCNN_person.detector('C:\\Users\\yufen\\Pictures\\face\\20230913192631_tmp_9fbaaecf98fd1a3e91a6571f744f4b34511300d6c5a5b358.jpg')