private async Task DetectAndShowEmotion() { this.progressIndicator.IsActive = true; foreach (var child in this.hostGrid.Children.Where(c => !(c is Image)).ToArray()) { this.hostGrid.Children.Remove(child); } ImageAnalyzer imageWithFace = this.DataContext as ImageAnalyzer; if (imageWithFace != null) { if (imageWithFace.DetectedEmotion == null) { await imageWithFace.DetectEmotionAsync(); } double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth; double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight; foreach (Emotion emotion in imageWithFace.DetectedEmotion) { FaceIdentificationBorder faceUI = new FaceIdentificationBorder(); faceUI.Margin = new Thickness((emotion.FaceRectangle.Left * renderedImageXTransform) + ((this.ActualWidth - this.imageControl.RenderSize.Width) / 2), (emotion.FaceRectangle.Top * renderedImageYTransform) + ((this.ActualHeight - this.imageControl.RenderSize.Height) / 2), 0, 0); faceUI.BalloonBackground = this.BalloonBackground; faceUI.BalloonForeground = this.BalloonForeground; faceUI.ShowFaceRectangle(emotion.FaceRectangle.Width * renderedImageXTransform, emotion.FaceRectangle.Height * renderedImageYTransform); faceUI.ShowEmotionData(emotion); this.hostGrid.Children.Add(faceUI); if (!this.ShowMultipleFaces) { break; } } } this.progressIndicator.IsActive = false; }
private async Task DetectAndShowFaceBorders() { this.progressIndicator.IsActive = true; foreach (var child in this.hostGrid.Children.Where(c => !(c is Image)).ToArray()) { this.hostGrid.Children.Remove(child); } ImageAnalyzer imageWithFace = this.DataContext as ImageAnalyzer; if (imageWithFace != null) { if (imageWithFace.DetectedFaces == null) { await imageWithFace.DetectFacesAsync(detectFaceAttributes : this.DetectFaceAttributes, detectFaceLandmarks : this.DetectFaceLandmarks); } double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth; double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight; foreach (Face face in imageWithFace.DetectedFaces) { FaceIdentificationBorder faceUI = new FaceIdentificationBorder() { Tag = face.FaceId, }; faceUI.Margin = new Thickness((face.FaceRectangle.Left * renderedImageXTransform) + ((this.ActualWidth - this.imageControl.RenderSize.Width) / 2), (face.FaceRectangle.Top * renderedImageYTransform) + ((this.ActualHeight - this.imageControl.RenderSize.Height) / 2), 0, 0); faceUI.BalloonBackground = this.BalloonBackground; faceUI.BalloonForeground = this.BalloonForeground; faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform); if (this.DetectFaceLandmarks) { faceUI.ShowFaceLandmarks(renderedImageXTransform, renderedImageYTransform, face); } this.hostGrid.Children.Add(faceUI); if (!this.ShowMultipleFaces) { break; } } if (this.PerformRecognition) { if (imageWithFace.IdentifiedPersons == null) { await imageWithFace.IdentifyFacesAsync(); } if (this.ShowRecognitionResults) { foreach (Face face in imageWithFace.DetectedFaces) { // Get the border for the associated face id FaceIdentificationBorder faceUI = (FaceIdentificationBorder)this.hostGrid.Children.FirstOrDefault(e => e is FaceIdentificationBorder && (Guid)(e as FaceIdentificationBorder).Tag == face.FaceId); if (faceUI != null) { IdentifiedPerson faceIdIdentification = imageWithFace.IdentifiedPersons.FirstOrDefault(p => p.FaceId == face.FaceId); string name = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Person.Name : null; string gender = this.DetectFaceAttributes ? face.FaceAttributes.Gender : null; double age = this.DetectFaceAttributes ? face.FaceAttributes.Age : 0; double confidence = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Confidence : 0; faceUI.ShowIdentificationData(age, gender, (uint)Math.Round(confidence * 100), name); } } } } } this.progressIndicator.IsActive = false; }