
We have all seen the square frames appearing whenever we want to take a picture of video using our smartphone cameras. The concept is very easy and using the Microsoft Azure Face REST API, I will take you through the process of writing a script that does that.
Using Azure Face REST API and Python, you can detect a human face in a given image. The script works by drawing frames around the faces of a human and then superimpose the gender and the age information of the human face on the image.
First you will need to have an Azure account, you can register here for free.
Next, you need to get the Face API subscription key from the Cognitive Services.
Run Jupyter notebook on MyBinder. Then create and run the sample code as follows below.
import requests
import json
# set to your own subscription key value
subscription_key = None
assert subscription_key
# replace <My Endpoint String> with the string from your endpoint URL
face_api_url = 'https://<My Endpoint String>.com/face/v1.0/detect'
image_url = 'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.jpg'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}
response = requests.post(face_api_url, params=params,
headers=headers, json={"url": image_url})
print(json.dumps(response.json()))
Remember to make changes in the code int he following parts:
- Replace the value of {subscription_key} with your subscription key you obtained from the Cognitive Services.
- Change the value of {face_api_url} to include the endpoint URL for your Face API resource.
- Replace the value of {image_url} with the URL of a different image that you want to analyze.
Run the code and you will get a response returned in JSON as shown below.
[
{
"faceId": "e93e0db1-036e-4819-b5b6-4f39e0f73509",
"faceRectangle": {
"top": 621,
"left": 616,
"width": 195,
"height": 195
},
"faceAttributes": {
"smile": 0,
"headPose": {
"pitch": 0,
"roll": 6.8,
"yaw": 3.7
},
"gender": "male",
"age": 37,
"facialHair": {
"moustache": 0.4,
"beard": 0.4,
"sideburns": 0.1
},
"glasses": "NoGlasses",
"emotion": {
"anger": 0,
"contempt": 0,
"disgust": 0,
"fear": 0,
"happiness": 0,
"neutral": 0.999,
"sadness": 0.001,
"surprise": 0
},
"blur": {
"blurLevel": "high",
"value": 0.89
},
"exposure": {
"exposureLevel": "goodExposure",
"value": 0.51
},
"noise": {
"noiseLevel": "medium",
"value": 0.59
},
"makeup": {
"eyeMakeup": true,
"lipMakeup": false
},
"accessories": [],
"occlusion": {
"foreheadOccluded": false,
"eyeOccluded": false,
"mouthOccluded": false
},
"hair": {
"bald": 0.04,
"invisible": false,
"hairColor": [
{
"color": "black",
"confidence": 0.98
},
{
"color": "brown",
"confidence": 0.87
},
{
"color": "gray",
"confidence": 0.85
},
{
"color": "other",
"confidence": 0.25
},
{
"color": "blond",
"confidence": 0.07
},
{
"color": "red",
"confidence": 0.02
}
]
}
}
},
{
"faceId": "37c7c4bc-fda3-4d8d-94e8-b85b8deaf878",
"faceRectangle": {
"top": 693,
"left": 1503,
"width": 180,
"height": 180
},
"faceAttributes": {
"smile": 0.003,
"headPose": {
"pitch": 0,
"roll": 2,
"yaw": -2.2
},
"gender": "female",
"age": 56,
"facialHair": {
"moustache": 0,
"beard": 0,
"sideburns": 0
},
"glasses": "NoGlasses",
"emotion": {
"anger": 0,
"contempt": 0.001,
"disgust": 0,
"fear": 0,
"happiness": 0.003,
"neutral": 0.984,
"sadness": 0.011,
"surprise": 0
},
"blur": {
"blurLevel": "high",
"value": 0.83
},
"exposure": {
"exposureLevel": "goodExposure",
"value": 0.41
},
"noise": {
"noiseLevel": "high",
"value": 0.76
},
"makeup": {
"eyeMakeup": false,
"lipMakeup": false
},
"accessories": [],
"occlusion": {
"foreheadOccluded": false,
"eyeOccluded": false,
"mouthOccluded": false
},
"hair": {
"bald": 0.06,
"invisible": false,
"hairColor": [
{
"color": "black",
"confidence": 0.99
},
{
"color": "gray",
"confidence": 0.89
},
{
"color": "other",
"confidence": 0.64
},
{
"color": "brown",
"confidence": 0.34
},
{
"color": "blond",
"confidence": 0.07
},
{
"color": "red",
"confidence": 0.03
}
]
}
}
}
]
If you have any question or comment, do not hesitate to ask us.
Quote: The moon looks upon many night flowers; the night flowers see but one moon. – Jean Ingelow