<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"/>
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; }
using Microsoft.ProjectOxford.Face; using Microsoft.ProjectOxford.Face.Contract;
private readonly IFaceServiceClient faceServiceClient = new FaceServiceClient("key");
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]; } }
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(); }
Source: https://habr.com/ru/post/282231/
All Articles