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. ..
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".
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.
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.
Even multiple people can do it. (Although the accuracy of gender and age is doubtful)
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