dlib realizes face alignment method

Steps

  1. Face Detection
  2. Face key point detection
  3. face alignment

Key class and interface method definitions

kind

  1. Face detection class: dlib.fhog_object_detectorand dlib.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.rectanglesand run(img,upsample_num,threshold)->(dlib.rectangles,List[scores:int],List[int]), the latter detection method is called__call(img)__->dlib.mmod_rectangles
  2. Key point detection class: dlib.shape_predictor, prediction call method__call__(self,image, box: dlib.rectangle)->dlib.full_object_detection
  3. Detection result class: dlib.full_object_detection, common method part(self, idx: int)->dlib.pointsingle key point information, parts(self)->dlib.pointsall key point information
  4. Keypoint class: dlib.pointkeypoint, members include x, y, dlib.pointslist of keypoints

method

  1. 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.
  1. 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)

Guess you like

Origin blog.csdn.net/LJX_ahut/article/details/124969978