Quickly try Microsoft's Face API in Python

At the beginning

Hello. This is a post after a long time. Recently, Google and Microsoft have provided pretty good APIs for machine learning. Until now, what I had created using OpenCV and Neural network frameworks can be easily used just by calling the API, so I think that the threshold has been lowered considerably for those who want to use it for a while. .. So, this time I would like to try Microsoft's Face API. It may seem like face recognition now, but it's a pretty good API that allows you to search and group similar faces as well as eye and nose positions, as well as age and gender. The How old do I look? that was popular a while ago should use the same API. Probably, it is better than writing the algorithm by yourself using OpenCV etc. ..

Let's make the API available

First, let's go to the Microsoft page and get the Face API. https://www.microsoft.com/cognitive-services/en-us/face-api Press Buy on Azure at the bottom of the page to register for an Azure account. After registration, select the API you want to use. Here, select Free "30,000 transactions per month".

Choose Face API.jpg

If you get the key safely, it will be displayed on your page as follows. Press the "Show" button to display the Key. You will need this Key later, so copy it and save it.

Face API.jpg

Let's write the code

Since this is a trial, I will write the code quickly in Python.

import httplib, urllib, base64
import cv2
import numpy as np
import json
import sys

headers = {
    # Request headers
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': '[Input your key]',
}

params = urllib.urlencode({
    # Request parameters
    'analyzesFaceLandmarks': 'false',
    'analyzesAge': 'true',
    'analyzesGender': 'true',
    'analyzesHeadPose': 'false',
})

def display_expression(data,img):
    font = cv2.FONT_HERSHEY_PLAIN
    font_size = 2

    data = json.loads(data)
    for face in data:
        f_rec  =  face['faceRectangle']
        width  =  f_rec['width']
        height =  f_rec['height']
        left   =  f_rec['left']
        top    =  f_rec['top']
        cv2.rectangle(img,(left,top),(left+width,top+height),(0,200,0),2)
        
        f_attr = face['attributes']
        gender = f_attr['gender']
        age = f_attr['age']
        cv2.putText(img, gender, (left, 30+top+height), font, font_size, (0, 200, 0), 2)
        cv2.putText(img, str(age), (left, 60+top+height), font, font_size, (0, 200, 0), 2)



if __name__ == '__main__':
    if len(sys.argv) < 1:
        quit()
    file_path = sys.argv[0]
    conn = httplib.HTTPSConnection('api.projectoxford.ai')
    conn.request("POST", "/face/v0/detections?%s" % params, open(file_path, 'rb'), headers)
    response = conn.getresponse()
    data = response.read()

    print(data)

    img = cv2.imread(file_path)
    display_expression(data, img)

    cv2.imshow('image',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    conn.close()

** Code explanation ** Enter the Key you obtained earlier in [Input your key].

'Ocp-Apim-Subscription-Key': '[Input your key]',

You can change the information to be acquired by disabling / enabling the parameters sent to the request.

    # Request parameters
    'analyzesFaceLandmarks': 'false',
    'analyzesAge': 'true',
    'analyzesGender': 'true',
    'analyzesHeadPose': 'false',

The display part uses OpenCV.

result

Nozomi.jpg

Ryan.jpg

Even multiple people can do it. (Although the accuracy of gender and age is doubtful) Students.jpg

Summary

We have been able to detect the position of the face fairly accurately, but it seems that there are still doubts about gender and age. (Especially for Asian faces, I have the impression that the accuracy is poor) There are many other APIs for audio and video in the Microsoft API, so I would like to use other APIs as well. However, if you really want to use it, you have to switch to a paid plan. ..

In the future, I felt that initial verification and consideration can be done much easier by calling APIs of Google and Microsoft for projects related to machine learning and image processing. We are also looking forward to the emergence of interesting Web services and IoT that use these. I recently got a Raspberry Pi3 and would like to make some interesting IoT in the meantime!

Recommended Posts

Quickly try Microsoft's Face API in Python
Quickly implement REST API in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try hitting the YouTube API in Python
Evernote API in Python
Try gRPC in Python
C API in Python 3
Try 9 slices in Python
Try using the BitFlyer Ligntning API in Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Hit Mastodon's API in Python
Try LINE Notify in Python
Try implementing Yubaba in Python 3
Face detection summary in Python
Blender Python API in Houdini (Python 3)
Try face recognition with Python
Try hitting the Twitter API quickly and easily with Python
Getting the arXiv API in Python
Hit the Sesami API in Python
Try implementing extension method in python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Let's try Fizz Buzz in Python
Try to calculate Trace in Python
Try PLC register access in Python
Create Gmail in Python without API
Hit the web API in Python
Try face recognition with python + OpenCV
Try using Python argparse's action API
Access the Twitter API in Python
Try using Leap Motion in Python
Try python
Mouse operation using Windows API in Python
Try logging in to qiita with Python
Try working with binary data in Python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Try the Microsoft Cognitive Services Face API
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
[Python] Quickly create an API with Flask
Serverless face recognition API made with Python
Try text mining your diary in Python
Try a functional programming pipe in Python
Try something like Python for-else in Ruby
Try hitting the Spotify API in Django.
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
First steps to try Google CloudVision in Python
Python 3.3 in Anaconda
Try to implement Oni Maitsuji Miserable in python
Geocoding in python
SendKeys in Python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Receive websocket of kabu station ® API in Python
Try auto to automatically price Enums in Python 3.6