[Python] Use the Face API of Microsoft Cognitive Services

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. key

Hit the API from Python

Version is 2.7. I think it works even with 3. Install requests with pip in advance.

$ pip install requests

Image file version

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

Image URL version

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

Output result

[
    {
        "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

[Python] Use the Face API of Microsoft Cognitive Services
Try the Microsoft Cognitive Services Face API
Let's use the Python version of the Confluence API module.
Use the Flickr API from Python
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
the zen of Python
[Python] Get the text of the law from the e-GOV Law API
Let's use the API of the official statistics counter (e-Stat)
Let's use the open data of "Mamebus" in Python
Use Trello API with python
Towards the retirement of Python2
Use Twitter API with Python
About the ease of Python
Call the API with python3.
Use subsonic API with python3
About the features of Python
Use e-Stat API from Python
The Power of Pandas: Python
Survey on the use of machine learning in real services
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
The story of Python and the story of NaN
Getting the arXiv API in Python
Wrap (part of) the AtCoder Library in Cython for use in Python
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Hit the Sesami API in Python
[Python] Hit the Google Translation API
[September 2020 version] Explains the procedure to use Gmail API with Python
Proper use of Python visualization packages
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Sample to use after OAuth authentication of BOX API with Python
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
Change the Python version of Homebrew
[Python Queue] Convenient use of Deque
Hit the Etherpad-lite API with Python
Use kabu Station® API from Python
Hit the web API in Python
[Python] Understanding the potential_field_planning of Python Robotics
How to use OpenPose's Python API
Review of the basics of Python (FizzBuzz)
Sakura Use Python on the Internet
Access the Twitter API in Python
[Python] How to use Typetalk API
Python beginners hit the unofficial API of Google Play Music to play music
About the basics list of Python basics
Use Google Analytics API from Python
Learn the basics of Python â‘  Beginners
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
I tried to get the authentication code of Qiita API with Python.
About Python code for simple moving average assuming the use of Numba
Hit a method of a class instance with the Python Bottle Web API
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
[Python] Summary of how to use pandas
Change the length of Python csv strings
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module