Face recognition — OpenCV

Anchit Jain
Data Science 101
Published in
4 min readAug 8, 2018

Face recognition has evolved as one of the most widely used biometric in the recent times. Face recognition has stamped its uses in fields like auto door lock-unlock, criminal face detection, auto car start and it’s one of my favourites.

In this blog post, I’ll demonstrate how you can recognise face using python.

Being a path finder, something with a single way doesn’t please me much so here I have tried to recognise the face using different ways. I have implemented the architect of OpenCV using python and the entire summary can be jotted down in just few simple steps.

  1. Reading and gathering the data.
  2. Training the dataset.
  3. Detecting the face (creating box around the face and mapping the result with correct name)
  4. Recognizing the name.

Before we dive into coding stuff a little knowledge about the algorithm helps you to understand the picture more clearly

OpenCV-Python is a library of Python bindings designed to solve computer vision problems.Using this library we not only can read image as input to our model but also we can use to detect and recognize the face of the given person.

Let’s see how the things works with the simple implementation of code.First things first,prepare your data. Below is the folder structure which is must for this code.

training-data →person1,person2

Where person1 and person2 contains 10–15 or more training images of a person to be recogninzed .

test-data →test1.jpg,test2.jpg…..some test images.

So this is how you need to structure your images to train.Moving ahead, create a list of labels don’t worry labels are nothing but the names of the person you want to train just remember the order of the names in the list should match with ordering of images folder in training-data and test-data.

We will begin with preparing our data.For this task we need to declare two list, one for face and another for storing the corresponding name for the trained faces.You can avoid this step by keeping the folder name with the same as the index of the recognized face in the label list because our end goal is to just to train the face with the name.Using an external source of labels is preferred for large number of dataset as per good ML standards.

Read data from source folder

Done with data preparation ? We now want to detect the face by drawing the rectangle around the face.Below code of lines will help you in this.Big thanks to openCV for making this simple.Since we are using openCV multi scale detection for which we need to first convert the original image in gray scale.OpenCV comes with a in-built detector which will help us to detect the region of interest (ROI) and the method returns the coordinates of the detected face.

Face Detection using Haar Cascades

You can use either LBP classifier (fast in process) or Haar cascade classifier which is quite slow as compared to LBP but more accurate.You can download these xml files from here and here

Detecting face.

Once we have gained the co-ordinates of detected face we can create functions to draw the box around the face and and write the name of recognized person.

Now comes the most important part of this blog. How can we forget about classifier of course the entire story is about classifying an image with correct label.Well again openCV did this with an great ease.What if i say with single line of code ? Right, you heard me.

Although its seems easy to use but let’s try to understand the working of this classifier.First, the original image is converted into gray scale in order to opt the value of pixel in binary format.A scrolling matrix window of size 3 x 3 is rolled over the gray image of pixels in the range (0–255).Picking up the central value of matrix as a threshold value and remaining values are converted is being converted in the 0 or 1 by setting 1 if the value of pixel is equal or greater than threshold value and 0 if the value is less than threshold value.Now we have linear binary number for eg. 11001010 (ignoring the central value) and this linear binary number is converted into a decimal number which is set back to the original image with new better characteristic.

Architect Diagram of LBHP

Next, a grid of 8 x 8 is placed on the converted image and a histogram is formed after creating a bar of these grid.All these bars are then amalgamated into a histogram and then compared with histogram of the original image.

Finally,we need to find the image that matches the input image we just need to compare two histograms and return the image with the closest histogram using Euclidean distance.

For further insights read here.

Here comes the final part of this blog,prediction.Once again OpenCv has made this very easy for us. Just loading the test image, using opencv’s face_recognizer.predict method with faces and labels,calculating the accuracy of test model calling the text and rectangle method and we are done.

After running the entire code, you can see the output below.

Well this is only one way to recognize your face.I have also implemented the same task using other algorithms like KNN and CNN .

You can download the full source code from here.

That’s all folk.

Thank you for your patience…..Claps(Echoing)

--

--

Anchit Jain
Data Science 101

Machine learning engineer. Loves to work on Deep learning based Image Recognition and NLP. Writing to share because I was inspired when others did.