API summary and application of face detection face_recognition

face_recognition is the simplest face recognition library in the world, you can use it through Python reference or command line to manage and recognize faces. This software package uses the most advanced deep learning algorithm for face recognition in dlib, making the recognition accuracy rate reach 99.38% under the Labeled Faces in the world dataset.

Before installing face_recognition, you need to install the dlib library first. The dlib library is a python library for face key point detection, but because it is written in C++, it needs some dependencies and is more complicated. It is recommended to use conda installation to automatically download related dependencies.

face_recognition library installation

# 安装dlib
conda install -c https://conda.anaconda.org/conda-forge dlib
# -i https://pypi.tuna.tsinghua.edu.cn/simple 国内下载比较慢,清华镜像
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple

Introduction to common APIs

load_image_file(file, mode=‘RGB’)

Load an image file onto an object of type numpy array.
parameter:

  • file: the name of the image file to be loaded
  • mode: The format of the converted image. Only "RGB" (8-bit RGB, 3 channels) and "L" (black and white) are supported
  • return: an object of type numpy array containing the image data

face_landmarks(face_image,face_locations=None,model=“large”)

Given an image, extract the location of facial features for each face in the image

parameter:

  • face_image: input face image
  • face_locations=None: optional parameter, the default value is None, representing each face in the default decoded picture. If you enter face_locations()[i], you can specify the face for decoding
  • model="large": The output feature model, the default is "large", and "small" is optional. When "small" is selected, only the left eye, right eye, and nose tip are extracted.

face_encodings(face_image, known_face_locations=None, num_jitters=1)

Given an image, return the 128 face encodings (feature vectors) for each face in the image.
parameter:

  • face_image: input face image
  • known_face_locations: optional parameter, if you know the bounding box where each face is located
  • num_jitters=1: Number of times to resample when computing the encoding. Higher is more accurate, but slower (100 will be 100 times slower)
  • return: a 128-dimensional face encoding list

batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128)

Given an image, returns the position of each face in the image
Parameters:

  • img: an image (numpy array type)
  • number_of_times_to_upsample: How many times to find faces from the samples of images. The higher the value of this parameter, the smaller the face can be found.
  • model: Which face detection model to use. "hog" is less accurate, but runs faster on CPUs, "cnn" is more accurate and deeper (and GPU/CUDA accelerated, if GPU support is available), default is "hog"
  • return: a list of tuples, each tuple in the list contains the position of the face (top, right, bottom, left)

compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

Compare the list of face encodings and candidate encodings to see if they match.
parameter:

  • known_face_encodings: list of known face encodings
  • face_encoding_to_check: single face encoding data to be compared
  • tolerance: How much distance between two faces is considered a match. The smaller the value, the stricter the contrast, and 0.6 is a typical optimal value.
  • return: A list of True or False values ​​indicating the matching result for each member of the known_face_encodings list

face_distance(face_encodings, face_to_compare)

Given a set of facial encodings, compare them to known facial encodings to get the Euclidean distance. For each compared face, the Euclidean distance represents how similar the faces are.
parameter:

  • faces: list of face codes to compare
  • face_to_compare: single face encoding data to be compared
  • tolerance: How much distance between two faces is considered a match. The smaller the value, the stricter the comparison, 0.6 is a typical optimal value
  • return: a numpy ndarray, the Euclidean distance in the array corresponds to the order of the faces array one by one

face_recognition application code practice:

Feature points

from PIL import Image, ImageDraw
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("./data/zjl.jpg")
# Find all facial features in all the faces in the image
ace_landmarks_list = face_recognition.face_landmarks(image)

pil_image = Image.fromarray(image)

for face_landmarks in face_landmarks_list:
    draw = ImageDraw.Draw(pil_image, 'RGBA')
    for key in face_landmarks.keys():
        #左图
        draw.point(face_landmarks[feature_key],fill=(256, 256, 256))
        #右图
        #draw.line(face_landmarks[feature_key], width=5)
    pil_image.show()    

Code running effect:
insert image description here insert image description here

References:
https://github.com/ageitgey/face_recognition

Guess you like

Origin blog.csdn.net/Peyzhang/article/details/126321202