Face Recognition with Azure FaceAPI

Azure Cognitive services offers a way to store a database of known faces, then be able to detect, with a certain confidence level, of whether different faces match any face it has in its database.

Create Azure Service

First, we need to create the cognitive services account, via the Azure Portal.

Currently they support a Free version, which is great for developing and testing.

Get the API, and copy your keys as you will need them later.

Project Setup

For this demo, I am just setting up a quick console application. Install the Microsoft Cognitive Services Face API package.

Install-Package Microsoft.ProjectOxford.Face

Create Face Database

Add in your API link and APIKey.

 var url = "https://westus.api.cognitive.microsoft.com/face/v1.0";

Now we create a client and we need to detect faces.

var stream = new FileStream(@"D:\sample\images\dj1.jpg", FileMode.Open, FileAccess.Read);

var face1 = await client.DetectAsync(stream);

stream = new FileStream(@"D:\sample\images\dj2.jpg", FileMode.Open, FileAccess.Read);

var face2 = await client.DetectAsync(stream);

I uploaded 2 pictures of Dwayne Johnson.

Note: To use the VerifyAsync command coming up, we can’t add to a Person or FaceList. PersistedFaceId’s do not work, we can only use the FaceId’s from the DetectAsync method.

Face Recognition

Now, lets try and verify if the faces are the same or similar. There can be multiple faces in each photo, but since I know there is only one, I can use the first item in the array.

var result = await client.VerifyAsync(face1[0].FaceId, face2[0].FaceId);


In this demo, and other examples I have seen, having a confidence level > 0.7 is normally considered a match.

Learn More

There is a demo of this code at my GitHub repo AzureFaceAPI. You can also store Face’s and Person’s via this API. For more information you can look at the Face API Documentation.