Examples of implementing face recognition using OpenCV in python

Source: Internet
Author: User
This article describes python's use of opencv for face recognition. It has good reference value. Next, let's take a look at it. This article mainly introduces python's use of opencv for face recognition. It has good reference value. Let's take a look at it with the small editor.

Follow these steps:

1. face detection

2. face preprocessing

3. machine learning algorithms for Face Training

4. face recognition

5. final work

Face detection algorithm:

The basic idea behind Haar-based face detectors 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 cheek, and the mouth should be darker than the cheek. It usually runs around 20 such comparisons to determine whether the detected object is a face. In fact, it often performs thousands of times.

The basic idea of a face detector based on BPS is similar to that of a Haar-based face detector, but it compares the pixel brightness histogram, such as the histogram of edges, corners, and flat areas.

These two face detectors can be used to train a large image set to find a face. These image sets are stored in an XML file in opencv for later use.

These cascade classification detectors usually require at least 1000 unique face images and 10000 non-face images for training. the training time generally takes a few hours,

Haar takes one week.

The key code in the project is as follows:

InitDetectorsfaceCascade. load (faceCascadeFilename); eyeCascade1.load (eyeCascadeFilename1); eyeCascade2.load (eyeCascadeFilename2); initWebcamvideoCapture. open (cameraNumber); cvtColor (img, gray, CV_BGR2GRAY); // reduce the image size to make the detection run faster, and then restore the original size resize (gray, inputImg, size (scaledWidth, scaledHeight); equalizeHist (inputImg, equalizedImg); cascade. detectMultiScale (equalizedImg ......);

Face preprocessing:

In practice, the training (acquisition image) and testing (from the camera image) images are very different, such as illumination, face orientation, expressions, etc ),

The results will be poor, so datasets used for training are very important.

Face preprocessing aims to reduce such problems and improve the reliability of the entire face recognition system.

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

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

Use the trained eye detector that comes with OpenCV. For example, after the front face detection is completed, a person's face is obtained. the left and right eye areas of the face are extracted using the eye detector, and the histogram is balanced for each eye area.

This step involves the following operations:

1. Geometric transformation and cropping

Face alignment is very important. rotating a face keeps the eyes horizontal. scaling the face keeps the distance between the eyes the same. translating the face keeps the eyes horizontally centered at the required height,

Crop the peripheral face (like background, hair, forehead, ears, and chin ).

2. use histogram balancing for the left and right sides of the face

3. smooth

Using bilateral filters to reduce image noise

4. elliptical mask

Remove the remaining hair and face image background

The key code in the project is as follows:

Const Mat & face, centers & eyeCascade1, centers & eyeCascade2, Point & leftEye, Point & rightEye, Rect * searchedLeftEye, Rect * searchedRightEye); Centers = face (Rect (leftX, topY, widthX, heightY); // detect the detectLargestObject (topLeftOfFace, eyeCascade1, leftEyeRect, topLeftOfFace in the left face area. cols); // The right eye is similar, so that the center of the eye is leftEye = Point (leftEyeRect. x + leftEyeRect. width/2, leftE YeRect. y + leftEyeRect. height/2); // Obtain the midpoint of the two eyes, and then calculate the angle Point2f eyesCenter = Point2f (leftEye. x + rightEye. x) * 0.5f, (leftEye. y + rightEye. y) * 0.5f); // Affine Warping requires an Affine matrix rot_mat = getRotationMatrix2D (eyesCenter, angle, scale ); // Now you can change the face to obtain the detected eyes in the required position of the face warpAffine (gray, warped, rot_mat, warped. size (); // histogram balancing (leftSide, leftSide) is performed on the left and right sides of the face first; equalizeHist (rightSide, rightSid) E); // merge again. here, when the merge is performed on the left side and on the right side of the 1/4, the pixel value in the middle of the 1/4 region is calculated for processing. // BilateralFilter (warped, filtered, 0, 20.0, 2.0); // use an elliptical mask to delete some regions filtered. copyTo (dstImg, mask );

Collect and train faces:

A good dataset should contain various situations of face transformation, which may occur in the training set. If you only test the front face, you only need to train the image to have a full front face.

Therefore, a good training set should contain many actual situations.

The images collected in this project are separated by at least one second. the similarity between two image pixels is compared using the L2 norm-based relative error rating standard.

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

Compare with the threshold for collecting new faces to determine whether to collect the image.

You can use many techniques to obtain more training data, such as using image faces, adding random noise, changing certain pixels of face images, and rotating them.

// Flip (preprocessedFace, mirroredFace, 1 );

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

Face recognition algorithm:

1. feature face, also known as PCA (principal component analysis)

2. Fisher's face, also known as LDA (linear discriminant analysis)

3. Local Binary Pattern Histograms)

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

OpenCV provides the CV: Algorithm class, which has several different algorithms. one of them can be used to complete simple and common face recognition.

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

initModule_contrib();model = Algorithm::create
 
  (facerecAlgorithm);model->train(preprocessedFaces, faceLabels);
 

This code executes the entire training algorithm for the selected face recognition.

Face recognition:

1. Face Recognition: identify this person through a face

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

int identity = model->predict(preprocessedFace);

The problem with it is that it can always predict a given person (even if the input image does not belong to the person in the training set ).

The solution to this problem is to develop confidence level standards. if the confidence level is too low, it can be interpreted as an unknown person.

2. face verification: verify whether there are people in the image you want to find.

In order to verify the reliability or whether the system can correctly identify a person you don't know, face verification is required.

Here, the confidence level is calculated as follows:

Use feature vectors and feature values to reconstruct the face image, and then compare the input image with the re-graph. If a person has multiple face images in the training set, use feature vectors and features

Value reconstruction should have a very good effect. if there is no difference, it indicates that it may be an unknown 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.

Ending: interactive GUI

Using OpenCV functions, you can easily draw components and click on them.

The above is a detailed description of the examples of implementing face recognition using OpenCV in python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.