Detailed explanation of examples of face recognition implemented by OpenCV in python

Y2J
Release: 2017-04-27 11:27:24
Original
4903 people have browsed it

This article mainly introduces the relevant information on using opencv to implement face recognition in python. Has very good reference value. Let’s take a look with the editor below

The main steps are as follows:

1. Face detection

2. Face preprocessing

#3. Training machine learning algorithm from collected faces

4. Face recognition

5. Finishing work

Face detection algorithm:

The basic idea of ​​the Haar-based face detector is that for most areas on the front of the face, the area where the eyes are located should be darker than the forehead and cheeks, and the mouth should be Darker than the cheeks, etc. It typically performs around 20 of these comparisons to decide whether the detected object is a face, often thousands of times in fact.

The basic idea of ​​the LBP-based face detector is similar to the Haar-based face detector, but it compares pixel brightness histograms, for example, histograms of edges, corners, and flat areas.

These two face detectors can find faces by training large image sets. These image sets are stored in XML files in opencv for subsequent use.

These cascade classification detectors usually need to use at least 1,000 unique face images and 10,000 non-face images as training. The training time generally takes several hours for LBP and

Haar takes One week.

The key codes in the project are as follows:

initDetectors
faceCascade.load(faceCascadeFilename);
eyeCascade1.load(eyeCascadeFilename1);
eyeCascade2.load(eyeCascadeFilename2);

initWebcam
videoCapture.open(cameraNumber);

cvtColor(img, gray, CV_BGR2GRAY);
//有需要则缩小图片使检测运行更快,之后要恢复原来大小
resize(gray, inputImg, Size(scaledWidth, scaledHeight));
equalizeHist(inputImg, equalizedImg);
cascade.detectMultiScale(equalizedImg......);
Copy after login

Face preprocessing:

In practice, usually training (collecting images) and testing (from cameras Images) will be very different depending on (such as lighting, face orientation, expression, etc.),

The results will be poor, so the data set used for training is very important.

The purpose of face preprocessing is to reduce such problems and help improve the reliability of the entire face recognition system.

The simplest form of face preprocessing is to use the equalizeHist() function to do histogram equalization, which is the same as the face detection step.

In practice, in order to make the detection algorithm more reliable, facial feature detection (such as detecting eyes, nose, mouth, and eyebrows) will be used. This project only uses eye detection.

Use the trained eye detector that comes with OpenCV. For example, after frontal face detection is completed, a face is obtained, and the eye detector is used to extract the left eye area and right eye area of ​​the face, and histogram equalization is performed on each eye area.

The operations involved in this step include the following:

1. Geometric transformation and cropping

Face alignment is very important , rotate the face so that the eyes remain horizontal, scale the face so that the distance between the eyes is always the same, translate the face so that the eyes are always horizontally centered at the desired height,

Crop the periphery of the face (such as the image background, hair, forehead, ears and chin).

2. Use histogram equalization on the left and right sides of the face respectively

3.Smoothing

Use bilateral filter to reduce image noise

4. Ellipse mask

Put the remaining hair and face image background removal

The key codes in the project are as follows:

detectBothEyes(const Mat &face, CascadeClassifier &eyeCascade1, CascadeClassifier &eyeCascade2,
Point &leftEye, Point &rightEye, Rect *searchedLeftEye, Rect *searchedRightEye);
topLeftOfFace = face(Rect(leftX, topY, widthX, heightY));
//在左脸区域内检测左眼
detectLargestObject(topLeftOfFace, eyeCascade1, leftEyeRect, topLeftOfFace.cols);
//右眼类似,这样眼睛中心点就得到了
leftEye = Point(leftEyeRect.x + leftEyeRect.width/2, leftEyeRect.y + leftEyeRect.height/2);
//再得到两眼的中点,然后计算两眼之间的角度
Point2f eyesCenter = Point2f( (leftEye.x + rightEye.x) * 0.5f, (leftEye.y + rightEye.y) * 0.5f );
//仿射扭曲(Affine Warping)需要一个仿射矩阵
rot_mat = getRotationMatrix2D(eyesCenter, angle, scale);
//现在可变换人脸来得到检测到的双眼出现在人脸的所需位置
warpAffine(gray, warped, rot_mat, warped.size());

//先对人脸左侧和右侧分开进行直方图均衡
equalizeHist(leftSide, leftSide);
equalizeHist(rightSide, rightSide);
//再合并,这里合并时左侧1/4和右侧1/4直接取像素值,中间的2/4区域像素值通过一定计算进行处理。

//双边滤波
bilateralFilter(warped, filtered, 0, 20.0, 2.0);

//采用椭圆掩码来删除一些区域
filtered.copyTo(dstImg, mask);
Copy after login

Collect and train faces:

A good data set should Contains various scenarios of face transformations that may appear in the training set. If you only test frontal faces, you only need to have complete frontal faces in the training images.

So a good training set should contain many actual situations.

The images collected in this project are separated by at least one second, and the relative error evaluation criterion based on the L2 norm is used to compare the similarity between the pixels of the two images.

errorL2 = norm(A, B, CV_L2);
similarity = errorL2 / (double)(A.rows * A.cols);
Copy after login

Then compare it with the threshold for collecting new faces to decide whether to collect this image.

Many techniques can be used to obtain more training data, such as using mirrored faces, adding random noise, changing some pixels of the face image, rotating, etc.

//翻转
flip(preprocessedFace, mirroredFace, 1);
Copy after login

After collecting enough face images for each person, you must then select a machine learning algorithm suitable for face recognition, and use it to learn the collected data to train a face recognition system.

Face recognition algorithm:

1. Eigenface, also called PCA (Principal Component Analysis)

2. Fisher face, also called LDA (linear discriminant analysis)

3. Partial Binary Pattern Histograms (LBPH)

Other face recognition algorithms: www.face-rec.org/algorithms/

OpenCV provides CV::Algorithm class, this class has several different algorithms, and you can use one of these algorithms to complete simple and universal face recognition.

There is a FaceRecognizer class in OpenCV's contrib template, which implements the above face recognition algorithms.

initModule_contrib();
model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);

model->train(preprocessedFaces, faceLabels);
Copy after login

This code will execute the entire training algorithm for the selected face recognition.

Face recognition:

1. Face recognition: Identify the person through the face

You can simply call the FaceRecognizer::predict() function to identify the person in the photo ,

int identity = model->predict(preprocessedFace);

The problem it brings is that it can always predict a given person (even if the input image does not People who belong to the training concentration).

The way to solve this problem is to set a confidence standard. If the confidence is too low, it can be judged to be an unknown person.

2. Face verification: Verify whether there is the person you are looking for in the image

In order to verify whether it is reliable, or whether the system can correctly Correct identification of an unknown person requires face verification.

The method of calculating confidence here is:

Use eigenvectors and eigenvalues ​​to reconstruct the face map, and then compare the input image with the reconstructed map. If a person has multiple face images in the training set, reconstruction with feature vectors and feature

values ​​should have very good results. If not, there will be a big difference, indicating that it may be an unknown person. Face.

The subspaceProject() function maps the face image to the feature space, and then uses the subspaceReconstruct() function to reconstruct the image from the feature space.

Finding: Interactive GUI

It is easy to draw some components, mouse clicks, etc. using OpenCV functions.

The above is the detailed content of Detailed explanation of examples of face recognition implemented by OpenCV in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template