• For any query, contact us at
  • +91-9872993883
  • +91-8283824812
  • info@ris-ai.com

Face Comparision using python-facepp

Hello everyone here we are doing face comparison where we will compare to image of a person using url of the given image.here we don't have to download image we need just the url.so lets begin with the code

Python is a high-level general-purpose language. It is used for multiple purposes like AI, Web Development, Web Scraping, etc. One such use of Python can be Face Comparision. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.

Module Required

python-facepp – To install this module type the below command in the terminal.

pip install python-facepp

This app compares two photographs of the same person or two different persons against his/her face features like face landmarks, beauty score, face emotion, etc. If both photographs are matching with each other, the app result is “Both photographs are of same person ” otherwise app result is “Both photographs are of two different persons”.

Now the question arises here is that what is Face++?

so Here it is, Python-FacePP is a library for communicating with a Face++ facial recognition service. Face++ exposes some of it’s data via Web API for which Python-FacePP provides a simple but powerful Pythonic API inspired by a well-known Django ORM

How it works in python

So firstly we will show the images we are using by using pillow library.Here we are trying to create a PIL image object from a file pulled from a URL.

In [30]:
import requests
import io
from PIL import Image

image1 = 'https://wallpapercave.com/wp/wp4000980.jpg'
image2 = 'https://upload.wikimedia.org/wikipedia/commons/2/2d/Atif_Aslam_at_Badlapur_%28cropped%29.jpg'
response = requests.get(image1)
response1 = requests.get(image2)
image_bytes = io.BytesIO(response.content)
image_bytes1 = io.BytesIO(response1.content)
img = Image.open(image_bytes)
img1 = Image.open(image_bytes1)
img.show()
img1.show()

secondly, we are importing various libraries for face comparison also declaring some variables.

In [31]:
# Python program for face
# comparision
from __future__ import print_function, unicode_literals
from facepplib import FacePP, exceptions
import emoji
face_comparing_localphoto=""
face_comparing_websitephoto=""
face_detection = ""
faceset_initialize = ""
face_search = ""
face_landmarks = ""
dense_facial_landmarks = ""
face_attributes = ""
beauty_score_and_emotion_recognition = ""
In [32]:
# define face comparing function
def face_comparing(app, Image1, Image2):
    print()
    print('-'*30)
    print('Comparing Photographs......')
    print('-'*30)
    cmp_ = app.compare.get(image_url1 = Image1,
                           image_url2 = Image2)
    print('Photo1', '=', cmp_.image1)
    print('Photo2', '=', cmp_.image2)
    # Comparing Photos
    if cmp_.confidence > 70:
        print('Both photographs are of same person......')
    else:
        print('Both photographs are of two different persons......')
In [29]:
# Driver Code 
if __name__ == '__main__':
   
    # api details
    api_key ='xQLsTmMyqp1L2MIt7M3l0h-cQiy0Dwhl'
    api_secret ='TyBSGw8NBEP9Tbhv_JbQM18mIlorY6-D'
   
    try:
        # call api
        app_ = FacePP(api_key = api_key,
                      api_secret = api_secret)
        funcs = [
            face_detection,
            face_comparing_localphoto,
            face_comparing_websitephoto,
            faceset_initialize,
            face_search,
            face_landmarks,
            dense_facial_landmarks,
            face_attributes,
            beauty_score_and_emotion_recognition
        ]
          
        # Pair 1
        image1 = 'https://wallpapercave.com/wp/wp4000980.jpg'
        image2 = 'https://upload.wikimedia.org/wikipedia/commons/2/2d/Atif_Aslam_at_Badlapur_%28cropped%29.jpg'
        face_comparing(app_, image1, image2)
    except exceptions.BaseFacePPError as e:
        print('Error:', e)

Comparing Photographs......

Face Comparison
Face Comparison

Both photographs are of same person......

Resources You Will Ever Need