首先在conda创建python3.6的环境,然后使用清华源下载dlib与相关依赖库
pip install dlib==19.7.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pe_predictor_68_face_landmarks.dat
是已经训练好的人脸关键点检测器。dlib_face_recognition_resnet_model_v1.dat
是训练好的ResNet人脸识别模型。
detector是dlib训练好的人脸检测器,是基于HOG特征的,也可以是基于resnet深度学习的。
如:shape = landmark_predictor(img,d)
我们想要的特征点全部保存在了shape里面,d是dlib.rectangle(),里面保存着人脸检测矩形的左上和右下坐标,shape.part(i)是第i个特征点
检测的代码:
# _*_ coding:utf-8 _*_
import numpy as np
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# cv2读取图像
img = cv2.imread("1.jpg")
# 取灰度
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# 人脸数rects
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
for idx, point in enumerate(landmarks):
# 68点的坐标
pos = (point[0, 0], point[0, 1])
print(idx,pos)
# 利用cv2.circle给每个特征点画一个圈,共68个
cv2.circle(img, pos, 5, color=(0, 255, 0))
# 利用cv2.putText输出1-68
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, str(idx+1), pos, font, 0.8, (0, 0, 255), 1,cv2.LINE_AA)
cv2.namedWindow("img", 2)
cv2.imshow("img", img)
效果图如下
68个特征点:
关于 shape_predictor_68_face_landmarks.dat.bz2检测文件,官网下载贼慢,这里本人上传到csdn,链接如下:https://download.csdn.net/download/qq_35054151/11483794
不错的链接:https://blog.csdn.net/weixin_39875161/article/details/88073849
眨眼检测:https://www.cnblogs.com/gmhappy/p/9472368.html
参考链接:https://blog.csdn.net/AI_girl/article/details/86165960
https://blog.csdn.net/qq_39438636/article/details/79304130
https://www.cnblogs.com/as3asddd/p/7257820.html
c++:https://blog.csdn.net/qq_34106574/article/details/84022449