Abimbola Olawale Victor
4 min readDec 22, 2018

FACIAL RECOGNITION USING OpenCV

Facial Recognition

A facial recognition system is a technology capable of identifying or verifying a person from a digital image or a video frame from a video source. There are multiple methods in which facial recognition systems work, but in general, they work by comparing selected facial features from given image with faces within a database. It is also described as a Bio-metric Artificial Intelligence based application that can uniquely identify a person by analyzing patterns based on the person’s facial textures and shape.

Facial recognition can be applied in different ways such as social media platforms, ID Verification Solutions, Face ID, National security and so on.

There are different approaches in building a facial recognition system but for the purpose of this post OpenCV is been considered.

OpenCV

OpenCV (Open source computer vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). The library is cross-platform and free for use under the open-source BSD license.

OpenCV supports the deep learning frameworks TensorFlow, Torch/PyTorch and Caffe.

STEP BY STEP PROCESS IN BUILDING THE FACIAL RECOGNITION SYSTEM

Requirement

· Numpy

· CV2

· OS

· sqlite3

If all the dependencies are installed, then it’s time to start writing the code to build up the face recognition system.

STEP 1: CREATE THE DATABASE

The first thing to do is to create the database to store the faces. Sqlite3 is been used and the database file is automatically generated into the working directory folder.

The copy of the code is pasted below:

Source: Authors Computation

STEP 2: CREATE THE DATASET

The next step is to create the dataset in which we will be using to train.

Our dataset generator is going to capture few sample faces of one person from the live video frame and assign a ID to it and it will save those samples in a folder which will be generated automatically called dataset.

For those who don’t know how we captured the face, “gray[y:y+h,x:x+w]” part where x,y is the top left coordinate of the face rectangle and h,w is the height and the weight of the face in terms of pixels. We want to capture faces from different angles and for that it needs to be slow and 20 samples of pictures were taken.

Note that haarcascade_frontalface_default.xml is necessary and it is provided in the OpenCV directory.

It is advice to copy the haarcascade_frontalface_default.xml file in your current working directory folder because the file is a great requirement for the facial recognition system.

The copy of the code is pasted below.

Source: Authors Computation
Source: Authors Computation

So our main loop is done now we just have to release the camera and close the windows.

STEP 3: TRAINNING THE GENERATED DATA

For that we need to initialize the recognizer and the face detector in order to train the captured images.

OpenCv provides three (3) methods of face recognition which are Fisherfaces, Eigenfaces and Local Binary Pattern Histograms(LBPH). But for this purpose, LBPH was considered and this method performs the recognition by comparing the faces to be recognized with some training set of known faces.

Therefore the algorithm will work on the faces ant it will tell which face they belongs to. The copy of the code is shown below:

Source: Authors Computation

STEP 4: DETECTING THE FACE

Finally the last thing is to run the last code which is the facial recognition app in which the code is pasted below:

Source: Authors Computation
Source: Authors Computation