代码语言:javascript复制
from mtcnn.mtcnn import MTCNN
import cv2
img=cv2.imread("C:/Users/xpp/Desktop/Lena.png")#读取图像
detector=MTCNN()
face_list=detector.detect_faces(img)#人脸检测与对齐
for face in face_list:
box=face["box"]
confidence=face["confidence"]
keypoints=face["keypoints"]
#边界框
x,y,w,h=box
cv2.rectangle(img,(x,y),(x w,y h),(0,255,0),2)
#置信度
cv2.putText(img,str(round(confidence,4)),(x,y-10),0,0.5,(255,0,0))
#关键点
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("C:/Users/xpp/Desktop/result.png",img)
算法:人脸检测是将人脸区域检测与人脸关键点检测放在了一起,它的主题框架类似于cascade。总体可分为P-Net、R-Net、和O-Net三层网络结构。
- P-Net:Proposal Net,实现人脸候选框提取
- R-Net:Refine Net,在P-Net输出结果的基础上进一步去除错误的候选框
- O-Net:Output Net,与R-Net类似,最终输出人脸bounding box以及landmark
文献:Zhang, K. , Zhang, Z. , Li, Z. , & Qiao, Y. . (2016). Joint face detection and alignment using multitask cascaded convolutional networks. IEEE Signal Processing Letters, 23(10), 1499-1503.
链接:https://pypi.org/project/mtcnn/