Retrieving Face Attributes Using Cognitive Service Face API In UWP With Azure, XAML And C#

Before reading this article, please go through the following articles.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. Getting Started With Microsoft Azure Cognitive Services Face APIs
  3. Cognitive Service Face API, Using UWP With Azure, XAML, And C#

Microsoft Cognitive Services lets you build apps with powerful algorithms, using just a few lines of code. They work across devices and platforms, such as iOS, Android, and Windows, keep improving, and are easy to set up.

In this article, we will see how to detect faces in an image and get back face rectangles for where the faces are, along with face attributes which contain machine learning-based predictions of facial features. After detecting faces, you can take the face rectangle and pass it to the Emotion API to speed up the processing. The face attribute features available are: Age, Gender, Pose, Wearing Glasses, Smile, and Facial Hair, along with 27 landmarks for each face in the image.

Reading this article, you can learn how to retrieve Face Attributes (like Face ID, Gender, Age, Wearing Glasses) using Cognitive Service Face API in Universal Windows Apps development with Azure, XAML and Visual C#. 

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software available online).
  3. Face API Key (using Azure, Getting Started With Microsoft Azure Cognitive Services Face APIs)

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2015 and go to Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank App -> Give it a suitable name (UWPCogFaceData) -> OK.

Visual studio 2015

After choosing the Target and Minimum platform versions that your app will support, the Project creates App.xaml and MainPage.xaml.

Visual studio 2015

Add TextBlock control and change the name and text property for title.

Visual studio 2015

Step 2

Open (double click) the MainPage.xaml file in Solution Explorer and add the following references in the project.

  1. Microsoft.ProjectOxford.Face
  2. Newtonsoft.Json

For adding Microsoft.ProjectOxford.Face Reference, right click on your project name (UWPCogFace) and select "Manage NuGet Packages".

Visual studio 2015

Choose Browse and search Microsoft.ProjectOxford.Face. Select the package and install it. The Reference is now added to your project.

Visual studio 2015

For adding Newtonsoft.Json Reference, choose Browse and search Newtonsoft.Json. Select the package and install it.

Visual studio 2015

Reference is now added to your project.

Visual studio 2015

Step 3

Add Gender icon images in Assets folder.

Visual studio 2015

Add Image control and set the name for face image.

Visual studio 2015

Add a Text Block and set the name and text property for file attribute title.

Visual studio 2015

Add Image control and set the name for Gender display.

Visual studio 2015

Add 3 Text Blocks and set the name and text property for age, glasses, and Face ID.

Visual studio 2015

Visual studio 2015

Note Automatically, the following code will be generated in XAML code view, while we are done in the design view.

  1. <Page x:Class="UWPCogFaceData.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogFaceData" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="87,61,0,0" TextWrapping="Wrap" Text="Retriving Face Attributes using Cogintive Service Face API in UWP" VerticalAlignment="Top" Width="522" FontWeight="Bold" />  
  4.         <Image x:Name="imgFace" HorizontalAlignment="Left" Height="184" Margin="87,116,0,0" VerticalAlignment="Top" Width="174" />  
  5.         <TextBlock x:Name="tblFAttrib" HorizontalAlignment="Left" Margin="331,113,0,0" TextWrapping="Wrap" Text="Face Attributes" VerticalAlignment="Top" FontWeight="Bold" />  
  6.         <Image x:Name="imgGender" HorizontalAlignment="Left" Height="40" Margin="331,138,0,0" VerticalAlignment="Top" Width="28" />  
  7.         <TextBlock x:Name="tblAge" HorizontalAlignment="Left" Height="40" Margin="364,138,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="73" FontSize="12" />  
  8.         <TextBlock x:Name="tblWearGlass" HorizontalAlignment="Left" Margin="359,183,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="78" Height="27" />  
  9.         <TextBlock x:Name="tblFaceID" HorizontalAlignment="Left" Height="34" Margin="266,210,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="222" FontSize="10" />  
  10.     </Grid>  
  11. </Page>  
Step 4

Add the following namespaces in Mainpage.xaml.cs for face upload, detect, and draw rectangle.
  1. using Microsoft.ProjectOxford.Face;  
  2. using Windows.UI.Xaml.Media.Imaging;  
Step 5

Add the FaceServiceClient key use Azure service and generate it (For More Information, please refer to this article Getting Started With Microsoft Azure Cognitive Services Face APIs).

Add the following code with Azure generated key for FaceServiceClient.

 private readonly IFaceServiceClient fsClient = new FaceServiceClient("<Your Key>");


Step 6

Add the code for retrieving the face attributes.
  1. public MainPage() {  
  2.     this.InitializeComponent();  
  3.     string FaceimgUrl = "http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/ea4f1720160902050509.jpg";  
  4.     imgFace.Source = new BitmapImage(new Uri("http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/ea4f1720160902050509.jpg", UriKind.Absolute));  
  5.     retriveFaceAttributes(FaceimgUrl);  
  6. }  
  7. async void retriveFaceAttributes(string imageUrl) {  
  8.     try {  
  9.         var requiredFaceAttributes = new FaceAttributeType[] {  
  10.             FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Glasses  
  11.         };  
  12.         var faces = await faceServiceClient.DetectAsync(imageUrl, returnFaceLandmarks: true, returnFaceAttributes: requiredFaceAttributes);  
  13.         foreach(var face in faces) {  
  14.             var id = face.FaceId;  
  15.             var attributes = face.FaceAttributes;  
  16.             var age = attributes.Age;  
  17.             var gender = attributes.Gender;  
  18.             var glasses = attributes.Glasses;  
  19.             int roundedAge = (int) Math.Round(age);  
  20.             if (!string.IsNullOrEmpty(imageUrl)) {  
  21.                 tblAge.Text = roundedAge.ToString();  
  22.                 tblFaceID.Text = "Face ID: " + id.ToString();  
  23.                 tblWearGlass.Text = glasses.ToString();  
  24.                 if (string.Compare(gender, "male"true) == 0) {  
  25.                     imgGender.Source = new BitmapImage(new Uri("ms-appx:///Assets/male.png"));  
  26.                 } else if (string.Compare(gender, "female"true) == 0) {  
  27.                     imgGender.Source = new BitmapImage(new Uri("ms-appx:///Assets/female.png"));  
  28.                 }  
  29.             }  
  30.         }  
  31.     } catch (Exception ex) {  
  32.         System.Diagnostics.Debug.WriteLine(ex.Message);  
  33.     }  
  34. }  
http://www.c-sharpcorner.com/article/getting-started-with-microsoft-azure-cognitive-services-face-apis/

Step 7

Deploy your app in a Local Machine. The output of the UWPCogFaceData app is shown below.

http://www.c-sharpcorner.com/article/getting-started-with-microsoft-azure-cognitive-services-face-apis/

Summary

Now, you have successfully retrieved the face attributes using Cognitive Service Face API, with Azure, XAML, and C# in UWP environment. 


Similar Articles