Electronics

Real-Time Face Recognition Using Python And OpenCV


A real-time face recognition system is capable of identifying or verifying a person from a video frame. To recognize the face in a frame, first, you need to detect whether the face is present in the frame. If it is present, mark it as a region of interest (ROI), extract the ROI, and process it for facial recognition.

Real-time Face Recognition – Step-by-step Guide

This project is divided into two parts: creating a database, and training and testing.

Creating a Database

Take pictures of the person for face recognition after running the create_database.py script. It automatically creates a Train folder in the Database folder containing the face to be recognized. You can change the name from Train to the person’s name.

While creating the database, the face images must have different expressions, which is why a 0.38-second delay is given in the code for creating the data set. In this example, we take about 45 pictures/images extract the face, convert it into grayscale, and save it to the database folder with its name.

Training and Testing

Training and face recognition are done next. face_rec.py code does everything. The algorithm used here is Local Binary Patterns Histograms (LBPH).

Haar features - real time face recognition
Fig. 1: Haar features

Face detection is the process of finding or locating one or more human faces in a frame or image. Haar-like feature algorithm by Viola and Jones is used for face detection. In Haar features, all human faces share some common properties. These regularities may be matched using Haar features, as shown in Fig. 1.

Two properties common to human faces are:

  1. The eye region is darker than the upper cheeks.
  2. The nose bridge region is brighter than the eyes.

Composition of two properties forming matchable facial features are:

  1. Location and size including eyes, mouth, and bridge of nose.
  2. Value for oriented gradients of pixel intensities.

For example, the difference in brightness between white and black rectangles over a specific area is given by:

Value = Σ (pixels in black area)- Σ (pixels in white area)

The above-mentioned four features matched by the Haar algorithm are compared in the image of a face shown on the left of Fig. 1.

Testing Procedure

Install OpenCV and Python on Ubuntu 16.04

The project was tested on Ubuntu 16.04 using OpenCV 2.4.10. The following shell script installs all dependencies required for OpenCV and also install OpenCV 2.4.10.

$ sh ./install-opencv.sh

After installing OpenCV, check it in the terminal using the import command, as shown in Fig. 2.

Checking OpenCV using import command
Fig. 2: Checking OpenCV using the import command
Creating the database for Real-time face recognition
Fig. 3: Creating the database

1. Create the database and run the recognizer script, as given below (also shown in Fig. 3). Make at least two data sets in the database.

$ python create_database.py person_name

2. Run the recognizer script, as given below:

$ python face_rec.py

This will start the training, and the camera will open up, as shown in Fig. 4. Accuracy depends on the number of data sets as well as the quality and lighting conditions.

 Screenshot of real time face recognition
Fig. 4: Real-time Face Recognition

OpenCV 2.4.10.

OpenCV provides the following three face recognizers:

  1. Eigenface recognizer
  2. Fisherface recognizer
  3. LBPH face recognizer

In this project, LBPH face recognition is used, which is the createLBPHFaceRecognizer( ) function.

LBP works on gray-scale images. For every pixel in a gray-scale image, a neighborhood is selected around the current pixel, and the LBP value is calculated for the pixel using the neighborhood.

After calculating the LBP value of the current pixel, the corresponding pixel location is updated in the LBP mask (it is of the same height and width as the input image) with the LBP value calculated, as shown in Fig. 5.

LBPH face recognizer
Fig. 5: LBPH face recognizer

In the image, there are eight neighboring pixels. If the current pixel value is greater than or equal to the neighboring pixel value, the corresponding bit in the binary array is set to 1. But if the current pixel value is less than the neighboring pixel value, the corresponding bit in the binary array is set to 0.

Download source code

Interested in face detection projects? Check out face recognition using Raspberry Pi.


This article was first published on 21 July 2017 and recently updated on August 2023.