Face Analysis
In this tutorial we will learn to write a python
application to perform face analysis using deepsight
Python code
Create a file face_analysis.py
with the following code in it.
import requests
import argparse
face_api = "http://localhost:5000/inferImage"
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('--id', action='store_true',help='return face id')
parser.add_argument('--attr', action='store_true',help='return gender age')
parser.add_argument('--lmk', action='store_true', help='return landmarks')
parser.add_argument('image', action='store', help='path to image')
args = parser.parse_args()
params = {
"returnFaceId": args.id,
"returnFaceAttributes": args.attr,
"returnFaceLandmarks": args.lmk,
"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
files = {'pic':open(args.image,'rb')}
r = requests.post(face_api,files=files)
result = r.json()
# The result is a list as follows
# [
# {face1},
# {face2},
# .
# {faceN},
# {diagnostics}
# ]
for i, face in enumerate(result[:-1]):
print("Face %d "%i, face)
Usage
# For help with usage
$ python face_analysis.py -h
usage: face_analysis.py [-h] [--double [d]] [--expandBy [x]] [--id] [--attr]
[--lmk]
image
Analyze faces
positional arguments:
image path to image
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
--id return face id
--attr return gender age
--lmk return landmarks
Running the program
Download an image from the internet and put it in the current folder. Then run the program as follows
# To get gender/age
$ python face_analysis.py face.jpg --attr
Face 0 {'gender': 'Female', 'faceRectangle': {'height': 96, 'top': 170, 'left': 168, 'width': 96}, 'age': '25-32'}
Face 1 {'gender': 'Female', 'faceRectangle': {'height': 95, 'top': 113, 'left': 379, 'width': 96}, 'age': '25-32'}
Face 2 {'gender': 'Male', 'faceRectangle': {'height': 96, 'top': 141, 'left': 62, 'width': 96}, 'age': '25-32'}
Face 3 {'gender': 'Male', 'faceRectangle': {'height': 80, 'top': 148, 'left': 252, 'width': 80}, 'age': '25-32'}
Face 4 {'gender': 'Male', 'faceRectangle': {'height': 80, 'top': 28, 'left': 220, 'width': 80}, 'age': '25-32'}
Face 5 {'gender': 'Male', 'faceRectangle': {'height': 80, 'top': 20, 'left': 316, 'width': 80}, 'age': '25-32'}
# To get everything after expanding bounding boxes by 10% and doubling up the image
$ python face_analysis.py face.jpg --attr --id --lmk --expandBy 0.1 --double 1