MTCNN_person.py 696 B

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