Skip to content

Instantly share code, notes, and snippets.

@symisc
Created March 27, 2021 01:46
Show Gist options
  • Save symisc/16aea0e3fa231e084bbaf52d4f4a9b57 to your computer and use it in GitHub Desktop.
Save symisc/16aea0e3fa231e084bbaf52d4f4a9b57 to your computer and use it in GitHub Desktop.
Find a person's face in a crowd or group of people using the PixLab API - https://pixlab.io/cmd?id=facelookup
import requests
import json
# Find a person's face in a crowd or group of people. https://pixlab.io/cmd?id=facelookup for additional information.
# This is the target face we are looking for.
face = 'http://static-secure.guim.co.uk/sys-images/Guardian/Pix/pictures/2012/7/9/1341860104423/obama_face.jpg'
# The people crowd to look on
crowd = 'http://www.acclaimimages.com/_gallery/_free_images/0519-0908-1001-0556_president_barack_obama_walking_with_a_crowd_of_people_o.jpg'
req = requests.get('https://api.pixlab.io/facelookup',params={
'face': face,
'crowd': crowd,
'key':'PIXLAB_API_KEY', # Your PixLab API Key - Get yours from https://pixlab.io/dashboard
})
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
else:
found = reply['found'] # Boolean value telling whether we got the face or not
if found:
print("Face found with confidence value = "+ str(reply['confidence']))
rectangle = reply['rectangle'] # Rectangle coordinates of the target face
print("Face Coordinates: top: "+str(rectangle['top'])+" left: "+str(rectangle['left'])+" width: "+str(rectangle['width'])+" height:"+str(rectangle['height']))
else:
print("Face NOT found in the target crowd..picking up the best candidate:")
best = reply['best']
rectangle = best['rectangle'] # Rectangle coordinates of the best candidate
print ("Confidence: "+ str(best['confidence']))
print ("Best Candidate Coordinates: top: "+str(rectangle['top'])+" left: "+str(rectangle['left'])+" width: "+str(rectangle['width'])+" height:"+str(rectangle['height']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment