Face Comparison in Python with similarity using Face++ API

Python is a wonderful language that has its application in all domains of development like machine learning, computer vision, data analytics, image processing, etc.
Image processing is a tool or a process where we can process an image and extract useful information from it. Applications like face recognition, object detection, comparing similarity index, etc can be implemented in Python using different libraries and APIs.
Today in this post we will be talking about face Comparision using Face++ API in Python.

It is an open-source API that is used in detecting whether two faces belong to the same person or not.

Modules Required:

python-facepp:  This module is required for comparing two faces in an image. Type the following command to install it.

pip install python-facepp

Module Details:

This module compares two faces on the basis of facial landmarks, emotions, beauty scores, etc and tells whether or not face belongs to the same person or not. If they match it will return “Both images belong to the same person”  else “Both images belong to different person”.

Prerequisites:

We need to have an image URL in order to perform a comparison.
Get Image URLs from different sites available by simply uploading the required image.

We also need to sign up for Face++ API in order to get the key and secret key for the app to work. Follow the steps

  • Search Face++ Python on Google and open the official link of the API.
  • From the right top corner click on the Sign in option.
  • If new User then sign up and create an account.
  • After verification copy the key and secret key in the code.

Implementation:

We will be here comparing images of Robert Downey Jr whose address has been taken from the internet.

Code:

from facepplib import FacePP

image1 = 'Address of Image 1'
image2 = 'Address of Image 2'

#Setting up the app by copying key
api_key = 'your_api_key_goes_here'
api_secret = 'api_secret_key_goes_here'
app_ = FacePP(api_key=api_key,api_secret=api_secret)

print()
print('Comparing Photographs')

#Compare function
cmp_ = app_.compare.get(image_url1=image1,image_url2=image2)

print('Photo1', '=', cmp_.image1)
print('Photo2', '=', cmp_.image2)
print("Similarity Confidence:="+str(cmp_.confidence))   #Confidence Level or similarity index of two images

# Comparing Photos
if cmp_.confidence > 70:      #setting threshold value as 70%
    print('Both photographs are of same person!')
else:
    print('Both photographs are of two different persons!')

Output:

Comparing Photographs
Photo1 = https://upload.wikimedia.org/wikipedia/commons/a/a2/Robert_Downey%2C_Jr._SDCC_2014_%28cropped%29.jpg
Photo2 = https://i.pinimg.com/originals/8e/21/29/8e2129f44804db65316ed3db92cf8552.jpg
Similarity Confidence:=85.083
Both photographs are of same person......

Leave a Reply

Your email address will not be published. Required fields are marked *