Showing posts with label Vision API. Show all posts
Showing posts with label Vision API. Show all posts

Thursday, April 9, 2020

Microsoft Azure Vision API - Face Detection

This post is a continuation of part 1 :  Microsoft Azure Vision API - Computer Vision

Lets play around with Face API which is a part of Microsoft Azure Vision. Similar to computer vision, we must have a Microsoft account, a valid subscription and face resource which is key and endpoint in order to access Face API services. Check out the Part 1 to see creation steps.

The purpose of Face API is to extract some parts of face, the expression to predict the age, the emotion of the image.


First, we select a face image. It can be in local or an URL





Next, we copy the secret key subscription and end point URL from Microsoft Azure Resource that we created in Part 1







Then, We build a REST API request object which consists of headers, parameters and data. They are formed as dictionary object.






What we know about image is put in 'returnFaceAttributes' attribute in parameters object. In the example, I want to know the age, gender, head pose, facial hair of the object. I also want to check if that person was wearing glasses or not or whether he/she was wearing makeup.

We just finish the request part. Lets try to send it  and hope no errors happen.


Return code is 200 means request is completely processed. Lets explore return attributes in the response



















Json object is well-printed with pprint. With data extracted in the image, it is a man whose age is 37. His emotion is predicted as neutral with 99% confidence. He doesn't wear glasses neither. Wanna guess who is he?





















It is Emmanuel Macron, French President. Well, in the picture he looks 5 years younger than he is.


Microsoft Azure Vision API - Computer Vision

Vision API is one of Cognitive Services APIs provided by Microsoft Azure in order to help AI developers to build their own dedicated Machine Learning model or whether they are going to use a pre-canned or pre-trained version. Developer can add Machine Learning features into their applications without much direct AI or data science knowledge.

Vision API includes computer vision, face recognition, content moderator, a video indexer and customer vision. In this post, We are trying to explore how to use Azure APIs to extract hidden data in the images with computer vision API and face recognition API.

Prerequisites

Create a Microsoft account - Create one here

Create a valid subscription key for computer vision and face detection. Create one for free. The free trial is available in 30 days before the update.

Create Azure Cognitive Services Resource - Create one here. The resource give us key and endpoint URL that allow us to call APIs

Now , go to  Azure portal, login with your  Microsoft account, list created resources, you are able to something similar to the below screen:










We are going to call those APIs with Python and Jupiter Notebook

Call Computer Vision Service

- Prepare an image: It can be located in your computer or in internet. I choose an Eiffel tower in Paris where I pass by every morning.





- Get the API Key and endpoint that you already created in previous step to authenticate your applications and start sending calls to the service.





- Once we got the endpoint, we build complete URI request to access computer vision service:


- Setup the request header with subscription key and URL request, parameters and data object. They are structured as dictionary.






- Lets send the request to server with the help of request - a Python package




- Notice that we got model response as data type and response code of 200. It means request is successfully sent and we receive data in the response.
- We're going to sign analysis to response.json. And now, we're going to print the type of analysis which is going to be a dictionary this returned and we're going to print the analysis itself as well







- The returned data consists of categories, tags, descriptions, captions and metadata of image. The picture is categorized a building with confident score 36%. In the description, some information is extracted such as outdoor, city, grass, sheep, view, standing, white, river, group, large, water, building,..With those information, a caption is generated as 'a large body of water with a city in the background' with confidence of 90%. Sound funny!!

- Lets use the metadata to plot the picture. We're going see what is large body in a city










View Python code and Jupiter Notebook on Github