private async Task DetectAndShowEmotion() { this.progressIndicator.IsActive = true; this.DetectFaceAttributes = 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) { double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth; double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight; if (imageWithFace.DetectedFaces == null) { await imageWithFace.DetectFacesAsync(detectFaceAttributes : this.DetectFaceAttributes, detectFaceLandmarks : this.DetectFaceLandmarks); } foreach (Face face in imageWithFace.DetectedFaces) { FaceIdentificationBorder faceUI = new FaceIdentificationBorder(); 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); Microsoft.ProjectOxford.Common.Rectangle rectangle = new Microsoft.ProjectOxford.Common.Rectangle(); rectangle.Height = face.FaceRectangle.Height; rectangle.Left = face.FaceRectangle.Left; rectangle.Top = face.FaceRectangle.Top; rectangle.Width = face.FaceRectangle.Width; Emotion emotion = new Emotion(); emotion.FaceRectangle = rectangle; emotion.Scores = face.FaceAttributes.Emotion; faceUI.ShowEmotionData(emotion); this.hostGrid.Children.Add(faceUI); if (!this.ShowMultipleFaces) { break; } } } this.progressIndicator.IsActive = false; }
private async Task DetectAndShowEmotion() { ShowProgressBar(); 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) { EmotionDetecting?.Invoke(this, new EventArgs()); await imageWithFace.DetectEmotionAsync(); EmotionDetected?.Invoke(this, new EventArgs()); } 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.emotionEmojiControl.UpdateEmotion(emotion.Scores); this.hostGrid.Children.Add(faceUI); if (!this.ShowMultipleFaces) { break; } } } //this.progressIndicator.IsActive = false; HideProgressBar(); }
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); } if (this.DataContext is ImageAnalyzer imageWithFace) { if (imageWithFace.DetectedFaces == null) { await imageWithFace.DetectFacesAsync(detectFaceAttributes : true); } double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth; double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight; foreach (DetectedFace face in imageWithFace.DetectedFaces) { FaceIdentificationBorder faceUI = new FaceIdentificationBorder { 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), BalloonBackground = this.BalloonBackground, BalloonForeground = this.BalloonForeground }; faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform); faceUI.ShowEmotionData(face.FaceAttributes.Emotion); this.hostGrid.Children.Add(faceUI); if (!this.ShowMultipleFaces) { break; } } } this.progressIndicator.IsActive = false; }
private async Task <bool> 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; if (App.RequiredEmotion != Areas.ExpectedEmotion.NaN) { if (imageWithFace.DetectedEmotion.Count() != 2) { this.progressIndicator.IsActive = false; if (this.OnFailure != null) { this.OnFailure(this, false); } } int winnerId = 0; switch (App.RequiredEmotion) { case Areas.ExpectedEmotion.Happy: winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Happiness == imageWithFace.DetectedEmotion.Max(i => i.Scores.Happiness)).FirstOrDefault().FaceRectangle.Left; break; case Areas.ExpectedEmotion.Sad: winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Sadness == imageWithFace.DetectedEmotion.Max(i => i.Scores.Sadness)).FirstOrDefault().FaceRectangle.Left; break; case Areas.ExpectedEmotion.Angry: winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Anger == imageWithFace.DetectedEmotion.Max(i => i.Scores.Anger)).FirstOrDefault().FaceRectangle.Left; break; case Areas.ExpectedEmotion.Surprised: winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Surprise == imageWithFace.DetectedEmotion.Max(i => i.Scores.Surprise)).FirstOrDefault().FaceRectangle.Left; break; case Areas.ExpectedEmotion.Neutral: winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Neutral == imageWithFace.DetectedEmotion.Max(i => i.Scores.Neutral)).FirstOrDefault().FaceRectangle.Left; break; } if (winnerId == 0) { this.progressIndicator.IsActive = false; if (this.OnFailure != null) { this.OnFailure(this, false); } } //Game foreach (Emotion emotion in imageWithFace.DetectedEmotion) { //Result: float r = 1; switch (App.RequiredEmotion) { case Areas.ExpectedEmotion.Happy: r = emotion.Scores.Happiness; break; case Areas.ExpectedEmotion.Sad: r = emotion.Scores.Sadness; break; case Areas.ExpectedEmotion.Angry: r = emotion.Scores.Anger; break; case Areas.ExpectedEmotion.Surprised: r = emotion.Scores.Surprise; break; case Areas.ExpectedEmotion.Neutral: r = emotion.Scores.Neutral; break; } EmotionFaceIdentificationBorder faceUI = new EmotionFaceIdentificationBorder(emotion.FaceRectangle.Left == winnerId, r); //faceUI.Winner = emotion.FaceRectangle.Left == winnerId; // faceUI.RequestedEmotion = App.RequiredEmotion.ToString(); 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); } this.progressIndicator.IsActive = false; if (this.OnSuccess != null) { this.OnSuccess(this, true); } //return true; } else { foreach (Emotion emotion in imageWithFace.DetectedEmotion) { //logic goes here. 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; } } // return true; } } this.progressIndicator.IsActive = false; return(false); }