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

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

  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

Microsoft Cognitive Services (formerly Project Oxford) are a set of APIs, SDKs, and services, available for developers to make their applications more intelligent, engaging, and discoverable. Microsoft Cognitive Services expand on Microsoft’s evolving portfolio of "Machine Learning APIs' and enable the developers to easily add intelligent features – such as, emotion and video detection; facial, speech, and vision recognition; and speech and language understanding – into their applications. Our vision is for more personal computing experiences and enhanced productivity aided by systems that increasingly can see, hear, speak, understand and even begin to reason.

Microsoft Cognitive Services let 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.

Reading this article, you can learn how to use 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 the step by step process of app development.

Step 1

Open Visual Studio 2015. Go to Start -> New Project-> Select Universal (under Visual C# -> Windows ) -> Blank App -> Give a name for your app (UWPCogFace) -> click OK.



After choosing the target and minimum platform version that your UWP application will support, the project creates App.xaml and MainPage.xaml.

MainPage.xaml

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

MainPage.xaml

Step 2

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

  1. Microsoft.ProjectOxford.Face
  2. Newtonsoft.Json
  3. Win2D.uwp

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

MainPage.xaml

Choose "Browse" and search "Microsoft.ProjectOxford.Face". Select the package and install it.

MainPage.xaml

Accept the Licence.

MainPage.xaml

Reference added to your project.

MainPage.xaml

For adding Newtonsoft.Json Reference,

MainPage.xaml

Choose "Browse" and search "Newtonsoft.Json". Select the package and install it.

Reference added to your project.

MainPage.xaml

For adding Win2D.uwp Reference, used for drawing Rectangle in face, choose "Browse" and search "Win2D.uwp". Select the package and install it.

MainPage.xaml

Reference added to your project.

MainPage.xaml

Step 3

Adding images to the "Assets" folder for face.



Step 4

Add Image control and set the name and source properties.

image

Step 5

Add the namespace and canvas control, using Draw event method,for drawing the rectangle in face.

xmlns:ccn="using:Microsoft.Graphics.Canvas.UI.Xaml"

image

Note

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

  1. <Page x:Class="UWPCogFace.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogFace" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ccn="using:Microsoft.Graphics.Canvas.UI.Xaml" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="192,52,0,0" TextWrapping="Wrap" Text="UWP Coginitive service Face API demo" VerticalAlignment="Top" FontWeight="Bold" />  
  4.         <Image x:Name="imgFace" Source="Assets/01.jpg" Margin="129,107,135,41" />  
  5.         <ccn:CanvasControl x:Name="CanFaceRect" Draw="CanFaceRect_Draw" />  
  6.     </Grid>  
  7. </Page>  
Step 6

Add the following namespaces in Mainpage.xaml.cs for face upload, detect and draw the rectangle.
  1. using Microsoft.Graphics.Canvas.UI.Xaml;  
  2. using Microsoft.ProjectOxford.Face;  
  3. using Microsoft.ProjectOxford.Face.Contract;  
  4. using Windows.Storage;  
  5. using Windows.UI;  
Step 7

Add the FaceServiceClient key, use Azure service and Generate it (For more information, please refer the article Getting Started With Microsoft Azure Cognitive Services Face APIs).

Add the following code with Azure Generated key for FaceServiceClient.
  1. private readonly IFaceServiceClient fsClient = new FaceServiceClient("<Your Key>");  
  2. Add the code  
  3. for faceupload, detect and draw rectangle  
  4. private readonly IFaceServiceClient fsClient = new FaceServiceClient("(" < Your Key > "");  
  5. FaceRectangle[] fRectangles;  
  6. public MainPage() {  
  7.     this.InitializeComponent();  
  8.     UploadAndDetectFaces("ms-appx:///Assets/02.png");  
  9. }  
  10. async void UploadAndDetectFaces(string imageFilePath) {  
  11.     try {  
  12.         StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;  
  13.         StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets");  
  14.         var storageFile = await assets.GetFileAsync("01.JPG");  
  15.         var randomAccessStream = await storageFile.OpenReadAsync();  
  16.         using(Stream stream = randomAccessStream.AsStreamForRead()) {  
  17.             //this is the fragment where face is recognized:  
  18.             var faces = await fsClient.DetectAsync(stream);  
  19.             var faceRects = faces.Select(face => face.FaceRectangle);  
  20.             fRectangles = faceRects.ToArray();  
  21.             CanFaceRect.Invalidate();  
  22.         }  
  23.     } catch (Exception ex) {  
  24.         System.Diagnostics.Debug.WriteLine(ex.Message);  
  25.     }  
  26. }  
  27. private void CanFaceRect_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) {  
  28.     if (fRectangles.Length > 0) {  
  29.         foreach(var faceRect in fRectangles) {  
  30.             args.DrawingSession.DrawRectangle(faceRect.Left, faceRect.Top, faceRect.Width, faceRect.Height, Colors.Red);  
  31.         }  
  32.     }  
  33. }  
image

Step 8

Deploy your app in Local Machine. The output of the UWPCogFace App is shown below.

UWPCogFace

Summary

Now, you have successfully tested Cognitive Service Face API using Azure, XAML, and C# with UWP environment.

Up Next
    Ebook Download
    View all
    Learn
    View all