Face verification with Azure Face API

Original article can be found here (source): Deep Learning on Medium

Face verification with Azure Face API

I have recently got a requirement to verify two images and see if both the images have the same person in it. This looks very interesting and there are few open source libraries doing this with decent accuracy. In order to get better accuracy I thought it can be dealt with CNN and started to think about implementing Siamese CNN to solve this problem. Then I saw this Face API from Azure which is doing exactly this task with better accuracy and it comes in the free tier as well.

So I gave it a try and implemented a face verification app with azure face API. To start with we need to create an Azure account and subscribe to Face API to get the endpoint and key details. Once we have the credentials, we can start building the app. Open the VSCode terminal and run the following command to create the Project.

dotnet new console -o "FaceVerification"

This will create a new Project FaceVerification. If you open the project in VSCode the structure will be like below.

If you open the Program.cs, it will have the namespace FaceVerification and class Program with default “Hello World!” code in it. We can start replacing this with our face verification code. Please note that I have followed the template given in the azure website for this implementation.

Open the FaceVerification.csproj file and add the Newtonsoft.json dependency as below to deal with JSON objects.

<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /></ItemGroup>

After adding this we need to run the restore command to resolve the newly added dependency. In the Program class, we can add subscription key and endpoint as constants as they will not change throughout. Here is how our Program class looks like now.

Now we can start writing the actual methods which help verify the images. This process has two tasks, one is to identify the face in the given image(detection task) and then comparing detected faces (verification task).

For this task, I will be using the images in my local as input by specifying it’s path inside Program class. And these images have to be converted as byteArray in order to be sent in the HTTP request body. Here is the method to convert the image into byteArray.

Now that we have all the necessary items ready, the next step is the method to call the detection API to detect the face in the given image. This async method “MakeDetectRequest” will take image path as input and return detected faceId as output.

If we see line number 77, we are assuming that the image will always contain one or no face. and If no face is detected we will return null.

Next step would be the method to call the verify API and check if the images have the same person in it. This “MakeVerifyRequest” method will receive an array of faceIds as input and return back an object with boolean “isIdentical” and “confidence” field contains the confidence score.

That’s all for the helper methods, now we can jump back to Main method and call these helper methods to get the work done. We can loop through the image path array and call detect request to populate the faceIds array, and then using this faceIds array we can call the verify the request. we need to make sure that we will call the verify request only if we identify the face in both the images to avoid exception in verify API call.

Now we are all set to test our code. Below are the images I chose to verify.

Image on the left is image_1 and the other one is image_2. Here is the output when we run the project.

» dotnet run FaceVerification 
{
"isIdentical": true,
"confidence": 0.89602
}

It did Identify that both the images having the same person in it. Now we can keep the image_1 as it is and change image_2 with different person’s image as below.

Now the isIdentical flag is false as expected.

» dotnet run FaceVerification 
{
"isIdentical": false,
"confidence": 0.18273
}

Next will be sending an image with no one in it as below.

As expected our app responded with isIdentical flag as false with 0.0 confidence score.

» dotnet run FaceVerification 
{
"isIdentical": false,
"confidence": 0.0
}

The amount of code we have written to get this done is very minimal and the accuracy is also pretty good. Hope this helps!