一、实现效果
能够基于不同的数据集实现68个关键点检测和81个关键点检测。
68个关键点检测效果如下:
81个关键点检测的效果如下:
二、安装环境配置
(1). Anaconda+pycharm
(2). 在pycharm中下载如下库:
(pycharm安装第三方库的方法参考:
https://blog.csdn.net/weixin_43978384/article/details/120204053
)
- python-opencv2
负责图像处理以及绘制图形等功能; - cmake
安装dlib库之前需要安装cmake;
3.dlib
负责识别人脸的关键点;
三、需要下载并使用训练的数据集
(1)、 68个关键点检测的数据集
链接:
https://pan.baidu.com/s/1ILxsaUAGRvzGnjnUsCfThA
提取码:abc7
(2)、 81个关键点检测的数据集
链接:
https://pan.baidu.com/s/1woyabe3B5KVoZzOohWkhjg
提取码:abc7
四、代码实现
import cv2
import dlib
import time
import numpy as np
filepath='D:/Users/13279/Desktop/dlib_faceDetection/' #the picture`s path
filepath1='D:/Users/13279/Desktop/dlib_faceDetection/68/' #the 68 points detectors path
filepath2='D:/Users/13279/Desktop/dlib_faceDetection/shape_predictor_81_face_landmarks-master/' #the 81 points detectors path
img=cv2.imread(filepath+"detection.JPG")
height,width,channels=img.shape
rescale=0.5
grayImg=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#get face detectors
faceDector=dlib.get_frontal_face_detector()
#get landmark detectors
#landmarkPredcitor=dlib.shape_predictor(filepath1+"shape_predictor_68_face_landmarks.dat")
landmarkPredcitor=dlib.shape_predictor(filepath2+"shape_predictor_81_face_landmarks.dat")
faces=faceDector(grayImg,1)
time_start=time.time()
for face in faces:
#get 68 landmarks for each face
landmarks=landmarkPredcitor(img,face)
print(len(landmarks.parts()))
#go through all the points and print them
for pt in landmarks.parts():
pt_pos=(pt.x,pt.y)
cv2.circle(img,pt_pos,2,(0,255,0),1)
time_end=time.time()
print("It takes {} to detect with dlib model!".format(time_end-time_start))
img=cv2.resize(img,(int(width*rescale),int(height*rescale)))
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
这篇文章永久免费,对所有用户开放,csdn不要再给我篡改用户权限了。