The last few articles told you about the image face detection of OpenCV, and this article brings you the Dlib library for image face detection that is more accurate than OpenCV.
Click to view past issues:
"Picture face detection - OpenCV version (2)"
"Video face detection - OpenCV version (3)"
Comparison between dlib and OpenCV
Recognition accuracy: Dlib >= OpenCV
Dlib has more face recognition models that can detect 68 or more feature points on the face
Show results
68 feature points of human face
install dlib
Download address: https://pypi.org/simple/dlib/ Choose the version that suits you, and configure it yourself:
Window 10 + Python 3.6.4
My current version is: dlib-19.8.1-cp36-cp36m-win_amd64.whl
Install using the command:
pip3 install D:\soft\py\dlib-19.8.1-cp36-cp36m-win_amd64.whl
显示结果: Processing d:\soft\py\dlib-19.8.1-cp36-cp36m-win_amd64.whl Installing collected packages: dlib Successfully installed dlib-19.8.1
for the installation to succeed.
Download training model
The training model is the key to face recognition, which is used to find the key points of the picture.
Download address: http://dlib.net/files/
Download file: shape_predictor_68_face_landmarks.dat.bz2
Of course, you can also train your own face keypoint model, which will be discussed later.
The downloaded model file, my storage address is: C:\Python36\Lib\site-packages\dlib-data\shape_predictor_68_face_landmarks.dat.bz2
Unzip: shape_predictor_68_face_landmarks.dat.bz2 to get the file: shape_predictor_68_face_landmarks.dat
Code
#coding=utf-8
import cv2
import dlib
path = "img/meinv.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#face classifier
detector = dlib.get_frontal_face_detector()
# get face detector
predictor = dlib.shape_predictor(
"C:\\Python36\\Lib\\site-packages\\dlib-data\\shape_predictor_68_face_landmarks.dat"
)
dets = detector (gray, 1)
for face in dets:
shape = predictor(img, face) # Find the 68 calibration points of the face
# Traverse all points, print out their coordinates, and circle them
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Serial directory:
"OpenCV Environment Construction (1)"
"Picture face detection - OpenCV version (2)"
"Video face detection - OpenCV version (3)"
"Picture face detection - Dlib version (4)"
更多动态,请关注我的GitHub:https://github.com/vipstone/faceai