/// <summary> /// Pick image for detection, get detection result and put detection results into LeftResultCollection /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event argument</param> private async void LeftImagePicker_Click(object sender, RoutedEventArgs e) { // Show image picker, show jpg type files only Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".jpg"; dlg.Filter = "Image files(*.jpg, *png, *.bmp, *.gif) | *.jpg; *.png; *.bmp; *.gif"; var result = dlg.ShowDialog(); if (result.HasValue && result.Value) { FaceVerifyResult = string.Empty; // User already picked one image var pickedImagePath = dlg.FileName; var imageInfo = UIHelper.GetImageInfoForRendering(pickedImagePath); LeftImageDisplay.Source = new BitmapImage(new Uri(pickedImagePath)); // Clear last time detection results LeftResultCollection.Clear(); FaceVerifyButton.IsEnabled = (LeftResultCollection.Count != 0 && RightResultCollection.Count != 0); MainWindow.Log("Request: Detecting in {0}", pickedImagePath); var sw = Stopwatch.StartNew(); // Call detection REST API, detect faces inside the image using (var fileStream = File.OpenRead(pickedImagePath)) { try { MainWindow mainWindow = Window.GetWindow(this) as MainWindow; string subscriptionKey = mainWindow._scenariosControl.SubscriptionKey; var faceServiceClient = new FaceServiceClient(subscriptionKey); var faces = await faceServiceClient.DetectAsync(fileStream); // Handle REST API calling error if (faces == null) { return; } MainWindow.Log("Response: Success. Detected {0} face(s) in {1}", faces.Length, pickedImagePath); // Convert detection results into UI binding object for rendering foreach (var face in UIHelper.CalculateFaceRectangleForRendering(faces, MaxImageSize, imageInfo)) { // Detected faces are hosted in result container, will be used in the verification later LeftResultCollection.Add(face); } FaceVerifyButton.IsEnabled = (LeftResultCollection.Count != 0 && RightResultCollection.Count != 0); } catch (FaceAPIException ex) { MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage); return; } } } GC.Collect(); }
/// <summary> /// Pick image for detection, get detection result and put detection results into LeftResultCollection /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event argument</param> private async void LeftImagePicker_Click(object sender, RoutedEventArgs e) { // Show image picker, show jpg type files only Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".jpg"; dlg.Filter = "Image files(*.jpg) | *.jpg"; var result = dlg.ShowDialog(); if (result.HasValue && result.Value) { VerifyResult = string.Empty; // User already picked one image var pickedImagePath = dlg.FileName; var imageInfo = UIHelper.GetImageInfoForRendering(pickedImagePath); LeftImageDisplay.Source = new BitmapImage(new Uri(pickedImagePath)); // Clear last time detection results LeftResultCollection.Clear(); Output = Output.AppendLine(string.Format("Request: Detecting in {0}", pickedImagePath)); var sw = Stopwatch.StartNew(); // Call detection REST API, detect faces inside the image using (var fileStream = File.OpenRead(pickedImagePath)) { try { var faces = await App.Instance.DetectAsync(fileStream); // Handle REST API calling error if (faces == null) { return; } Output = Output.AppendLine(string.Format("Response: Success. Detected {0} face(s) in {1}", faces.Length, pickedImagePath)); // Convert detection results into UI binding object for rendering foreach (var face in UIHelper.CalculateFaceRectangleForRendering(faces, MaxImageSize, imageInfo)) { // Detected faces are hosted in result container, will be used in the verification later LeftResultCollection.Add(face); } } catch (ClientException ex) { Output = Output.AppendLine(string.Format("Response: {0}. {1}", ex.Error.Code, ex.Error.Message)); return; } } } }
private async Task SetWebcamImage(MainWindow mainWindowInstance) { var pickedImagePath = System.IO.Path.GetFullPath(ImageNameHelper.GetLatestWebcamImage()); var renderingImage = FaceRecognitionHelper.LoadImageAppliedOrientation(pickedImagePath); var imageInfo = FaceRecognitionHelper.GetImageInfoForRendering(renderingImage); LeftImageDisplay.Source = renderingImage; mainWindowInstance.Log(string.Format("Request: Detecting in {0}", pickedImagePath)); var sw = Stopwatch.StartNew(); LeftResultCollection.Clear(); var detectedFaces = await MicrosoftApiHelper.DetectFaces(pickedImagePath, mainWindowInstance, imageInfo); for (var i = 0; i < detectedFaces.Count; i++) { LeftResultCollection.Add(detectedFaces[i]); } }
/// <summary> /// Pick image for detection, get detection result and put detection results into LeftResultCollection /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event argument</param> private async void LeftImagePicker_Click(object sender, RoutedEventArgs e) { // Show image picker, show jpg type files only Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog { DefaultExt = ".jpg", Filter = "Image files(*.jpg) | *.jpg" }; var result = dlg.ShowDialog(); if (!result.HasValue || !result.Value) { return; } VerifyResult = string.Empty; // User already picked one image var pickedImagePath = dlg.FileName; var imageInfo = UIHelper.GetImageInfoForRendering(pickedImagePath); LeftImageDisplay.Source = new BitmapImage(new Uri(pickedImagePath)); // Clear last time detection results LeftResultCollection.Clear(); Output = Output.AppendLine($"发送请求: 检测图片 {pickedImagePath} 中"); var sw = Stopwatch.StartNew(); // Call detection REST API, detect faces inside the image using (var fileStream = File.OpenRead(pickedImagePath)) { try { MainWindow mainWindow = Window.GetWindow(this) as MainWindow; if (mainWindow == null) { return; } string subscriptionKey = mainWindow.SubscriptionKey; var faceServiceClient = new FaceServiceClient(subscriptionKey); var faces = await faceServiceClient.DetectAsync(fileStream); // Handle REST API calling error if (faces == null) { return; } Output = Output.AppendLine($"反馈:检测成功. 共发现 {faces.Length} 张脸 在图片 {pickedImagePath}"); // Convert detection results into UI binding object for rendering foreach (var face in UIHelper.CalculateFaceRectangleForRendering(faces, MaxImageSize, imageInfo)) { // Detected faces are hosted in result container, will be used in the verification later LeftResultCollection.Add(face); } } catch (ClientException ex) { Output = Output.AppendLine($"反馈: 出错啦 {ex.Error.Code}. {ex.Error.Message}"); } } }