Get Image Attributes using Cognitive Services face API in WPF

To know about Cognitive Services Face API, you must read this article first.

Getting Started

  • Start Visual Studio 2015
  • From the file menu, select New, then Project
  • Select Windows template
  • Select .NET Framework 4.6.1
  • Select WPF Application
  • Enter Name
  • Browse save location
  • Click OK

As I discussed in my previous article, how to add Newston.json and ProjectOxford.Face and you need a subscription of Cognitive Service Face preview and copy the subscription key. Once you are done with all this then let’s move to the .xaml page.

  1. <Window x:Class="CognitiveServicesFaceAPI_WPFSample.MainWindow1"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:CognitiveServicesFaceAPI_WPFSample"  
  7. mc:Ignorable="d"  
  8. Title="MainWindow1" Height="550" Width="725">  
  9. <Grid x:Name="BackPanel" Margin="10">  
  10. <Grid.ColumnDefinitions>  
  11. <ColumnDefinition Width="2*" />  
  12. <ColumnDefinition Width="1*" />  
  13. </Grid.ColumnDefinitions>  
  14. <Grid.RowDefinitions>  
  15. <RowDefinition Height="2*" />  
  16. </Grid.RowDefinitions>  
  17. <Image x:Name="imgPhoto" Grid.Row="1" Grid.Column="0" Stretch="Uniform" Margin="0,0,0,30"/>  
  18. <Button x:Name="btnBrowse" Height="20" VerticalAlignment="Bottom" Content="Get Details..." Grid.Row="1" Grid.Column="0"  
  19. Click="btnBrowse_Click"/>  
  20. <Grid Grid.Row="1" Grid.Column="1" ShowGridLines="true" Margin="5">  
  21. <Grid.RowDefinitions>  
  22. <RowDefinition></RowDefinition>  
  23. <RowDefinition></RowDefinition>  
  24. <RowDefinition></RowDefinition>  
  25. <RowDefinition></RowDefinition>  
  26. <RowDefinition></RowDefinition>  
  27. <RowDefinition></RowDefinition>  
  28. <RowDefinition></RowDefinition>  
  29. <RowDefinition></RowDefinition>  
  30. </Grid.RowDefinitions>  
  31. <TextBlock Grid.Row="0" x:Name="txtId" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  32. <TextBlock Grid.Row="1" x:Name="txtAttributes" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  33. <TextBlock Grid.Row="2" x:Name="txtAge" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  34. <TextBlock Grid.Row="3" x:Name="txtGender" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  35. <TextBlock Grid.Row="4" x:Name="txtSmile" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  36. <TextBlock Grid.Row="5" x:Name="txtFacialHair" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  37. <TextBlock Grid.Row="6" x:Name="txtHeadPose" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  38. <TextBlock Grid.Row="7" x:Name="txtGlasses" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>  
  39. </Grid>  
  40. </Grid>  
  41. </Window>  

 

In my xaml code I have image control and a button, once I click on the button we will get all image attributes in textblocks.

Let’s work on code part now.

  1. using Microsoft.ProjectOxford.Face;  
  2. using System;  
  3. using System.Windows;  
  4. using System.Windows.Media.Imaging;  
  5. namespace CognitiveServicesFaceAPI_WPFSample  
  6. {  
  7. /// <summary>  
  8. /// Interaction logic for MainWindow.xaml  
  9. /// </summary>  
  10. public partial class MainWindow1 : Window  
  11. {  
  12. private readonly IFaceServiceClient objFaceServiceClient = new FaceServiceClient("Subscription Key");  
  13. string imageUrl = "http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/raj197920160520091111.jpg";  
  14. public MainWindow1()  
  15. {  
  16. InitializeComponent();  
  17. Uri fileUri = new Uri(imageUrl);  
  18. BitmapImage bitmapSource = new BitmapImage();  
  19. bitmapSource.BeginInit();  
  20. bitmapSource.CacheOption = BitmapCacheOption.None;  
  21. bitmapSource.UriSource = fileUri;  
  22. bitmapSource.EndInit();  
  23. imgPhoto.Source = bitmapSource;  
  24. }  
  25. private async void btnBrowse_Click(object sender, RoutedEventArgs e)  
  26. {  
  27. var requiredFaceAttributes = new FaceAttributeType[] {  
  28. FaceAttributeType.Age,  
  29. FaceAttributeType.Gender,  
  30. FaceAttributeType.Smile,  
  31. FaceAttributeType.FacialHair,  
  32. FaceAttributeType.HeadPose,  
  33. FaceAttributeType.Glasses  
  34. };  
  35. var faces = await objFaceServiceClient.DetectAsync(imageUrl,returnFaceLandmarks: true,returnFaceAttributes: requiredFaceAttributes);  
  36. foreach (var face in faces)  
  37. {  
  38. var rect = face.FaceRectangle;  
  39. var landmarks = face.FaceLandmarks;  
  40. //get photo attributes  
  41. var id = face.FaceId;  
  42. var attributes = face.FaceAttributes;  
  43. var age = attributes.Age;  
  44. var gender = attributes.Gender;  
  45. var smile = attributes.Smile;  
  46. var facialHair = attributes.FacialHair;  
  47. var headPose = attributes.HeadPose;  
  48. var glasses = attributes.Glasses;  
  49. //bind textblocks  
  50. txtId.Text = "Id:- " + id.ToString();  
  51. txtAttributes.Text = "Attributes:- " + attributes.ToString();  
  52. txtAge.Text = "Age:- " + age.ToString() + " Years";  
  53. txtGender.Text = "Gender:- " + gender.ToUpper();  
  54. txtSmile.Text = "Smile:- " + smile.ToString();  
  55. txtFacialHair.Text = "Facial Hair:- " + facialHair.ToString();  
  56. txtHeadPose.Text = "Head Pose:- " + headPose.ToString();  
  57. txtGlasses.Text = "Glasses:- " + glasses.ToString();  
  58. }  
  59. }  
  60. }  
  61. }  

Run the project.


Click on Get details button to get the all attribute details.


As you can see in given screenshot we have Id, Attributes, Age, Gender, Smile, Facial hair, Head Pose, Glasses. I am showing only few things here not showing attributes, facial hair and head pose.

I wonder I know my real age now :)

Conclusion - In this article, we have seen how to use Microsoft Cognitive Service face API in WPF to get image attributes. If you have any question or comments, drop me a comment or message in C# corner comments section.

Up Next
    Ebook Download
    View all
    Learn
    View all