Face API Cognitive Service - Day Three - Face Identification Using Face API

Preface

 
This article series is a complete end to end tutorial that will explain the concept of face recognition and face detection using modern AI-based Azure cognitive service i.e. Azure’s Face API service.
 

Introduction

 
In the last two articles on learning Azure Face API Cognitive service, we learned how to set up Azure account, how to create Face API on Azure portal and test the services created. We learned the use of Face API SDK and how we can perform the needed operations from the code itself. In this article, we’ll explore how the face identification can be done and what all services are involved to achieve this. We’ll create models and train them and see the result. So, there is a lot of machine learning involved.
 

Tutorial Series

 
The entire series on learning Face API cognitive services are divided into four parts. The first part focuses on Azure Functions, Serverless computing and creating and testing Face API on Azure Portal.
 
The second part explains the use of Face API SDK. The third part will be focused on face identification, where person groups will be created, and identification of faces would be done via training the models i.e. via machine learning techniques. The fourth part is the most interesting part that gives a walkthrough of Face classification application which performs face detection, identification, grouping and finding look-alike faces. Following is the four-part series.
  1. Face API Cognitive Service Day 1: Face API on Azure Portal
  2. Face API Cognitive Service Day 2: Exploring Face API SDK
  3. Face API Cognitive Service Day 3: Face Identification using Face API.
  4. Face API Cognitive Service Day 4: Face classification app using Face API.

Face Identification

 
In the last two articles, we saw face detection. In this section, we’ll focus on face identification and check the capabilities of Face API to identify the faces of the people. In this section, we’ll see what all services we can write to perform face identification. We’ll test the services using Postman and in the next section, we’ll walk through a live face identification application.
 
Face API Cognitive Service - Face Identification Using Face API
 
Before we look at the individual operations in detail, it's important for us to first understand the high-level structure and the workflow of how everything fits together. The first thing we need to do is to create a person group. The Face API gives us all the CRUD operations. It gives us all these operations for managing person groups. Once our person group is created, we can add n number of persons to that person group. After we add a person to a person group, then we add n number of faces to each person. Once we've added as many persons and faces to our person group that we want, we then invoke the Face API method to train our person group. Once our person group is trained, we're ready to do face identification. Face identification always starts with face detection. You've already seen face detection. The key result is that we get a unique faceId for each face in face detection. Once we invoke detection, we can finally invoke face identification using the results of our face detection calls along with the person group we want to use for face identification. Let’s do that step by step.
 

Creating A Person Group

 
I have used some settings in the Postman to save my base URL i.e. https://centralindia.api.cognitive.microsoft.com/face/v1.0 and the secret key. So I’ll use the keyword {{base-url}} and {{face-api-key}} for URL and key respectively. You can use the base URL and the key you have in place of my keywords.
 
Make a pull request in the Postman for actions persongroups/1 appended to base URL as shown below. In the body, provide the raw JSON telling the name of the person group i.e. in my case “family” and the userData as a description of the person group. The JSON should be like following.
  1. {  
  2.     "name""family",  
  3.     "userData""family person group"  
  4. }  
Face API Cognitive Service - Face Identification Using Face API
 
In the headers section, provide two keys i.e. Ocp-Apim-Subscription-Key and Content-Type and their values as your API key and application/json respectively. Hit the send button. Once you hit the send button, you get the response as 200 i.e. OK that means the person group is created.
 
Face API Cognitive Service - Face Identification Using Face API
 

Get A Person Group

 
We can check the crated person group by making a get call to the API at here.
 
And you get the created person group with a person group id, name, and user data. In the next step, we’ll add a few persons to the person group.
 
Face API Cognitive Service - Face Identification Using Face API
 

Create Person

 
In the person groups created, append the URL with “persons” and in the body section, provide the name and user data of the function as below.
  1. {  
  2.     "name" : "Akhil Mittal",  
  3.     "userData" : "Author"  
  4. }  
Keep the headers the same; i.e. providing key and content type and hit send button. Make sure the HTTP verb is Post. We get the response with a created person having a person id under person group 1.
 
Face API Cognitive Service - Face Identification Using Face API 
 

Get Person

 
You can get the person with the id of the person that we received in the last step. Make a Get call to the API as https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/1/persons<personid>.
 
We get the JSON response with personId, name and userData and an additional field that says an array of persistedFaceIds. We have not created any face for this person so the field remains empty.
 
Face API Cognitive Service - Face Identification Using Face API 
 

Get all Persons

 
If you wish to get the list of all the persons you created for that particular person group, just name a Get call to the API at https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/1/persons/.
 
This will get you all the persons created in the JSON response under the person group having Id as 1.
 
Face API Cognitive Service - Face Identification Using Face API 
 

Create a Face for the Person

 
Time to create the face of the person; i.e. the persisted face for the created person. Since you have the person id of the created person for which we want to create a persisted face. We’ll make a call to the API as https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/1/persons/<personId>/persistedFaces.
 
In the body tag, provide a raw JSON that contains the URL of the person's face i.e. the image of the person whose face has to be created.
  1. {  
  2.    "url""https://www.codeproject.com/script/Membership/Uploads/7869570/Akhil.png"  
  3. }  
Following is the image of the person for which the URL is given,
 
Face API Cognitive Service - Face Identification Using Face API
 
Now, make a POST request with these configurations and make sure your header data has key and content-type defined. Hit Send button. Once we get the response, we see that the response contains the JSON that has “persistedFaceId” that means the person's face is created and a corresponding Id is returned.
 
Face API Cognitive Service - Face Identification Using Face API
 
Now if you again make the get request for that person, you’ll also get the persisted face id attribute having value for that person’s face as shown below.
 
Face API Cognitive Service - Face Identification Using Face API
 

Train the Person Group

 
It’s time to train the person group. Since we got the person group, person and his face. We’ll train our model and then perform face identification for that person. Just append train to persongroups/<id> URL and make a POST request as https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/1/persons/train.
 
Once done, we’ll get the response as 200 i.e. Accepted.
 
Face API Cognitive Service - Face Identification Using Face API

 
Check Training Status

 
You can check the training status by making a Get request to the URL
 
https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/1/persons/training.
 
We see here in the following image that we get the training status as succeeded. That means the person group is trained now to perform face identification operations.
 
Face API Cognitive Service - Face Identification Using Face API
 
Let’s do that in the next step.
 

Identification

 
I have uploaded the following image on the public URL that has my friend and me.
 
Face API Cognitive Service - Face Identification Using Face API
  1. Make a detect call to the uploaded image as we did initially and when we get the response, we wee the JSON for two faces returned one for my friend and another for mine.

    Face API Cognitive Service - Face Identification Using Face API
  1. Now, since our person group is trained we can send an identify request to the base-URL. In the body, provide the JSON with person group id and the faces we got in the detect call in last operations,
    1. {  
    2.     "personGroupId" : "1",  
    3.     "faceIds" : [  
    4.                   "5a45a46a-6327-499e-8442-0fb404f4e426",  
    5.                   "c88546a1-543c-4497-ab9c-df4e055820cd"  
    6.          ]  
    7. }  
Once we hit the send button and make that POST request. We get the response for both the provided faces but only one face has candidates that are telling the personId and the confidence level that the identified person is there in the person group or not. It seems to be working. Here we made a request for identification for two persons, but we got the response for only one that is my face because my face is already there in the person's list of the created person group and our model is trained to process that. The response here is returned with the confidence level value which means that this is the level of confidence the API has that the identified person is there in the person group.
 
Face API Cognitive Service - Face Identification Using Face API
 
In the next section, we’ll go through a running application having all these operations and the visual view of the operations.
 

Conclusion

 
In this article we explored the capabilities of Azure Face API; i.e. one of Azure’s cognitive service. The API is quite intelligent and strong to leverage the AI and machine learning capabilities and perform the actions. We saw in detail how to create an Azure account, how to create a Face API and get it up and running. We saw how CRUD operations could be performed over the Face API for person groups, persons and faces. We performed face identification and model training as well.
 
Face API Cognitive Service - Face Identification Using Face API
 
References
  1. https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Vision.Face/
  2. https://azure.microsoft.com/en-in/services/cognitive-services/face/
  3. https://centralindia.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236


Similar Articles