OpenCV之使用MTCNN进行脸部特征检测

MTCNN

Multi-task Cascaded Convolutional Neural Networks for Face Detection, based on TensorFlow

install

pip install mtcnn==0.0.8

usage

>>> from mtcnn.mtcnn import MTCNN
>>> import cv2
>>>
>>> img = cv2.imread("ivan.jpg")
>>> detector = MTCNN()
>>> print(detector.detect_faces(img))
[{'box': [277, 90, 48, 63], 'keypoints': {'nose': (303, 131), 'mouth_right': (313, 141), 'right_eye': (314, 114), 'left_eye': (291, 117), 'mouth_left': (296, 143)}, 'confidence': 0.99851983785629272}]

检测器返回一个JSON对象列表。每个JSON对象包含三个键:"box"、"confidence"、"keypoints"

  • 检测框在键"box"下以 [x, y, width, height]格式存在
  • 键“confidence” 是检测框内容是一张脸的概率
  • 关键点包括左眼、右眼、鼻子、左嘴角、右嘴角。每个都是以像素点位置定义 (x, y)。

MTCNN的作者主页

实时检测

import numpy as np
import cv2

from mtcnn.mtcnn import MTCNN
import cv2

# img = cv2.imread("me.jpg")
detector = MTCNN()
cap = cv2.VideoCapture(0)
while True:
	ret,img = cap.read()
	faces = detector.detect_faces(img)
	for face in faces:
		x,y,w,h = face['box']
		cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
	cv2.imshow('img',img)
	if cv2.waitKey(1) &0xFF == ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

检测结果(脸太丑就遮起来了):