Earlier this month I wrote about giving your applications a more human side with Microsoft Cognitive Services, which provides a number of API’s that you can start using immediately in your applications. Today I’ll dive into the vision API’s and show you how you can leverage the Face API to detect faces in your images.

What is the Face API?

The Face API provides facial and emotion recognition and location in an image. There are 5 main areas for this API:

– Face detection
– Face verification
– Find similar faces
– Face grouping
– Face identification

Potential uses for this technology include facial login, photo tagging, and home monitoring. You can also use it for attribute detection to know age, gender, facial hair, whether the person is wearing a hat, wearing glasses, or has a beard. This API can also be used to determine if two faces belong to the same person, identify previously tagged people, find similar-looking faces in a collection.

So let’s get started with creating an Face API resource and then a small application to detect faces. In the next post I’ll extend this example to do face verification to determine if it’s the same person.

Step 1 – Requirements

To get started with Microsoft Cognitive Services and specifically the Face API you will need to have an Azure Subscription. If you don’t have one you can get a free trial subscription which includes $250 of credits to be used for any Azure services.

You will also need to have Visual Studio 2017 installed, which you can download for free.

Step 2 – Subscribe to the Face API

1. Log in to the Azure portal and click on the Create a resource link in top left corner. From here select AI + Cognitive Services and then select Face API as shown here:

image

2. Give your Face API a name, select your subscription, location, resource group and then select the F0 Free tie for pricingr:

image

3. After a few seconds your Face API subscription will be created and ready for you to start using. At this point you will need to get two items, your subscription key and your endpoint location.

The endpoint URL is shown on the Overview section and your subscription keys are located under Keys in the Resource Management section as shown here:

image

Now that we have the subscription key and endpoint let’s create our application.

Step 3 – Create new Application and reference the Face API

1. Open Visual Studio and from the File menu, click on New then Project. From here you can select any type of application but for me I’m going to create a new WPF application in C#. This code will also work with Xamarin.Forms project if you wanted to try this out for mobile.

image

2. Go to the Solution Explorer pane in Visual Studio, right click your project and then click Manage NuGet Packages.

3. Click on the Include prerelease checkbox and then search for Microsoft.Azure.CognitiveServices.Vision.Face. You might be wondering why are these API’s still in preview? Well the Cognitive Services API’s were previously called Microsoft.ProjectOxford.* and are now being moved over to Microsoft.Azure.CognitiveServices.*. Once that migration is complete they should be out of prerelease and is what you should be using from then on.

image

4.Now let’s go to the code and configure the Face API client library.

Step 4 – Configure the Face API Client Library

1. Open up your MainWindow.cs file and declare a new FaceServiceClient instance as shown here

image

2. Insert your Face API subscription key and endpoint. Replace “YOUR-SUBSCRIPTION-KEY-GOES-HERE” with your subscription key from step 2. Do the same for the second parameter which is your endpoint URL.

Step 5 – Upload images, detect faces, and show facial attributes

I wont walk through the entire code as you can do that on my GitHub repository. Instead in this step I’ll show you how I used the Face API to detect the faces, draw a square around each detected face, and finally show you the facial attributes when the mouse hovers over a detected face.

It’s worth mentioning that the maximum size of the image to upload is 4 MB.

image

As highlighted above you will take a photo you have and upload it to the Face API where it will detect an array of faces. The largest face in the image is usually what is returned first in the array. Using the DetectAsync method, you have the option to pass in an IEnumerable of FaceAttributeTypes. Just declare a list of the attributes you want back in the results like so:

image

The second highlighted code shows were we store the facial attributes returned for each face. The GetFaceDescription method is used when you mouse over a detected face and you want to show the attributes that were returned from the Face API:

image

Now let’s run our application and try detecting some faces for an image containing one or more faces. After a few seconds the API will return back with the results. As you can see we’re drawing blue squares for the makes and pink for the females, and when you hover your mouse over one of the faces I’m displaying the description of all the facial attributes returned by the API.

image

Wrap up

As you can see its very easy to add AI to your application with Microsoft Cognitive Services. Today I showed you how you can leverage the Face API for facial recognition.

Enjoy!

Resources

Sample Code

Face API Documentation

Posted by Callon Campbell

One Comment

  1. […] Close NextUsing the Face API from Microsoft Cognitive Services (part 1)–Face Detection […]

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s