Python computer vision -- face recognition in images and videos

I will share some of the articles and papers I refer to - the links are posted in the original text. I upload the papers to the resources, and you can download them for free. If you can't find the papers in the resource area that day, wait. They may be under review, and you can download them after review. Let's study together and make progress together! come on.

This blog post mainly refers to: OpenCV official website 

catalogue

I. preparatory work

1 basic ideas

2 preparation

Face recognition in binary images

Face recognition in three videos

1 face recognition in video files

2 face recognition under camera

IV. conclusion

I. preparatory work

1 basic ideas

        Generally speaking, face recognition is completed by classifier, and this classifier needs to be trained by special classifier training methods of machine learning or deep learning. For example, various features of classification targets are obtained by inputting a large number of positive images (target images to be classified) and negative images (object images not training targets), The trained classifier can recognize and judge the input image. In this blog post, we will certainly not train a classifier ourselves, but opportunely get the ready-made face recognition classifier provided by OpenCV for recognition.

2 preparation

First, we need to get the classifiers provided on the official website, that is, several xml files that have been encapsulated, as follows:

There are many ways to download these classifiers, and some bloggers have made it clear. They will not be described in detail here. If necessary, you can refer to the resources provided by the blogger. The download speed in the online disk resources provided by him is also very fast:

All XML downloads of haar cascade classifier in opencv

Face recognition in binary images

        After downloading the classifier, import it in the same way as reading the image. Just enter the classifier in the CascadeClassifier() function   The save path of the xml file is OK. Since the eyes will certainly be on the face, the recognition and detection of eyes can be carried out after face recognition, which can reduce the amount of calculation. The code is as follows. Notes have been added. If there are unclear explanations, you can refer to the official website or discuss them in the comment area:

"""
Author:XiaoMa
date:2021/11/23
"""
import cv2 as cv

face_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_frontalface_default.xml')  #Import face recognition classifier
eye_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_eye.xml')                   #Import eye recognition classifier

img = cv.imread('E:\From Zhihu\For the desk\\cvsixteen1.jpg')     #Read image
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)                        #Convert image to grayscale image

faces = face_cascade.detectMultiScale(gray, 1.1, 5)               #The size of the face classifier and the number of drawn block diagrams are limited
for (x, y, w, h) in faces:
    cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)     #Rectangle the face
    roi_gray = gray[y:y+h, x:x+w]                                 #The purpose of establishing this data is to reduce the amount of calculation in eye recognition
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)                 #Define eye classifier
    for (ex, ey, ew, eh) in eyes:
        cv.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 0, 255), 2) #Frame the eyes
cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

The results obtained are as follows:

 

It can be seen that the effect of the classifier provided on the official website is OK, but if we change the image:

 

        It can be seen that the trained classifier is not completely reliable, which may be related to the number of training samples used in training the classifier and the diversity of samples. Some knowledge points such as over fitting and under fitting in machine learning will be explained later.

Face recognition in three videos

1 face recognition in video files

"""
Author:XiaoMa
date:2021/11/23
"""

import cv2 as cv

face_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_frontalface_default.xml')  #Import face recognition classifier
eye_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_eye.xml')                   #Import eye recognition classifier

capture = cv.VideoCapture('E:\From Zhihu\For the desk\\facedetection.avi')  #You can import video information through the videoCapture() function
while(True):
    # Get a frame
    ret, frame = capture.read()  #After successful reading, ret returns a Boolean value of true, and frame returns a read image 
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)   #It was noted in the previous image recognition
    #It has been roughly annotated
    for (x, y, w, h) in faces:
        cv.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
    frame = cv.resize(frame, dsize = None, fx = 0.3, fy = 0.3)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('b'):         #Press the 'b' key to exit the window
        break

The results obtained are as follows (since the gif format image is not made, a simple screenshot is taken to show you the effect):

2 face recognition under camera

It mainly modifies the VideoCapture() function. There are no major changes in other functions. Please refer to the previous

"""
Author:XiaoMa
date:2021/11/23
"""

import cv2 as cv

face_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_frontalface_default.xml')  #Import face recognition classifier
eye_cascade = cv.CascadeClassifier('E:\Python\opencvclassifier\data\haarcascades\haarcascade_eye.xml')                   #Import eye recognition classifier
capture = cv.VideoCapture(0)  #When the function parameter is set to 0, the computer device camera is turned on by default
while(True):
    ret, frame = capture.read()
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        cv.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('b'):
        break

The effect is OK:

IV. conclusion

        This blog post mainly refers to the OpenCV official website and uses the provided ready-made classifiers for image, video and face recognition under the camera. You can also go to the official website for detailed learning. As for the training of classifiers, we have a long way to go. We will update them slowly in the future. Come on!

Keywords: Python OpenCV AI Computer Vision image processing

Added by TylerL on Fri, 26 Nov 2021 00:05:28 +0200