论文Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks提出了一种多任务-级联的卷积神经网络框架,用以实现人脸检测和对齐。构成人脸检测和对齐的级联网络的三个部分分别为:P-Net,R-Net和O-Net.
(1)P-Net:Proposal Net,用于实现人脸候选框提取;
(2)R-Net:Refine Net,在P-Net输出结果的基础上进一步去除错误的候选框;
(3)O-Net:Output Net,与R-Net类似,最终输出人脸bounding box以及landmark。
本篇介绍通过一个Python的mtcnn包快速实现一个人脸检测和对齐程序。
环境:
Ubuntu16.04 / python3.6 / tensorflow 1.10.1 / opencv 3.4
通过pip安装mtcnn:
$pip install mtcnn
安装完成后可以使用mtcnn的detect_faces方法实现图片中的人脸检测与对齐,代码实现:
from mtcnn.mtcnn import MTCNN
import cv2
img = cv2.imread("test.jpg")
detector = MTCNN()
face_list = detector.detect_faces(img) # face detect and alignment
for face in face_list:
box = face["box"]
confidence = face["confidence"]
keypoints = face["keypoints"]
# draw boundingbox
x,y,w,h = box
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# put the confidence
cv2.putText(img,str(round(confidence,4)),(x,y-10),0,0.5,(255,0,0))
# draw keypoints
cv2.circle(img,keypoints["left_eye"],1,(0,0,255),2)
cv2.circle(img,keypoints["right_eye"],1,(0,0,255),2)
cv2.circle(img,keypoints["nose"],1,(0,0,255),2)
cv2.circle(img,keypoints["mouth_left"],1,(0,0,255),2)
cv2.circle(img,keypoints["mouth_right"],1,(0,0,255),2)
cv2.imwrite("result.jpg",img)