The final project of Python course in university, with immature code and shallow language, is just to record my university experience, please forgive me.

  1. Description of project

In our daily life, whether it is payment or punching in and out, face recognition is inseparable from this function. This function has strong security and convenience, and can help us get a lot of data through face matching.

A Python based OpencV face recognition project, through the saved picture gray processing training data can achieve face detection and recognition function.

Goal:

1. Detect faces

2. Recognize faces corresponding to your training set

Tools:

1.Python

2.pycharm

3.OpenCV

 

Environment configuration: After downloading Python and PyCharm, you need to download the OpencV package, which can be downloaded by CMD through PIP.

pip install opencv-python

pip3 install opencv-python -i pypi.tuna.tsinghua.edu.cn/simple

Generally speaking, using a domestic image download faster, less prone to connection timeout.

After downloading the OpencV Python package, you need to download the OpencV package from the official website.

opencv.org/

After downloading, we need to update our environment variables. In my computer – right-click – Properties – Advanced Program Settings – Environment variable – Path property, we need to update the path of OpencV environment configuration is almost complete.

2. Project steps

Step 1: First learn how to open images in Python.

First import our CV module (this module is required for the following steps)

Images in the path can be read by CV. Imread in CV module, and the current picture can be displayed by CV. Imshow. The current window can be displayed and not refreshed by waitkey method (all image detection needs to set the key value to 0), and finally the memory can be released.

Step 2: grayscale conversion. Opencv’s recognizer detects faces in grayscale images, so we need to turn the images into grayscale images before detection. Just add the cv.cvtColor method to the original code.

Step 3: Modify the size. Since our subsequent training pictures need a unified specification, we need a unified size for each photo. Add cv.resize to the original code.

Step 4: draw a rectangle or circle, we need a target box to frame our face, so we need to draw a few graphics.

First we need the base coordinates and the length and width of the rectangle. Here we initialize it to 100. For the rectangle, based on the base coordinates, we draw a rectangle of x+ W, y+ H with adjustable color and width.

Step 5: face detection, this step requires us to use the XML detection tool in OpencV packet, find our OpencV folder -sources-data-haarcascades- haarCascade_frontalface_alt2.xml. I’m going to use face detection by default. Copy and paste this path into our parser.

Face_detect is our face recognizer. Gray-scale images are used for face recognition.

The parameters are respectively, yan target image, recognition (resolution), face detection several times, default 0, face recognition minimum size, face recognition maximum size.

When we get the recognized face after we need to put our drawing graphics on our face, or by reference coordinates and matrix length and width to draw.

Test one:

Detect multiple:

Step 6: Train the data. When we have the photos, we need to train the data. Here, we need to number and name the photos and get the folder where we put the photos.

Here we need to go through the photo ourselves to get the face data and ID number.

Firstly, all the photos we have are stored in the data and then traversed, each photo is grayed, and the images we obtain are converted to data (using NP.array (PIL_img, ‘Uint8 ‘) method), and then through the detection Multiscale method, the face features of the images are acquired. Number and name each photo with a face.

Save the face data and ID parameters we obtained.

Using cv2. Face. LBPHFaceRecognizer_create () method gets the current data and use the train started training.

Finally, store our data in a YML file.

Step 7: face recognition, first of all, load the data we trained in the last step, ready to identify the photos, you can also open the camera to detect our face. Grayscale our photos and add our recognizer and capture our faces. We then started drawing rectangles and circles of our faces and used Recogizer.predict to compare our face database with the values of our current photo

Get a confidence score and add a name to our border when we have more than 80% similarity.

  1. Project real column

1. Face detection:

1. Multi-person detection:

1. Picture recognition:

1. Multi-party recognition:

3. Summary

The project uses OpencV to complete face recognition function, through face detection, drawing graphics. According to the needs of face storage, detection, recognition and other functions.