Skip to content

Instantly share code, notes, and snippets.

@symisc
Created March 30, 2021 22:06
Show Gist options
  • Save symisc/d3575d9f59f48e1e1887a95fe7c36f0d to your computer and use it in GitHub Desktop.
Save symisc/d3575d9f59f48e1e1887a95fe7c36f0d to your computer and use it in GitHub Desktop.
Detect all human faces present in a given image and try to guess their age, gender and emotion state using the PixLab API - https://pixlab.io/cmd?id=facemotion
import requests
import json
# Detect all human faces present in a given image and try to guess their age, gender and emotion state via their facial shapes
# Target image: Feel free to change to whatever image holding as many human faces as you want
img = 'http://www.scienceforums.com/uploads/1282315190/gallery_1625_35_9165.jpg'
req = requests.get('http://api.pixlab.io/facemotion',params={
'img': img,
'key':'PIXLAB_API_KEY', # Your PixLab API Key - Get your from https://pixlab.io/dashboard
})
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
exit();
total = len(reply['faces']) # Total detected faces
print(str(total)+" faces were detected")
# Extract each face now
for face in reply['faces']:
cord = face['rectangle']
print ('Face coordinate: width: ' + str(cord['width']) + ' height: ' + str(cord['height']) + ' x: ' + str(cord['left']) +' y: ' + str(cord['top']))
# Guess emotion
for emotion in face['emotion']:
if emotion['score'] > 0.5:
print ("Emotion - "+emotion['state']+': '+str(emotion['score']))
# Grab the age and gender
print ("Age ~: " + str(face['age']))
print ("Gender: " + str(face['gender']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment