31st Jan 2020

AI / ML Based Facial Detection and Recognition

Artificial Intelligence, Machine Learning, Python

Written By, Ayaz Saiyed



‘You are being watched’ – the truth that surrounds all of us in this era of Artificial Intelligence, ML, and Computer Vision.

Whenever we hear about face recognition, we start thinking of surveillance and human face recognition, just like it’s done in the movies by the cops and secret agencies to maintain the security.

But, soon every human will be trackable and recognizable with the help of these AI algorithms, which we are going to discuss.

Just take a deep breathe and think

Which applications or products come to your mind while thinking of Computer Vision / Artificial Intelligence ?


Huhh !! Yes, the list is long enough .

We use some of them in our day to day life!

Few features like unlocking cell phones, doors, smartphone cameras, self-driving cars, and many more uses face recognition feature.

One of the most trending areas of research over the last few years in the field of Computer vision is the one and only – Face detection and Recognition.

So, here we are going to see how it works.


Computer Vision is a field of deep learning that enables machines to see, identify and process images like humans

‘Face detection’ is a computer vision technology that helps to locate / visualize the human faces in images and predicts the identity of that face.


Yes, it is possible ! !

So, finally it’s time to reveal how face recognition works !

How does it work ?

The Face Recognition System uses accurate computer algorithms
to pick out the specific, distinctive details about a person’s face.

Such as the distance between the eyes or shape of the chin, are converted into a mathematical representations & compared to data of other faces which are collected in a face recognition database.

The secret behind face recognition is a technique called ‘deep metric learning ’.

So, here we are going to see how these deep learning + face recognition works.

Our network quantifies the faces, by constructing the 128-d embedding (quantification) for each face .

The idea is to tweak the weights of our neural network so that the 128-d measurements of the two Will Ferrel will be closer to each other and farther from the measurements for Chad Smith.

Our facial recognition is based on ResNet-34. The network itself is trained on more than 3 million images. On the Labeled Faces in the Wild (LFW) dataset the network compares to other state-of-the-art methods, reaching 99.38% accuracy.

Steps to implement face recognition

Before we can recognize faces in images and videos, we first need to quantify the faces in our training set.

And yes, let’s be clear that we are not training a network (model) here — the network has already been trained to create 128-d embeddings on a dataset with more than 3 million images.


Instead, it’s easier to use the pre-trained network and then use it to construct 128-d embeddings for each of the images in dataset.

Then during the classification, we will use a simple k-NN model + votes to make the final face classification.

STEP 1 – Creating encoding of faces

  • Builds ‘image_paths’ from the path to the directory where the dataset (collection of user images) are stored.
  • Then it does the task of embedding .

For ex, Architecture of the dataset will be as such:

  • All the images of the person are stored in the folder with their name.
  • Here the script will map/ embed the ‘encodings’ and ‘name’ of the person, which is helpful while detecting the name of the matched face.

The output of these encode_faces.py will be ‘encodings.pickle’ — this file will contain the 128-dimensional face embeddings for each face in our dataset.

  • Algorithms complete execution in a few minutes while using GPU.
  • While in MAC it takes around 5-10 minutes to train but it depends on the number of images in the dataset .

Now after creating 128-d face embeddings for each image in our dataset, we are ready to detect & recognize the faces in the image.


Now hold on, we are a step away from the Face recognition system !


STEP 2 – Recognizing the faces

It requires three arguments for implementation,

  1. encodings: Path to the pickle file containing our face encodings.
  2. image: Path of the image to recognize the face from it.
  3. detection-method: Path to the detection method (for accurate results use ‘cnn’ (convolutional neural network) detection method)
  • It loads the pre-computed encodings + face names & then constructs a 128 dimensional face encoding for the input image.
  • Than converts the input image to RGB from BGR
  • Detects the multiple faces from the input image
  • Initializes the list of names for each face detected in the image

And here come the final logic for face recognition

  • In this code it attempts to match input images’s encodings with our dataset’s pre-computed encodings (encodings.pickle) using the method ‘face_recognition.compare_faces’
  • This function returns a list of True / False values, one for each image in our dataset.
  • For our ex, Suppose if there are 90 images in the dataset (30 images for each name) , so here the returned list will have 90 boolean values.

Overall description of how the face recognition actually works

Internally, the ‘compare_faces’ function is computing the Euclidean distance between the candidate’s embedding and all faces in our dataset.

If the distance is below some threshold then it returns True, indicating the faces match.

Otherwise, if the distance is above the threshold it returns False as the faces do not match.

While iterating over the loop of names, the ‘count’ value of the names that matches the threshold are updated, and finally the name with the maximum ‘counts’ will be the original identity of the face.

Real time face recognition system from live streaming



So, I hope you have learned some new things from this blog and about the importance of AI/ Ml in various fields which will change the future. You can download this demo project from github for understanding thoroughly.

If you are interested in this field then stay tuned with us for upcoming updates about machine learning and artificial intelligence and to know how we humans are surrounded by Artificial Intelligence and Machine learning techniques.

Don’t forget to share as these are the only things which will rule over the future.

Written By,

Python Developer at Yudiz Solutions Pvt. Ltd