1. anaconda 安装dlib库:
安装命令:https://anaconda.org/conda-forge/dlib
打开Anaconda Prompt:输入
conda install -c conda-forge dlib=19.9
等待安装完成即可;
2. 测试示例
方法1:基于Hog-SVM人脸检测器
方法2:基于深度卷积神经网络实现的人脸检测
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 17 16:51:46 2019
@author: zfjuan
"""
import cv2
import dlib
img = cv2.imread('.\\image\\keliamoniz1.jpg');
'''
#方法1:
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
# 使用 detector 检测器来检测图像中的人脸
# use detector of Dlib to detector faces
faces = detector(img, 1)
print("人脸数 / Faces in all: ", len(faces))
# Traversal every face
for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:",
"left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())
cv2.rectangle(img, tuple([d.left(), d.top()]), tuple([d.right(), d.bottom()]), (0, 255, 255), 2)
'''
#方法2
hog_face_detector = dlib.cnn_face_detection_model_v1('.\\data\\mmod_human_face_detector.dat')
face_rects = hog_face_detector(img, 0)
for feacRect in face_rects:
cv2.rectangle(img, tuple([feacRect.rect.left(), feacRect.rect.top()]), tuple([feacRect.rect.right(), feacRect.rect.bottom()]), (0, 255, 255), 2)
cv2.namedWindow("img", 1)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()