📜 ⬆️ ⬇️

Determine gender and age using Microsoft Project Oxford and C #

In the last article I told you what Microsoft Project Oxford is and how to create a Telegram bot that determines gender and age using a photo in PHP.

But today, I will show an example of a WPF application using the Microsoft Project Oxford Client SDK and C # similar to How-Old.net



Let's start:
')
1) Open Visual Studio 2015 and create a WPF project



2) The next step we need to install via Manage NuGet Packages - Newtonsoft.Json



3) Open MainWindow.xaml and add Image and Button

<Image x:Name="FacePhoto" HorizontalAlignment="Left" Height="240" Margin="72,25,0,0" VerticalAlignment="Top" Width="381"/> <Button x:Name="button" Content=" " HorizontalAlignment="Left" Height="28" Margin="72,281,0,0" VerticalAlignment="Top" Width="381" Click="button_Click"/> 


4) Go to the code, add the code to the button to upload photos
  private async void button_Click(object sender, RoutedEventArgs e) { var openDlg = new Microsoft.Win32.OpenFileDialog(); openDlg.Filter = "JPEG Image(*.jpg)|*.jpg"; bool? result = openDlg.ShowDialog(this); if (!(bool)result) { return; } string filePath = openDlg.FileName; Uri fileUri = new Uri(filePath); BitmapImage bitmapSource = new BitmapImage(); bitmapSource.BeginInit(); bitmapSource.CacheOption = BitmapCacheOption.None; bitmapSource.UriSource = fileUri; bitmapSource.EndInit(); FacePhoto.Source = bitmapSource; } 


5) Now you need to install the same through Manage NuGet Packages - Microsoft.ProjectOxford.Face



6) Now we need to connect the following directives

 using Microsoft.ProjectOxford.Face; using Microsoft.ProjectOxford.Face.Contract; 


After all the preparations are made, proceed to the most interesting =)

7) Create a variable that will store the API key

 private readonly IFaceServiceClient faceServiceClient = new FaceServiceClient("key"); 


8) Functions that will process the photo.

 private async Task<FaceRectangle[]> UploadAndDetectFaces(string imageFilePath) { List<FaceAttributeType> FaceAG = new List<FaceAttributeType>(); a.Add(FaceAttributeType.Age); a.Add(FaceAttributeType.Gender);//     try { using (Stream imageFileStream = File.OpenRead(imageFilePath)) { var faces = await faceServiceClient.DetectAsync(imageFileStream, true, false, FaceAG); //     var faceRects = faces.Select(face => face.FaceRectangle); //    var faceA = faces.Select(face => face.FaceAttributes);//   -    faceAtr = faceA.ToArray(); return faceRects.ToArray(); } } catch (Exception) { return new FaceRectangle[0]; } } 


That's all, now it remains to issue a little application, circle the faces in the photo and display the gender and age.

For convenience, I connected the console, where I will display age and gender.

Go to the button handler function and add the following code there:

  Title = "..."; FaceRectangle[] faceRects = await UploadAndDetectFaces(filePath); Title = String.Format(". {0} ", faceRects.Length); if (faceRects.Length > 0) { DrawingVisual visual = new DrawingVisual(); DrawingContext drawingContext = visual.RenderOpen(); drawingContext.DrawImage(bitmapSource, new Rect(0, 0, bitmapSource.Width, bitmapSource.Height)); double dpi = bitmapSource.DpiX; double resizeFactor = 96 / dpi; foreach (var faceRect in faceRects) { drawingContext.DrawRectangle( Brushes.Transparent, new Pen(Brushes.Red, 2), new Rect( faceRect.Left * resizeFactor, faceRect.Top * resizeFactor, faceRect.Width * resizeFactor, faceRect.Height * resizeFactor ) ); } drawingContext.Close(); RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap( (int)(bitmapSource.PixelWidth * resizeFactor), (int)(bitmapSource.PixelHeight * resizeFactor), 96, 96, PixelFormats.Pbgra32); faceWithRectBitmap.Render(visual); FacePhoto.Source = faceWithRectBitmap; } foreach (var fecea in faceAtr) { Console.WriteLine(": {0}", fecea.Age); Console.WriteLine(": {0}", fecea.Gender); Console.WriteLine(); } 


As a result, we have the following



That's all, a small sample application is ready, good luck to all!

Source: https://habr.com/ru/post/282231/


All Articles