安装PyFaceDet库:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple PyFaceDet==0.2.0
安装opencv-python:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24from PyFaceDet import facedetectcnn import cv2 path = r'hezhao4.jpg' img = cv2.imread(path) faces = facedetectcnn.facedetect_cnn(path) print(faces) for face in faces: x = face[0] # 横坐标 y = face[1] # 纵坐标 l = face[2] # 长度 w = face[3] # 宽度 confidence = face[4] # 置信度 angle = face[5] # 角度 font = cv2.FONT_HERSHEY_SIMPLEX cv2.rectangle(img, (x, y), (x + l, y + w), (255, 0, 0), 2) roi_color = img[y:y + w, x:x + l] cv2.putText(img, str(confidence), (x + 5, y - 5), font, 1, (0, 0, 255), 1) # img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5) cv2.imshow('camera', img) cv2.waitKey(0) cv2.destroyAllWindows()
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51from PyFaceDet import facedetectcnn import cv2 video_capture = cv2.VideoCapture(r"D:Data7.MP4") # 读视频 # video_capture = cv2.VideoCapture(0) # 捕获摄像头 video_capture.set(cv2.CAP_PROP_POS_FRAMES, 3000) def img_resize(image): height, width = image.shape[0], image.shape[1] # 设置新的图片分辨率框架 width_new = 640 height_new = 480 # 判断图片的长宽比率 if width / height >= width_new / height_new: img_new = cv2.resize(image, (width_new, int(height * width_new / width))) else: img_new = cv2.resize(image, (int(width * height_new / height), height_new)) return img_new while True: # Grab a single frame of video ret, frame = video_capture.read() # frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5) frame = img_resize(frame) print(frame.shape) # Resize frame of video to 1/4 size for faster face recognition processing # small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) faces = facedetectcnn.facedetect_cnn(frame) for face in faces: x = face[0] # 横坐标 y = face[1] # 纵坐标 l = face[2] # 长度 w = face[3] # 宽度 confidence = face[4] # 置信度 angle = face[5] # 角度 font = cv2.FONT_HERSHEY_SIMPLEX cv2.rectangle(frame, (x, y), (x + l, y + w), (255, 0, 0), 2) roi_color = frame[y:y + w, x:x + l] cv2.putText(frame, str(confidence), (x + 5, y - 5), font, 1, (0, 0, 255), 1) cv2.imshow('camera', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
最后
以上就是无限招牌最近收集整理的关于python实现人脸检测的全部内容,更多相关python实现人脸检测内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复