Face API Cognitive Service - Day Two - Exploring Face API SDK

In this article, you will learn about face API cognitive service - exploring face API SDK.

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 article on learning Azure Face API Cognitive service, we learned how to setup Azure account, create Face API on Azure portal and test the services created. In this article we’ll explore the Face API ADK and do some code. Please follow the first part before moving on to this one.
 

Tutorial Series

 
The entire series on learning Face API cognitive services is 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 applications which performs face detection, identification, grouping and finding lookalike 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 using Face API.

Face API SDK

 
C# provides a rich SDK for the Face API which you can use to write C# code and perform all the endpoint operations. Let’s go step by step to see how this could be done.
  1. Open your visual studio. I am using VS 2017 professional and create a console application name it FaceApiSdk or the name of your choice.

    Face API Cognitive Service - Exploring Face API SDK
  1. Right click on the project in Visual Studio and add a Nuget Package named ProjectOxford.Face.DotNetStandard

    Face API Cognitive Service - Exploring Face API SDK
  1. Once done, we can add the code in Program.cs class as follows.
    1. using Microsoft.ProjectOxford.Face;  
    2. using System;  
    3. using System.Threading.Tasks;  
    4.   
    5. namespace FaceApiSdk  
    6. {  
    7.     class Program  
    8.     {  
    9.         static async Task Main(string[] args)  
    10.         {  
    11.             IFaceServiceClient faceServiceClient = new FaceServiceClient("<put ypur key here>",   
    12.                 "https://centralindia.api.cognitive.microsoft.com/face/v1.0");  
    13.             var detectedFaces = await faceServiceClient.DetectAsync("https://www.codeproject.com/script/Membership/Uploads/7869570/Faces.png");  
    14.             foreach (var detectedFace in detectedFaces)  
    15.             {  
    16.                 Console.WriteLine($"{detectedFace.FaceId}");  
    17.             }  
    18.         }  
    19.     }  
    Go through the following article to enable async Main method if you work on .Net Framework lesser than 4.7: https://www.c-sharpcorner.com/article/enabling-c-sharp-7-compilation-with-visual-studio-2017/

    Face API Cognitive Service - Exploring Face API SDK

    In the above-mentioned code, we make first an instance of IFaceServiceClient and provide the key and the FaceAPI URL in the constructor as a parameter. Then we await the DetectAsync method of that instance and pass the URL of the image for which we want the faces to be detected as a parameter to the DetctAsync method. Then we write the face ID’s of all the faces returned.

    Compile the code and run it by pressing F5. In the following image that shows the output, we see that the faces of all the detected faces are returned.

    Face API Cognitive Service - Exploring Face API SDK
  1. Time to test face attributes as well. Add an array called faceAttributes mentioning what attributes you want to get returned in the response. In the DetectAsync method pass that as a parameter to the returnFaceAttributes as shown below.

    Face API Cognitive Service - Exploring Face API SDK
Code
  1. using Microsoft.ProjectOxford.Face;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace FaceApiSdk  
  9. {  
  10.     class Program  
  11.     {  
  12.         /// <summary>  
  13.         /// Go through the following article to enable async Main method if you work on .Net Framework lesser than 4.7:   
  14.         /// https://www.c-sharpcorner.com/article/enabling-c-sharp-7-compilation-with-visual-studio-2017/  
  15.         /// </summary>  
  16.         /// <param name="args"></param>  
  17.         /// <returns></returns>  
  18.         static async Task Main(string[] args)  
  19.         {  
  20.             IFaceServiceClient faceServiceClient = new FaceServiceClient("<Provide your key here>",  
  21.                 "https://centralindia.api.cognitive.microsoft.com/face/v1.0");  
  22.   
  23.             var faceAttributes = new[] { FaceAttributeType.Emotion, FaceAttributeType.Age };  
  24.   
  25.             var detectedFaces = await faceServiceClient.DetectAsync("https://www.codeproject.com/script/Membership/Uploads/7869570/Faces.png",  
  26.                 returnFaceAttributes:faceAttributes);  
  27.   
  28.             foreach (var detectedFace in detectedFaces)  
  29.             {  
  30.                 Console.WriteLine($"{detectedFace.FaceId}");  
  31.                 Console.WriteLine($"Age = {detectedFace.FaceAttributes.Age}, Happiness = {detectedFace.FaceAttributes.Emotion.Happiness}");  
  32.   
  33.             }  
  34.             Console.ReadLine();  
  35.         }  
  36.     }  
  37. }  
Output
 
We see the following output that also returns the face attributes we asked for. Happiness = 0.983 means that scale upon which the API think a person is happy. So, 0.983 means that there is a huge possibility that a person is smiling or laughing in the picture.
 
Face API Cognitive Service - Exploring Face API SDK
 

Conclusion

 
This was the continuation of the first part pf learning Azure cognitive service named Face API. In this article we explored how to leverage the capabilities of Face API SDK and perform operations via code using .Net, C# and Visual Studio.
 
Face API Cognitive Service - Exploring Face API SDK
 
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
Code
  1. SDK Code
<<Prev Part