opencv advanced 12-EigenFaces face recognition

EigenFaces is also commonly referred to as eigenfaces. It uses Principal Component Analysis (PCA) to process high-dimensional face data into low-dimensional data (dimension reduction), and then perform data analysis and processing to obtain recognition results. .

Fundamental

In the real world, the representation of a lot of information is redundant. For example, there is redundant information in the parameters of the set of circles listed in Table 23-2.

insert image description here
Among the parameters shown in Table 23-2, there is a very strong correlation between each parameter:

  • diameter = 2*radius
  • Circumference = 2 π radius
  • area = pi radius radius

It can be seen that the diameter, circumference and area can all be calculated from the radius.

When performing data analysis, if we want to see the values ​​of these parameters more intuitively, we need to obtain the values ​​of all fields.

However, when comparing the size of circles, it is sufficient to use only the radius, at which point other information is "redundant" for us.

Therefore, we can understand that "radius" is the "principal component" in the data listed in Table 23-2. We extract "radius" from the above data for subsequent analysis, and realize "dimension reduction".

Of course, the data in the above example is very simple and easy to understand, but in most cases, the data we have to deal with is more complex. In many cases, we may not be able to directly judge which data are the key "principal components", so we must use the PCA method to analyze the "principal components" in complex data.

EigenFaces is to use the PCA method to reduce the dimensionality of the original data, and obtain the principal component information in it, so as to realize the method of face recognition.

Function introduction

OpenCV 函数 cv2.face.EigenFaceRecognizer_create()generates an eigenface recognizer instance model, then applies it cv2.face_FaceRecognizer.train()函数完成训练, and finally uses the cv2.face_FaceRecognizer.predict() function to complete face recognition.

  1. Function cv2.face.EigenFaceRecognizer_create()

The syntax of the function cv2.face.EigenFaceRecognizer_create() is:

retval = cv2.face.EigenFaceRecognizer_create( [, num_components[,
threshold]] )

The two parameters in the formula are optional parameters with the following meanings:

  • num_components: The number of components to keep in PCA. Of course, the value of this parameter is usually
    determined according to the input data, and there is no certain rule. Generally, 80 components are sufficient.
  • threshold: The threshold used for face recognition.
  1. The function cv2.face_FaceRecognizer.train()
    function cv2.face_FaceRecognizer.train() performs EigenFaces calculation on each reference image to obtain a vector.
    Each face is a point in the entire vector set. The syntax format 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: The labels corresponding to the face images.
    This function has no return value.
  1. Function cv2.face_FaceRecognizer.predict()
    The function cv2.face_FaceRecognizer.predict() will find the face image closest to the current image when judging a face image to be tested. Which face image is the closest, the image to be tested is recognized as its corresponding label. The syntax of this function is:

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

The meanings of each parameter and return value 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. The value of this parameter is usually between 0 and 20 000, as long as it is lower than 5000, it is considered to be a fairly reliable recognition result . Note that this range is different from the range of confidence score values ​​for LBPH.

Example: Use the EigenFaces module to 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.EigenFaceRecognizer_create()
recognizer.train(images, np.array(labels))
predict_image=cv2.imread("face\\face5.png",cv2.IMREAD_GRAYSCALE)
label,confidence= recognizer.predict(predict_image)
print("label=",label)
print("confidence=",confidence)

operation result:

An error was reported
insert image description here
, saying that all images must be the same size for training.

new code:


import cv2
import numpy as np
images=[]
img1= cv2.imread("face\\face2.png",cv2.IMREAD_GRAYSCALE);
img1.resize((240,240))
images.append(img1)

img2= cv2.imread("face\\face3.png",cv2.IMREAD_GRAYSCALE);
img2.resize((240,240))
images.append(img2)

img3= cv2.imread("face\\face4.png",cv2.IMREAD_GRAYSCALE);
img3.resize((240,240))
images.append(img3)

img4= cv2.imread("face\\face5.png",cv2.IMREAD_GRAYSCALE);
img4.resize((240,240))
images.append(img4)

labels=[0,0,1,1]
#print(labels)
recognizer = cv2.face.EigenFaceRecognizer_create()
recognizer.train(images, np.array(labels)) # 识别器训练
predict_image=cv2.imread("face\\face6.png",cv2.IMREAD_GRAYSCALE)
predict_image.resize((240,240))
label,confidence= recognizer.predict(predict_image)
print("label=",label)
print("confidence=",confidence)

operation result:

label= 1
confidence= 11499.110301703204

From the results, it is slightly more accurate than LBPH face recognition comparison.

おすすめ

転載: blog.csdn.net/hai411741962/article/details/132401375