Python Black Technology: 50 Lines of Code Using Python + OpenCV to Realize Face Tracking + Detailed Tutorial + Quick Start + Image Recognition + Face Recognition + Great God's Explanation

Hi my dearest guys, glad we meet again.

First of all, thank you for your attention. Of course, I would prefer to meet friends in computer-related fields and let's discuss and communicate together. The point is, I'm a real person, not a self-media organization that picks up articles, everyone can communicate with each other!

 

 

In this article, we will talk about some knowledge about AI-related face tracking and face recognition. Of course, this tutorial is the (top) part. I will talk about using python+opencv to realize face recognition and tracking. In the next part, I will use python to realize face verification and face unlocking through fingerprint comparison (everyone is interested in it.) You can pay attention in advance).

In these two classes, the amount of code is not very large. I will try to annotate as many points as possible to facilitate everyone's understanding. Then we won't talk too much nonsense, let's go straight to the dry goods!

 

 

OpenCV:

Opencv is currently a very popular visual library and can support multiple languages. When it comes to opencv, I have to say its cascades classifier.

If we want to judge whether a picture has a face, the early method is to use thousands of classifiers to match from the beginning to the end. There is nothing wrong with this, but there are too many pictures to judge? That might take the Year of the Monkey and the Horse Moon. As for the cascades of opencv, these containers for judging face features are divided into multi-block layers for matching, and when one layer does not match, they are discarded.

This is like a group of people going to a company for an interview. The first requirement of the company is that if only men are required, the group of women will leave. ,till the end. This kind of workload is much easier than going through the process of interviewing everyone regardless of gender.

Environment topology:

Operating system: windows7

python version: 2.7.14

opencv version: 3.x

Environment configuration:

1. Install python (uh... this is when I didn't say it)

2. Install Opencv

This is OK to download from the official website

 

 

After downloading, unzip it directly. It is recommended to unzip it to the parent path of your python installation.

3. Install numpy using pip

Open cmd and enter:

pip install numpy

To install, you will be prompted after the installation is complete.

 

 

4. Find your opencv installation path (for example, mine is D drive)

Copy cv2.py under the path of D:opencvopencv3.x uildpython .7\x64

 

 

Note: My windows version is 64-bit, so I choose X64, if yours is 32-bit, you need to select cv2.pyd in the X86 folder

After copying, paste it into the Lib/site-packages folder under your python installation path.

After completing the above, it is basically OK. Let's test whether the environment is successfully configured before writing the code.

Run python on the CMD command line:

import numpy

import cv2

If no error is reported, the installation is complete.

 

 

Implementation principle:

1. Call the computer camera

cv2.Videocamture(0)

2. Take the camera data by frame (equivalent to adding a frame to the face of each frame of the picture)

cam.red()

3. Bring the data recorded by each frame of camera into opencv and let the classifier judge the face

detectMultiScale()

4. If there is a face, mark the frame for the face

cv2.rectangle()

5. Frame animation after outputting the frame

cv2.imshow('My Camera',frame)

Code:

The first step is to define a recognition function:

Put the picture first, this place is more important, I will talk about it in detail below.

 

 

6-7 lines of code description:

First create the classifier, why do you want to do this?

The file at the beginning of the quoted haarcascade is about the face cascade classifier in opencv. You can see it in the sourcesdatahaarcascades under the opencv folder:

 

 

Description function:

Face detector (default): haarcascade_frontalface_default.xml

Face Detector (Fast Harr): haarcascade_frontalface_alt2.xml

Face detector (side view): haarcascade_profileface.xml

Eye detector (left eye): haarcascade_lefteye_2splits.xml

Eye detector (right eye): haarcascade_righteye_2splits.xml

Mouth detector: haarcascade_mcs_mouth.xml

nose detector: haarcascade_mcs_nose.xml

Body Detector: haarcascade_fullbody.xml

Face Detector (Fast LBP): lbpcascade_frontalface.xml

1 : haarcascade_frontalface_alt.xml

Stump-based 20x20 gentle adaboost frontal face detector.

2 : haarcascade_frontalface_alt2.xml

Tree-based 20x20 gentle adaboost frontal face detector.

3 : haarcascade_frontalface_alt_tree.xml

Stump-based 20x20 gentle adaboost frontal face detector.This detector uses tree of stage classifiers instead of a cascade

4 : haarcascade_frontalface_default.xml

Stump-based 24x24 discrete(?) adaboost frontal face detector.

The above four I have tested, haarcascade_frontalface_alt.xml this effect is the best, others can be tested individually.

Set grayscale:

The setting of grayscale is to enhance the contrast of facial contours, which is essential to increase accuracy.

Core code anatomy:

The DetectMultiScale function is a general function for detecting objects. Let's introduce:

 

 

gray: This is the image after converting to grayscale

scaleFactor: Compensation parameter, the general setting value is 1.1-1.5 is better, the effect can be changed by yourself

minNeighbors: Define how many objects are currently around it

minSize: Set the window size

flags: slightly abstract, choose the default or the above picture

Draw a frame:

Define xywh to use the for loop to read the coordinates in the faces variable, and then draw the box through the rectangle() function.

The lower part of the code:

 

 

OK, after writing the code, let's piece together the upper part of the code and the lower part of the code, and try it out:

 

 

Let's make do with it, my laptop cam is a bit scum, but it can be recognized perfectly with the shaking of people. Since I can't upload the video, everyone can test it by themselves. Finally, I would like to emphasize one point: the part of tomorrow and the day after tomorrow (below), use python to realize face verification and face unlocking through fingerprint comparison (if you are interested, you can pay attention to it in advance).

If you want to learn Python, you can add my group: 719+139+688, the editor has compiled the latest learning materials in 2018 and shared the live broadcast of the great god. Welcome friends who have not yet found a direction to learn.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325103994&siteId=291194637