使用facenet_pytorch已经封装好了的库
环境:
pip install facenet_pytorch
代码:
from facenet_pytorch import MTCNN
import cv2
mtcnn = MTCNN()
img = cv2.imread('./pic_3.jpg')
#输出三元素,矩形框、概率、关键点
boxes, probs ,points= mtcnn.detect(img,landmarks=True)
#输出二元素,矩形框、概率
boxes2, probs2 = mtcnn.detect(img)
for index, bbox in enumerate(boxes):
if bbox is not None:
ymin, xmin, ymax ,xmax