MTCNN使用实在是太广泛了,以至于直接集成到了python的库中,可以直接使用指令pip install mtcnn安装
注意:安装前需要确认以下依赖:
python >= 3.4
opencv >= 4.1
tensorflow >=1.12.0
官网(https://github.com/ipazc/mtcnn)给出的示例如下:
>>> from mtcnn import MTCNN
>>> import cv2
>>>
>>> img = cv2.cvtColor(cv2.imread("ivan.jpg"), cv2.COLOR_BGR2RGB)
>>> detector = MTCNN()
>>> 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
}
]
结果信息的进一步使用如下(以显示为例):
#detect and get first face
face = detector.detect_faces(img)
face = face[0]
#draw box
box = face["box"]
I = cv2.rectangle(img, (box[0],box[1]),(box[0]+box[2], box[1]+box[3]), (255, 0, 0), 2)
#draw points
left_eye = face["keypoints"]["left_eye"]
right_eye = face["keypoints"]["right_eye"]
nose = face["keypoints"]["nose"]
mouth_left = face["keypoints"]["mouth_left"]
mouth_right = face["keypoints"]["mouth_right"]
points_list = [(left_eye[0], left_eye[1]),
(right_eye[0], right_eye[1]),
(nose[0], nose[1]),
(mouth_left[0], mouth_left[1]),
(mouth_right[0], mouth_right[1])]
for point in points_list:
cv2.circle(I, point, 1, (255, 0, 0), 4)
#show
cv2.imshow('result', I)
cv2.waitKey(0)
安装好mtcnn后可以在以下路径查看到调用的权重:
~/anaconda3/envs/tf2.0/lib/python3.7/site-packages/mtcnn/data
同理其他库位置皆可以重复pip install指令从其打印信息中获取
这个权重是可以在实例化MTCNN时重载的,但其实没什么必要,如果需要重载必然自己有一套训练代码,有训练则有推理,那何必用这个MTCNN呢?
人脸识别任务中存在检测器和识别器相匹配的问题,所以使用的mtcnn版本要一致