人脸检测python

Dlib Python 检测人脸特征点 Face Landmark Detection
首先安装Dlib,Opencv库

安装Dlib

1.到这里去下载你需要的的dlib轮子:Links for dlib

2.cmd进入你刚下好whl文件的位置,然后输入

pip install 文件名.whl
注意后缀是whl,并且选择安装的版本要和python版本一致,如果一次安装不成功选择不同的dlib安装试试,总有一款会成功。

下载特征检测器

设置特征检测器,dlib有已经训练的好的需要下载,也可以自己根据需要训练

下载链接:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

下载完之后解压,将路径送到dlib.shape_predictor()里面

import cv2
import dlib
detector = dlib.get_frontal_face_detector()
landmark_predictor = dlib.shape_predictor('D:/python/shape/shape_predictor_68_face_landmarks.dat')
img = cv2.imread('D:/python/demos/d.jpg')
faces = detector(img,1)
if (len(faces) > 0):
    for k,d in enumerate(faces):
           cv2.rectangle(img,(d.left(),d.top()),(d.right(),d.bottom()),(255,255,255))
        shape = landmark_predictor(img,d)
        for i in range(68):
            cv2.circle(img, (shape.part(i).x, shape.part(i).y),5,(0,255,0), -1, 8)
            cv2.putText(img,str(i),(shape.part(i).x,shape.part(i).y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,2555,255))
cv2.imwrite("D:/python/demos/fdres/hh.jpg", img)
//将实验的图像保存在fdres文件夹下            
cv2.imshow('Frame',img)
cv2.waitKey(0)

detector是dlib训练好的人脸检测器,是基于HOG特征的

shape = landmark_predictor(img,d)

我们想要的特征点全部保存在了shape里面,d是dlib.rectangle(),里面保存着人脸检测矩形的左上和右下坐标,shape.part(i)是第i个特征点landmark_predictor也是dlib训练好的人脸特征检测器,是基于Ensemble of Regression Trees的,在CVPR2014的论文有过,单人脸的特征点检测速度极快,Dlib就是实现了这种算法的,想要研究一下的可以看一下。
论文链接https://pdfs.semanticscholar.org/d78b/6a5b0dcaa81b1faea5fb0000045a62513567.pdf

猜你喜欢

转载自blog.csdn.net/u013309870/article/details/80016000