Face Comparison Using Face++ and Python - GeeksforGeeks

01 Oct, 2021
Prerequisites: Python Programming Language
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 Comparison. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.
python-facepp – To install this module type the below command in the terminal.pip install python-facepp
pip install python-facepp
emoji – To install this module type the below command in the terminal.pip install emoji
pip install emoji
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”.
This app is used mainly for a face verification process like “to deliver some confidentialdocument to you, courier boy first verify your face and then deliver a courier.”
Note: A facepp API is allowed only to compare image URL links of two photographs.
Convert Image to URL using postimages.org.
In this website, choose your photograph from your local drive by click on the “choose images” button and then this website will create different URL links after processing your photograph.(See below images)
We will be using two pairs of photos for comparison.
Pair one:
            
Pair two:
         
Below is the implementation.
# Python program for face# comparison    from __future__ import print_function, unicode_literalsfrom facepplib import FacePP, exceptionsimport emoji     # define global variablesface_detection = ""faceset_initialize = ""face_search = ""face_landmarks = ""dense_facial_landmarks = ""face_attributes = ""beauty_score_and_emotion_recognition = ""   # define face comparing functiondef 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......')            # Driver Code if __name__ == '__main__':       # api details    api_key ='xQLsTmMyqp1L2MIt7M3l0h-cQiy0Dwhl'    api_secret ='TyBSGw8NBEP9Tbhv_JbQM18mIlorY6-D'       try:           # create a logo of app by using iteration,        # unicode and emoji module-------------        for i in range(1,6):                          for j in range(6,-i):                print(" " , end = " ")                              for j in range(1,i):                print('\U0001F600', end =" ")                              for j in range(i,0,-1):                print('\U0001F6A3', end= " ")                              for j in range(i,1,-2):                print('\U0001F62B', end= " ")                              print()                      print()           #print name of the app--------        print("\t\t\t"+"Photo Comparing App\n")               for i in range(1,6):                          for j in range(6,-i):                print(" " , end = " ")                              for j in range(1,i):                print(emoji.emojize(":princess:"), end =" ")                              for j in range(i,0,-1):                print('\U0001F610', end= " ")                              for j in range(i,1,-2):                print(emoji.emojize(":baby:"), end= " ")                              print()                   # 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 = 'Image 1 link'        image2 = 'Image 2 link'        face_comparing(app_, image1, image2)                  # Pair2        image1 = 'Image 1 link'        image2 = 'Image 2 link'        face_comparing(app_, image1, image2)               except exceptions.BaseFacePPError as e:        print('Error:', e)
Output:
ruhelaa48
kashishsoda
python-modules
Python-projects
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()