Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created October 27, 2017 16:42
Show Gist options
  • Save PandaWhoCodes/6d5f0e3a554d058b1e63851f4e83b800 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/6d5f0e3a554d058b1e63851f4e83b800 to your computer and use it in GitHub Desktop.
Uses local image for Microsoft Azure face API
def getage():
subscription_key = '2c2c93c599a841e5ac01a2e0635fe3be'
uri_base = 'https://westcentralus.api.cognitive.microsoft.com'
# Request headers.
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
# Request parameters.
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}
filename = 'bacha.jpg'
f = open(filename, "rb")
body = f.read()
f.close()
# Body. The URL of a JPEG image to analyze.
body = body
try:
# Execute the REST API call and get the response.
response = requests.request('POST', uri_base + '/face/v1.0/detect', data=body, headers=headers,
params=params)
parsed = json.loads(response.text)
print(parsed)
age = parsed[0]['faceAttributes']["age"]
return(age)
except Exception as e:
print('Error:')
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment