Face Comparison

In this tutorial we will learn to write a python application to perform face comparison using deepsight

Python code

Create a file face_comparison.py with the following code in it.

import requests
import argparse
import json


# define endpoints
face_api = "http://localhost:5000/inferImage"
compare_api = "http://localhost:5000/compareFaces"

# setup arguments
parser = argparse.ArgumentParser(description='Compare faces')
parser = argparse.ArgumentParser(description='Analyze faces')
parser.add_argument('--double', metavar='d', action='store', 
        type=int, nargs='?', default=0, help='specified no. of times the image should be doubled')
parser.add_argument('--expandBy', metavar='x', action='store', 
        type=float, nargs='?', default=0.0, help='specify %% by which to expand face bounding boxes')
parser.add_argument('faceA', action='store', help='path to faceA')
parser.add_argument('faceB', action='store', help='path to faceB')

args = parser.parse_args()

# compose the request url
params = {
    "returnFaceId": True,
    "returnFaceLandmarks":True,
    "expandBy": args.expandBy,
    "dblScale": args.double
}
url_params = "?"
for k,v in params.items():
    url_params += "%s=%s&"%(k,str(v).lower())
face_api += url_params


# faceA embeddings
files = {'pic':open(args.faceA,'rb')}
r = requests.post(face_api,files=files)
result = r.json()
faceA = result[0]['faceEmbeddings128'];

# faceB embeddings
files = {'pic':open(args.faceB,'rb')}
r = requests.post(face_api,files=files)
result = r.json()
faceB = result[0]['faceEmbeddings128'];

# compareFaces
req_body = {"faceA":faceA, "faceB": faceB}
r = requests.post(compare_api, data=json.dumps(req_body))
result = r.json()

if result["same"]:
    print("Same face detected!")
else:
    print("Not same!")

print(result)

Usage

# For help with usage
$ python face_comparison.py -h
usage: face_comparison.py [-h] [--double [d]] [--expandBy [x]] faceA faceB

Analyze faces

positional arguments:
  faceA           path to faceA
  faceB           path to faceB

optional arguments:
  -h, --help      show this help message and exit
  --double [d]    specified no. of times the image should be doubled
  --expandBy [x]  specify % by which to expand face bounding boxes

Running the program

Download 2 face images from the internet and put it in the current folder. Then run the program as follows

$ python face_comparison.py faceA.jpg faceB.jpg

# You can experiment with expanding bounding boxes or doubling up the image 
$ python face_comparison.py faceA.jpg faceB.jpg --expandBy 0.1 --double 1