Directorio de artículos
Pasos
- Detección de rostro
- Detección de puntos clave de la cara
- alineación de la cara
Definiciones de métodos de interfaz y clase clave
amable
- Clase de detección de rostros:
dlib.fhog_object_detector
ydlib.cnn_face_detection_model_v1
, el primero se basa en el modelo HOG, el segundo se basa en el modelo CNN, el método de detección anterior se denomina__call(img)__ ->dlib.rectangles
yrun(img,upsample_num,threshold)->(dlib.rectangles,List[scores:int],List[int])
, el método de detección posterior se denomina__call(img)__->dlib.mmod_rectangles
- Clase de detección de puntos clave:
dlib.shape_predictor
método de llamada de predicción__call__(self,image, box: dlib.rectangle)->dlib.full_object_detection
- Clase de resultado de detección: información de punto clave único
dlib.full_object_detection
de método común , toda la información de punto clavepart(self, idx: int)->dlib.point
parts(self)->dlib.points
- Clase de punto clave:
dlib.point
punto clave, los miembros incluyen x, y,dlib.points
lista de puntos clave
método
- Obtenga un resultado de alineación de una sola cara
El parámetro de relleno puede ajustar el tamaño del valor de retorno de la imagen
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.
- Obtenga múltiples resultados de alineación de rostros
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.
Código de muestra
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)