face detection python

Dlib Python detects face landmarks Face Landmark Detection
First install Dlib, Opencv library

Install Dlib

1. Go here to download the dlib wheels you need: Links for dlib

2.cmd enter the location where you just downloaded the whl file, and then enter

pip install file name.whl
Note that the suffix is ​​whl, and the version you choose to install should be the same as the python version. If an installation is unsuccessful, choose a different dlib installation to try, and there will always be one that will succeed.

Download Feature Detector

Set the feature detector, dlib has already been trained and needs to be downloaded, or you can train it yourself as needed

Download link: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

After downloading, unzip it and send the path to 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 is a face detector trained by dlib, based on HOG features

shape = landmark_predictor(img,d)

All the feature points we want are stored in shape, d is dlib.rectangle(), which stores the upper left and lower right coordinates of the face detection rectangle, shape.part(i) is the ith feature point landmark_predictor is also dlib The trained face feature detector is based on Ensemble of Regression Trees, which was published in the CVPR2014 paper. The feature point detection speed of a single face is extremely fast. Dlib implements this algorithm. If you want to study it, you can take a look.
Paper link https://pdfs.semanticscholar.org/d78b/6a5b0dcaa81b1faea5fb0000045a62513567.pdf

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324656813&siteId=291194637