1234567891011121314151617181920212223242526 |
- #coding=utf-8
- from mtcnn.mtcnn import MTCNN
- import cv2
- # 初始化MTCNN人脸检测器
- face_detector = MTCNN()
- def detector(file):
- # 读取图像
- img = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
- # 进行人脸检测
- faces = face_detector.detect_faces(img)
- print(len(faces))
- # # 在图像中绘制人脸框和关键点
- for face in faces:
- x, y, w, h = face['box']
- cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
- for key, value in face['keypoints'].items():
- cv2.circle(img, value, 2, (0, 255, 0), -1)
- return faces
- # # 显示结果
- # cv2.imshow('Detected Faces', img)
- # cv2.waitKey(0)
- # cv2.destroyAllWindows()
|