[OpenCV-Python]33.OpenCV face detection and recognition -- face recognition

Posted by saadlive on Wed, 05 Jan 2022 22:41:50 +0100

33.OpenCV face detection and recognition - face recognition

preface

  face recognition is to further judge a person's identity on the basis of face detection.
  OpenCV provides three face recognition methods: eigenfaces, Fisherfaces and Local Binary Patterns Histograms (LBPH).

1, Eigenfaces face recognition

   EigenFaces face recognition uses Principal Component Analysis (PCA) method to process face data from high dimension to low dimension, obtain the main component information of face data, and then complete face recognition.
  the basic steps of EigenFaces face recognition are as follows:
(1) Call CV2 face. EigenFaceRecognizer_ The create () method creates the EigenFace recognizer.
(2) Call the train() method of the recognizer to train the model with a known image.
(3) Call the predict() method of the recognizer to identify the unknown image and confirm its identity.
  cv2. face. EigenFaceRecognizer_ The basic format of the create() function is as follows:

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

recognizer Returned for EigenFaces Recognizer object
num_components Is the number of components at the time of analysis, The default is 0, Indicates that it is determined according to the actual input
threshold Is the threshold used in face recognition

   the basic format of the train() method of the EigenFaces recognizer is as follows:

recognizer.train(src, label)

src Is an array of known images for training, All images must be grayscale and of the same size
label Label array, It corresponds to the face in the known image array one by one, The face tag of the same person should be set to the same value

   the basic format of the predict() method of the EigenFaces recognizer is as follows:

label, confidence = recoginer.predict(testimg)

label Label value returned for
confidence Is the confidence level returned, Represents the distance between the unknown face and the known face in the model, 0 Indicates an exact match, Below 5000 can be considered as a reliable matching result
test_img Is an unknown face image, The image must be grayscale and the size must be the same as the training image
# Eigenfaces
import cv2
import numpy as np

img11 = cv2.imread("xl11.jpg", cv2.IMREAD_GRAYSCALE)
img12 = cv2.imread("xl12.jpg", cv2.IMREAD_GRAYSCALE)
img13 = cv2.imread("xl13.jpg", cv2.IMREAD_GRAYSCALE)
img21 = cv2.imread("xl21.jpg", cv2.IMREAD_GRAYSCALE)
img22 = cv2.imread("xl22.jpg", cv2.IMREAD_GRAYSCALE)
img23 = cv2.imread("xl23.jpg", cv2.IMREAD_GRAYSCALE)

train_images = [img11, img12, img13, 
                img21, img22, img23]

labels = np.array([0, 0, 0, 
                   1, 1, 1])

recoginer = cv2.face.EigenFaceRecognizer_create()
recoginer.train(train_images, labels)

test_img = cv2.imread('test1.jpg', cv2.IMREAD_GRAYSCALE)

label, confidence = recoginer.predict(test_img)

print("Matching label:",label)
print("Credibility:",confidence)

   training image in the program (gray image):

   unknown face images used for testing in the program:

  the program output results are as follows:

2, Fisherfaces face recognition

   Fisher faces face recognition uses Linear Discriminant Analysis (LDA) method to realize face recognition.
  the basic steps of Fisher faces face recognition are as follows:
(1) Call CV2 face. FisherFaceRecognizer_ The create () method creates the FisherFaces recognizer.
(2) Call the train() method of the recognizer to train the model with a known image.
(3) Call the predict() method of the recognizer to identify the unknown image and confirm its identity.
  in OpenCV, CV2 face. Eigenfacerecognizer class and CV2 face. Fisher face recognizer belongs to CV2 face. Basicfacerecognizer class, CV2 face. Facerecognizer class and CV2 Subclass of algorithm class, corresponding to XXX_ The basic format and usage of the create (), train(), and predict() methods are the same.

# Fisherfaces
import cv2
import numpy as np

img11 = cv2.imread("xl11.jpg", cv2.IMREAD_GRAYSCALE)
img12 = cv2.imread("xl12.jpg", cv2.IMREAD_GRAYSCALE)
img13 = cv2.imread("xl13.jpg", cv2.IMREAD_GRAYSCALE)
img21 = cv2.imread("xl21.jpg", cv2.IMREAD_GRAYSCALE)
img22 = cv2.imread("xl22.jpg", cv2.IMREAD_GRAYSCALE)
img23 = cv2.imread("xl23.jpg", cv2.IMREAD_GRAYSCALE)

train_images = [img11, img12, img13, 
                img21, img22, img23]

labels = np.array([0, 0, 0, 
                   1, 1, 1])

recoginer = cv2.face.FisherFaceRecognizer_create()
recoginer.train(train_images, labels)

test_img = cv2.imread('test1.jpg', cv2.IMREAD_GRAYSCALE)

label, confidence = recoginer.predict(test_img)

print("Matching label:",label)
print("Credibility:",confidence)

   training image in the program (gray image):

   unknown face images used for testing in the program:


  the program output results are as follows:

3, Local Binary Patterns Histograms (LBPH) face recognition

   the basic principle of image processing by LBPH algorithm is as follows:
(1) Take 8 pixels around pixel x (field) and compare them. If the pixel value is greater than pixel x, take 0, otherwise take 1. Connect the 0 and 1 corresponding to 8 pixels to obtain an 8-bit binary number, which is converted to decimal as the LBP value of pixel X.
(2) All pixels of the pixel are processed in the same way to obtain the LBP value of the whole image, and the histogram of the image is the LBPH of the image.
   the basic steps of LBPH face recognition are as follows;
(1) Call CV2 face. LBPHFaceRecognizer_ The create () method creates an LBPH identifier.
(2) Call the train() method of the recognizer to train the model with a known image.
(3) Call the predict() method of the recognizer to identify the unknown image and confirm its identity.
  cv2. face. LBPHFaceRecognizer_ The basic format of the create() function is as follows:

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

recognizer Returned for LBPH Recognizer object
radius Is the radius size of the neighborhood
neighbors Is the number of pixels in the neighborhood, The default is 8
grid_x For will LBP When the image is divided into multiple cells, Number of cells in the horizontal direction, The default is 8
grid_y For will LBP When the image is divided into multiple cells, Number of cells in the vertical direction, The default is 8
threshold Is the threshold used in face recognition

   the basic format of the train() method of LBPH recognizer is as follows:

recognizer.train(src, label)

src Is an array of known images for training, All images must be grayscale and of the same size
label Label array, It corresponds to the face in the known image array one by one, The face tag of the same person should be set to the same value

   the basic format of the predict() method of LBPH recognizer is as follows:

label, confidence = recoginer.predict(testimg)

label Label value returned for
confidence Is the confidence level returned, Represents the distance between the unknown face and the known face in the model, 0 Indicates an exact match, Below 50 can be considered as a very reliable matching result
test_img Is an unknown face image, The image must be grayscale and the size must be the same as the training image
# Local Binary Patterns Histograms (LBPH)
import cv2
import numpy as np

img11 = cv2.imread("xl11.jpg", cv2.IMREAD_GRAYSCALE)
img12 = cv2.imread("xl12.jpg", cv2.IMREAD_GRAYSCALE)
img13 = cv2.imread("xl13.jpg", cv2.IMREAD_GRAYSCALE)
img21 = cv2.imread("xl21.jpg", cv2.IMREAD_GRAYSCALE)
img22 = cv2.imread("xl22.jpg", cv2.IMREAD_GRAYSCALE)
img23 = cv2.imread("xl23.jpg", cv2.IMREAD_GRAYSCALE)

train_images = [img11, img12, img13, 
                img21, img22, img23]

labels = np.array([0, 0, 0, 
                   1, 1, 1])

recoginer = cv2.face.LBPHFaceRecognizer_create()
recoginer.train(train_images, labels)

test_img = cv2.imread('test2.jpg', cv2.IMREAD_GRAYSCALE)

label, confidence = recoginer.predict(test_img)

print("Matching label:",label)
print("Credibility:",confidence)

   training image in the program (gray image):

   unknown face images used for testing in the program:

  the program output results are as follows:

4, Opencv Python resource download

Opencv Python test pictures, Chinese official documents, opencv-4.5.4 source code

summary

   the above content introduces the face recognition of OpenCV Python. Articles on Python, data science and artificial intelligence will be published from time to time. Please pay more attention and connect three times with one button (● '◡' ●).

Topics: Python OpenCV Computer Vision opencv-python