Facial similarity using OpenCV and DLib.

Sai Ashish
2 min readApr 14, 2021

About a month ago, I had to develop a software that compares pictures and checks if they are the same person. After scouring the internet for tutorials on how to do this, I gave up and decided to do it on my own. I decided to write this blog post to help out anyone who needs to implement facial similarity in their project.

Steps involved:

  • Extracting the face using a hog detector.
  • Computing embeddings for the given face.
  • Finding the euclidean distance between the 2 faces to check similarity.

Extracting the face.

Given an input picture, we must first locate the face that is present in the picture. To do this, we are going to use a Histogram of Oriented Gradients (HOG) detector.

Computing embedding.

Once we get the location of the face using the hog detector from dlib. We will use this to get the pose of the face in 3D. This will help us normalize faces that are in different orientations. Once we get the pose using a 68 point pose predictor, we will use the openface 128-dimensional embedding model to get a 128D vector that describes the given face.

Pose Predictor Model, 128D embedding model.

Find Similarity between the faces.

Once we get the embeddings for both the faces, we can use this to compute the euclidean distance between the faces since the vector represents the features of the faces. If the distance between the faces is large, we can say that they are not the same person.

Conclusion.

Once we get the distance between the two faces, we can use this to see if the faces are of the same person or of two different people. The default threshold for the distance is ‘.6’. If the distance is greater than this then the faces belong to different people, and if it is lower then they are the same person.

We can modify this threshold to make the detection more strict or more lenient.

Complete code.

--

--