Microsoft Cognitive Services - Face API https://www.microsoft.com/cognitive-services/en-us/face-api You can detect the position of the face contained in the image, the gender of the person, the age, etc.
Subscription Key Get a Face Preview (free) Subscription Key from Cognitive Services. There are Key1 and Key2, but only Key1 is required.
Version is 2.7. I think it works even with 3. Install requests with pip in advance.
$ pip install requests
detect.py
import sys
import requests
url = 'https://api.projectoxford.ai/face/v1.0/detect'
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '[your subscription key]',
}
params = {
'returnFaceId': 'true', # The default value is true.
'returnFaceLandmarks': 'false', # The default value is false.
'returnFaceAttributes': 'age,gender', # age, gender, headPose, smile, facialHair, and glasses.
}
if __name__ == '__main__':
argv = sys.argv
if len(argv) == 1:
print 'Usage: # python %s [filename]' % argv[0]
quit()
r = requests.post(url ,headers = headers,params = params,data = open(argv[1],'rb'))
print(r.text)
If you want to detect the face of image.png, execute as follows.
$ python detect.py image.png
When specifying the image by URL, do as follows.
detect.py
import sys
import json
import requests
url = 'https://api.projectoxford.ai/face/v1.0/detect'
image_url = 'http://example.com/image.png'
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '[your subscription key]',
}
params = {
'returnFaceId': 'true', # The default value is true.
'returnFaceLandmarks': 'false', # The default value is false.
'returnFaceAttributes': 'age, gender', # age, gender, headPose, smile, facialHair, and glasses.
}
payload = {
'url': image_url,
}
if __name__ == '__main__':
r = requests.post(url ,headers = headers, params = params, data = json.dumps(payload))
print(r.text)
If you want to detect the face of image_url, execute as follows.
$ python detect.py
[
{
"faceId": "xxxxxxxxxxxxxxxxxxxxxxxx",
"faceRectangle": {
"top": 119,
"left": 177,
"width": 144,
"height": 144
},
"faceAttributes": {
"gender": "female",
"age": 17.9
}
}
]
You can see that the result is returned in Json. By changing params, you can also get the position information of the facial part.
Recommended Posts