Simple face recognition system based on OpenCV


Disclaimer: This program is based on Python's OpenCV module programming, using opencv's existing face detector and face recognizer for real-time face recognition

1. Call library functions

import cv2
import numpy as np

2. Call the camera and set the window

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)      #设置参数,10为亮度
cap.set(4, frameHeight)
cap.set(10,150)

3. Set the path of the picture positive and negative sample data set

pos_path = './resource/face_detect/video2imagem3/'
neg_path = './resource/face_detect/video2imagec1/'
test_path = './resource/face/'
images = []
labels = []

4. Invoke the face detector

Note: The face recognizer is installed when the opencv library is installed, and it can be found under the opencv installation path.

faceCascade = cv2.CascadeClassifier("resource/haarcascade_frontalface_default.xml")

5. Loading of positive and negative samples

Since the face data of this dataset is obtained by processing the video by the program, it is convenient to import into the program in batches.

The ratio of the positive sample (face) to the negative sample (environment) is roughly 1:3. It is said that this ratio works well; the label is 0,1.
The import statement is as follows:

for i in range(60):         # 正样本
    images.append(cv2.imread(pos_path+str(2*(i+1))+'.jpg',cv2.IMREAD_GRAYSCALE))
    labels.append(0)

for i in range(173):        # 负样本
    images.append(cv2.imread(neg_path+str(2*(i+1))+'.jpg',cv2.IMREAD_GRAYSCALE))
    labels.append(1)

6. Extract the face area

Here, no processing is performed on the pictures where the face area is not detected. For the face area, about 20 pixels around it are extracted

def processing(imageslist):
    for j in range(len(imageslist)):
        faces = faceCascade.detectMultiScale(imageslist[j], 1.1, 4)
        for x,y,w,h in faces:
            if x >= 20 and y >= 20:		# 未处理没检测到人脸的情况
               imageslist[j] = imageslist[j][y-20:y+h+20, x-20:x+w+20]
    return imageslist

7. Establish LBPH face recognition model

The LBPH face recognition module is used here, because this method does not require the image data set to have a uniform size, and the EigenFaces and Fisherfaces modules both require the training set image and the test image size to be consistent.

imagesCopy = images
# 提取图像中人脸的区域
imagesCopy = processing(imagesCopy)
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(imagesCopy, np.array(labels))

8. Real-time detection

In face detection, if no face is detected, the face returned is an empty tuple. After the face is detected, the face area is extracted, and recognizer.predict is used for recognition. For the return value of confidence, the result is less than 50. Reliable, but greater than 80 is considered to be a greater difference.

while True:
    success,img = cap.read()
    predict_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face = faceCascade.detectMultiScale(predict_image, 1.1, 4)
    # print(face)     # 空的元组有可能
    if face != ():
        for x, y, w, h in face:
            if x >= 20 and y >= 20:
                faceArea = predict_image[y-20:y+h+20, x-20:x+w+20]
                cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 1)
                label, confidence = recognizer.predict(faceArea)
                if confidence <= 80 and label == 0:
                    cv2.putText(img, "LQL",(x, y-20), cv2.FONT_HERSHEY_COMPLEX, 1,(255,0,0,1))
                    print("YOU ARE LQL")
                else:
                    cv2.putText(img, "OTHERS", (x, y-20), cv2.FONT_HERSHEY_COMPLEX, 1,(255,0,0),1)
                    print("OTHERS")
                    print(confidence)
                    print(label)
    else:
        cv2.putText(img, "None", (320, 240), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 255), 1)
        print("None")
    cv2.imshow("img", img)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

9. Test results

It can basically realize the function of specific face recognition.

10. Shortcomings

  1. No separate training section and detection section, each time resulting in re-training, consume a certain amount of time, at present only a recognition; - Resolved
  2. Since the data is a picture derived from the video, it has not dealt with the undetected face situations that may exist in the training set;
  3. The size of all pictures has not been resized to a uniform size, so the LBPH module can only be used for face recognition. However, on the other hand, using cv2.resize may have the problem of image ratio imbalance and loss of training effect;
  4. In addition, for negative samples, only background images are used, and objects can be tried later.
    In short, only a simple real-time face recognition function has been implemented at present, and there will be time to improve it later.

11. Improvement methods

  1. The following sentence can save and read the trained model, so the training part can be separated from the recognition part
recognizer = cv2.face.LBPHFaceRecognizer_create()

recognizer.save('./resource/face_detect/MyFaceModel.xml')
recognizer.read('./resource/face_detect/MyFaceModel.xml')

Guess you like

Origin blog.csdn.net/weixin_45371989/article/details/107707761