Skip to content
Related Articles

Related Articles

Improve Article

Face Comparision Using Face++ and Python

  • Last Updated : 16 Jul, 2020

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 Comparision. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.

Modules Required

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

App Details

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 confidential
document 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)

image-to-url
image-to-url
image-to-url
image-to-url

We will be using two pairs of photos for comparision.

Pair one:

python-face-comparision
python-face-comparision

 
 
 
 
 
 
 
 
 
 
 
 

Pair two:

python-face-comparision
python-face-comparision

 
 
 
 
 
 
 
 
 

Below is the implementation.




# Python program for face
# comparision
  
  
from __future__ import print_function, unicode_literals
from facepplib import FacePP, exceptions
import emoji
  
   
# define global variables
face_detection = ""
faceset_initialize = ""
face_search = ""
face_landmarks = ""
dense_facial_landmarks = ""
face_attributes = ""
beauty_score_and_emotion_recognition = ""
   
# 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......')
  
          
# 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:

python-face-comparision

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :