A Practical Intro to Face Comparison

Face comparison and face detection, use-cases, and the Face Comparison API

Fabio Chiusano
BrainBotAPI
5 min readJul 26, 2022

--

Photo by Emile Guillemot on Unsplash

Today, face detection and face comparison are widely used in a variety of settings, from unlocking your smartphone to identifying criminals. The technology is constantly evolving, and it’s important to stay up-to-date on the latest developments in order to make the best use of these tools.

Face Detection and Face Comparison

Face detection is a computer vision technology that enables a system to detect human faces in digital images. Face detection can be used to automatically tag photos, identify individuals in security video footage, or locate faces in a crowd.

Face comparison is a technique used in computer vision to compare two or more faces in order to determine whether they are the same person. Face comparison works by first extracting features from each face image that can be used for comparison. These features can include things like the shape of the face, the size of the eyes, and the position of the nose. Once these features have been extracted, they are then compared to see if they match. If they do match, then it is likely that the two faces are of the same person.

One of the challenges with face comparison is that there can be a lot of variation in how people look in photos. For example, a person may wear different clothes or grow hair differently over time.

Real-world applications of face comparison

There are several applications of face comparison, such as:

  • Law Enforcement: Used by law enforcement agencies to compare a suspect’s face to a database of known criminals, helping in identifying a suspect or ruling out a suspect in a criminal investigation.
  • Border Control: Used by border control agents to compare the faces of people entering a country to a database of known criminals and terrorists. This can help prevent dangerous people from entering a country.
  • Retail Security: Used by retail security to compare the faces of people entering a store to a database of known shoplifters to prevent thefts.
  • Airport Security: Used by airport security to compare the faces of passengers to a database of known criminals and terrorists, helping in preventing dangerous people from boarding a plane.
  • ATM Security: Used by ATM security to compare the faces of people using an ATM to a database of known lawbreakers. This can help prevent criminals from using an ATM.
  • Social Media: Face comparison can be used by social media companies to compare the faces of users to a database of people of interest.

Training a Face Comparison model

There are pros and cons to training your personal face comparison model versus using an existing API that provides that service. On the pro side, training your own model gives you more control over the accuracy of the results and you may customize it to your specific needs. On the con side, training your own model takes far more time and effort (and knowledge) than using an existing API, and you also need a large dataset of faces in order to train a model accurately.

To keep things simple, in this article we’ll test the Face Comparison API from BrainBotAPI. There’s a free tier of 100 API calls per month, which are enough for personal testing.

Using the Face Comparison API

The API is hosted on RapidAPI, which is a marketplace of APIs. This makes testing the API easy and quick, indeed we just need to:

  1. Signup to RapidAPI.
  2. Subscribe to the Face Comparison API.
  3. Copy a code snippet proved by RapidAPI and test the API.

You can sign up for RapidAPI here. Then, visit the page of the Face Comparison API and click on the blue button “Subscribe to Test”.

The RapidAPI page for the Face Comparison API. Press the blue button in the red square to enable the API for your account.

RapidAPI will show you a page with several plans for the Face Comparison API. Subscribe to the Basic plan. You may need to add payment details to RapidAPI at this step.

Next, copy a code snippet from the Face Comparison API page. There are code snippets in a variety of programming languages and libraries, such as NodeJS, JavaScript, Java, Swift, Python, PHP, and many others. We’ll choose a Python code snippet with the requests library.

The RapidAPI page for the Face Comparison API. Code snippets can be found in the red square.

We are now ready to test it! The API works by specifying the URLs of two public images. We’ll test with the following three images where the first and the second show the same person, whereas the third shows a different person.

First image. Photo by Sigmund on Unsplash.
Second image. Photo by Sigmund on Unsplash.
Third image. Photo by Jake Nackos on Unsplash.

Let’s define a function to compute similarities between faces using the code snippet from RapidAPI.

We can then use it to check the similarity between the three images shown before. Remember to use your own RapidAPI key!

The first two images, depicting the same person from different angles, got a score of about 0.746 , which means that they’re probably the same person (indeed, it is). The comparison between the first and the third image got a score of about 0.345 , which correctly suggests that they may be different people.

By applying a threshold to the similarity scores, we can then make decisions on whether the two images show the same person or not.

Next steps

Possible next steps are:

--

--

Fabio Chiusano
BrainBotAPI

Freelance data scientist — Top Medium writer in Artificial Intelligence