opencv advanced 11-LBPH face recognition (face comparison)

The first step in face recognition is to find a model that can accurately reflect the characteristics of each face in a concise and differentiated way. When recognizing a face, first extract the features of the current face in the same way as above, and then find the nearest neighbor sample of the current feature from the existing feature set, so as to obtain the label of the current face.

OpenCV provides three face recognition methods, namely LBPH method, EigenFishfaces method, Fisherfaces method. This section mainly introduces the LBPH method briefly.

The model used by LBPH (Local Binary Patterns Histogram, local binary pattern histogram) is based on the LBP (Local
Binary Pattern, local binary pattern) algorithm. LBP
was first proposed as an effective texture description operator, and it has been widely used because of its outstanding effect in expressing local texture features of images.

Fundamental

The basic principle of the LBP algorithm is to compare the value of pixel A with the values ​​of its 8 nearest neighbors one by one:

  • If the pixel value of A is greater than the pixel value of its neighbors, you get 0.
  • Get 1 if the pixel value of A is less than the pixel value of its neighbors.

Finally, connect the 0 and 1 values ​​obtained by comparing pixel A with its surrounding 8 pixel points to get an 8-bit binary sequence, and convert the binary sequence into a decimal number as the LBP value of point A.

The following takes the center point of the left 3×3 area in Figure 23-6 as an example (the point with a pixel value of 76) to illustrate how to calculate the LBP value of this point. When calculating, use its pixel value of 76 as the threshold, and perform binarization processing on its 8 neighboring pixels,

  • Process the pixels whose value is greater than 76 as 1. For example, the points whose pixel values ​​are 128, 251, 99, and 213 in their neighborhood are all treated as 1 and filled in the corresponding pixel position.
  • Process pixels with pixel values ​​less than 76 as 0. For example, the points with pixel values ​​of 36, 9, 11, and 48 in their neighborhood are all treated as 0 and filled into the corresponding pixel positions.

According to the above calculation, the binary result shown in the right figure in Figure 23-6 can be obtained.

insert image description here
After the binarization is completed, specify a start position arbitrarily, and serialize the obtained binary results to form an 8-bit binary number.

For example, starting directly above the current pixel point, the binary sequence "01011001" is obtained in a clockwise order. Finally, convert the binary sequence "01011001" into the corresponding decimal number "89" as the pixel value of the current center point, as shown in Figure 23-7.

insert image description here
Process the image pixel by pixel in the above way to get the LBP feature image, and the histogram of this feature image is called LBPH, or LBP histogram.

In order to obtain texture structures at different scales, a circular neighborhood can also be used to expand the calculation to a neighborhood of any size. The circular neighborhood can be represented by (P, R), where P represents the number of pixels participating in the operation in the circular neighborhood, and R represents the radius of the neighborhood.

For example, different circular neighborhoods are used in Figure 23-8.

  • The (4, 1) neighborhood is used on the left, and the pixel value of the current pixel and the 4 pixels in the neighborhood are compared, and the radius used is 1.

  • The (8, 2) neighborhood is used on the right side, and the pixel value of the current pixel and the 8 pixel points in the neighborhood are compared, and the radius used is 2. Among the 8 neighborhood pixels participating in the comparison, some neighborhoods may not directly take the pixel at a certain position that actually exists, but construct a "virtual" pixel value through calculation to compare with the current pixel.
    insert image description here
    Due to the influence of light, the overall gray scale of the face often changes, but the relative gray scale between the various parts of the face will basically remain the same. The main idea of ​​LBP is to take the relative relationship between the current point and its neighboring pixels as the processing result. Because of this, when the overall gray level of the image changes (monotonous change), the features extracted from the LBP algorithm can remain unchanged.
    Therefore, LBP has been widely used in face recognition.

As can be seen from the above introduction, the LBP feature is very similar to the Haar feature, both of which are grayscale change features of the image.

Function introduction

In OpenCV, you can 函数 cv2.face.LBPHFaceRecognizer_create()generate the LBPH recognizer instance model, then apply it cv2.face_FaceRecognizer.train() 函数完成训练, and finally use cv2.face_FaceRecognizer.predict()the function to complete face recognition.
The above three functions are described below.

  1. Function cv2.face.LBPHFaceRecognizer_create()
    The syntax format of function cv2.face.LBPHFaceRecognizer_create() is:

retval = cv2.face.LBPHFaceRecognizer_create( [, radius[, neighbors[,
grid_x[, grid_y[, threshold]]]]])

All the parameters are optional and have the following meanings:

  • radius: Radius value, the default value is 1.
  • neighbors: The number of neighbor points, 8 neighbors are used by default, and more neighbor points can be calculated as needed.
  • grid_x: When the LBP feature image is divided into cells, the number of pixels in the horizontal direction of each cell.
    The default value of this parameter is 8, that is, the LBP feature images are grouped in units of 8 pixels in the row direction.
  • grid_y: When the LBP feature image is divided into cells, the number of pixels in the vertical direction of each cell.
    The default value of this parameter is 8, that is, the LBP feature images are grouped in units of 8 pixels in the column direction.
  • threshold: The threshold used when predicting. If it is greater than this threshold, it is considered that no target object has been identified.
  1. The function cv2.face_FaceRecognizer.train()
    function cv2.face_FaceRecognizer.train() calculates LBPH for each reference image and obtains a vector. Each face
    is a point in the entire vector set. The syntax of this function is:

None = cv2.face_FaceRecognizer.train( src, labels )

The meaning of each parameter in the formula is:

  • src: training image, the face image used for learning.
  • labels: label, the label corresponding to the face image.
    This function has no return value.
  1. Function cv2.face_FaceRecognizer.predict()
    The function cv2.face_FaceRecognizer.predict() judges a face image to be tested and finds the face image closest to the current image. Whichever face image is the closest, the current image to be tested is marked as its corresponding label. Of course, if the distance between the image to be tested and all face images is greater than the
    distance value specified by the parameter threshold in the function cv2.face.LBPHFaceRecognizer_create(), it is considered that no corresponding result is found, that is, the current face cannot be recognized.
    The syntax of the function cv2.face_FaceRecognizer.predict() is:

label, confidence = cv2.face_FaceRecognizer.predict( src )

The meanings of parameters and return values ​​in the formula are:

  • src: The face image to be recognized.
  • label: The label of the returned recognition result.
  • confidence: The returned confidence score. The confidence score is used to measure the distance between the recognition result and the original model.
    0 means an exact match . In general, values ​​less than 50 are considered acceptable, and values ​​greater than 80 are considered significant differences.

Example: complete a simple face recognition program

import cv2
import numpy as np
images=[]
images.append(cv2.imread("face\\face2.png",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("face\\face3.png",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("face\\face4.png",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("face\\face5.png",cv2.IMREAD_GRAYSCALE))
labels=[0,0,1,1]
#print(labels)
recognizer = cv2.face.LBPHFaceRecognizer.create()
recognizer.train(images, np.array(labels))
predict_image=cv2.imread("face\\face4.png",cv2.IMREAD_GRAYSCALE)

label,confidence= recognizer.predict(predict_image)
print("label=",label)
print("confidence=",confidence)

The pictures in it are the pictures of celebrities that I randomly downloaded from the Internet. The overall recognition degree is not high, and if it cannot be recognized, it will directly return 0. This problem will also occur in depth. Judgment logic needs to be adjusted.

return result:

label= 1
confidence= 0.0

Get familiar with it first, and then focus on comparing the differences and more suitable application scenarios of several face recognition algorithms.

Guess you like

Origin blog.csdn.net/hai411741962/article/details/132359991