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.
For this demo, I am just setting up a quick console application. Install the Microsoft Cognitive Services Face API package.
Create Face Database
Add in your API link and APIKey.
var azureFaceAPIKey = "XXXXXXXXXXXXXXXXXXXXX"; 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.
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.FaceId, face2.FaceId); Console.WriteLine(result.Confidence); Console.WriteLine(result.IsIdentical);
In this demo, and other examples I have seen, having a confidence level > 0.7 is normally considered a match.