Article directory
Steps
- Face Detection
- Face key point detection
- face alignment
Key class and interface method definitions
kind
- Face detection class:
dlib.fhog_object_detector
anddlib.cnn_face_detection_model_v1
, the former is based on the HOG model, the latter is based on the CNN model, the former detection method is called__call(img)__ ->dlib.rectangles
andrun(img,upsample_num,threshold)->(dlib.rectangles,List[scores:int],List[int])
, the latter detection method is called__call(img)__->dlib.mmod_rectangles
- Key point detection class:
dlib.shape_predictor
, prediction call method__call__(self,image, box: dlib.rectangle)->dlib.full_object_detection
- Detection result class:
dlib.full_object_detection
, common methodpart(self, idx: int)->dlib.point
single key point information,parts(self)->dlib.points
all key point information - Keypoint class:
dlib.point
keypoint, members include x, y,dlib.points
list of keypoints
method
- Get a single face alignment result
The padding parameter can adjust the size of the image return value
get_face_chip(img: numpy.ndarray[(rows,cols,3),uint8], face: _dlib_pybind11.full_object_detection, size: int=150, padding: float=0.25) -> numpy.ndarray[(rows,cols,3),uint8]
Takes an image and a full_object_detection that references a face in that image and returns the face as a Numpy array representing the image. The face will be rotated upright and scaled to 150x150 pixels or with the optional specified size and padding.
- Get multiple face alignment results
get_face_chips(img: numpy.ndarray[(rows,cols,3),uint8], faces: _dlib_pybind11.full_object_detections, size: int=150, padding: float=0.25)->list
Takes an image and a full_object_detections object that reference faces in that image and returns the faces as a list of Numpy arrays representing the image. The faces will be rotated upright and scaled to 150x150 pixels or with the optional specified size and padding.
sample code
import dlib
import numpy as np
from cv2 import cv2
# step 1. create face detect and shape predict model
face_detector_model_path = '../models/mmod_human_face_detector.dat'
face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path) # dlib.cnn_face_detection_model_v1
shape_model_path = r'../models/shape_predictor_68_face_landmarks.dat'
face_shape_predictor = dlib.shape_predictor(shape_model_path) # dlib.shape_predictor
# step 2. get all face detections
image_path = "sample.jpg"
img = dlib.load_rgb_image(image_path)
detections = face_detector(img, 1) # dlib.mmod_rectangles
# step 3. fetch one of all face detections, and if it's mmod_rectangle , convert to rectabgle
detection = detections[0] # dlib.mmod_rectangle
# the mmod_rectangle contains two parts : confidence and rect
# step 4. get one face shape and return a single object name full_object_detection
shape = face_shape_predictor(img, detection.rect) # dlib.full_object_detection
# step 5. get one aligned face
image = dlib.get_face_chip(img, shape, size=150, padding=0.45) # numpy.ndarray
# Optionally: get all the aligned faces that was detected in the first place
faces = [face.rect for face in detections]
# Get the aligned face images
# faces = dlib.full_object_detections()
# faces.append(shape)
# Optionally:
images = dlib.get_face_chips(img, faces, size=160, padding=0.25)