在这篇文章中,将介绍如何使用卷积神经网络进行人脸检测。
根据经验,基于深度学习的模型在面部检测方面取得了比opencv,cvlib或dlib更好的结果。
基于深度学习的人脸检测模型的一个例子是MTCNN。链接地址:https://github.com/ipazc/mtcnn 。
可以使用pip命令轻松安装。
pip install mtcnn

使用pip安装mtcnn
Python示例代码如下:
import cv2
from mtcnn.mtcnn import MTCNN
detector = MTCNN()
image = cv2.imread("street.jpg")
result = detector.detect_faces(image)
# Result is an array with all the bounding boxes detected.
bounding_box = result[0]['box']
keypoints = result[0]['keypoints']
cv2.rectangle(image,
(bounding_box[0], bounding_box[1]),
(bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
(0,155,255),
2)
cv2.circle(image,(keypoints['left_eye']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['right_eye']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['nose']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['mouth_left']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['mouth_right']), 2, (0,155,255), 2)
cv2.imwrite("detect.jpg", image)
使用mtcnn进行人脸检测的结果如下图所示:
