forked from dalaAM/MTCNN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_demo.py
More file actions
31 lines (23 loc) · 835 Bytes
/
camera_demo.py
File metadata and controls
31 lines (23 loc) · 835 Bytes
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
import cv2
from mtcnn import FaceDetector
from PIL import Image
import numpy
detector = FaceDetector()
def camera_detect():
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
# 将 OpenCV 格式的图片转换为 PIL.Image
pil_im = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
# 绘制带人脸框的标注图
drawed_pil_im = detector.draw_bboxes(pil_im)
# 再转回 OpenCV 格式用于视频显示
frame = cv2.cvtColor(numpy.asarray(drawed_pil_im), cv2.COLOR_RGB2BGR)
cv2.imshow("Face Detection", frame)
# 输入 q 的时候结束循环(退出检测程序)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
video.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
camera_detect()