public float[] computeAverageEmotions(Emotion[] emotions)
		{
			float[] emotionsTemp = new float[8];
			for (int i = 0; i < emotions.Length; i++)
			{
				emotionsTemp[0] += emotions[i].Scores.Anger / emotions.Length;
			}
			return emotionsTemp;
		}
        private EmotionViewModel GetTopScore(Emotion emotion, EmotionViewModel emotionViewModel)
        {
            emotionViewModel.TopScore = emotion.Scores.Anger;
            emotionViewModel.TopScoreType = "Anger";

            if (emotion.Scores.Contempt > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Contempt;
                emotionViewModel.TopScoreType = "Contempt";
            }

            if (emotion.Scores.Disgust > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Disgust;
                emotionViewModel.TopScoreType = "Disgust";
            }

            if (emotion.Scores.Fear > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Fear;
                emotionViewModel.TopScoreType = "Fear";
            }

            if (emotion.Scores.Happiness > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Happiness;
                emotionViewModel.TopScoreType = "Happiness";
            }

            if (emotion.Scores.Neutral > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Neutral;
                emotionViewModel.TopScoreType = "Neutral";
            }

            if (emotion.Scores.Sadness > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Sadness;
                emotionViewModel.TopScoreType = "Sadness";
            }

            if (emotion.Scores.Surprise > emotionViewModel.TopScore)
            {
                emotionViewModel.TopScore = emotion.Scores.Surprise;
                emotionViewModel.TopScoreType = "Surprise";
            }

            return emotionViewModel;
        }
Exemple #3
0
 private static IEnumerable<SentimentResult> ConvertEmotionsToSentimentResults(Emotion[] emotions)
 {
     foreach (var emotion in emotions)
     {
         yield return new SentimentResult
         {
             Anger     = Math.Round((decimal)emotion.Scores.Anger * 1000,0),
             Contempt  = Math.Round((decimal)emotion.Scores.Contempt * 1000,0),
             Disgust   = Math.Round((decimal)emotion.Scores.Disgust * 1000,0),
             Fear      = Math.Round((decimal)emotion.Scores.Fear * 1000,0),
             Happiness = Math.Round((decimal)emotion.Scores.Happiness * 1000,0),
             Neutral   = Math.Round((decimal)emotion.Scores.Neutral * 1000,0),
             Sadness   = Math.Round((decimal)emotion.Scores.Sadness * 1000,0),
             Surprise  = Math.Round((decimal)emotion.Scores.Surprise * 1000,0),
             Moment    = DateTime.Now,
             Age       = 22,
             Gender    = "M"
         };
     }
 }
        public void LogEmotionResult(Emotion[] emotionResult)
        {
            int emotionResultCount = 0;
            if (emotionResult != null && emotionResult.Length > 0)
            {
                foreach (Emotion emotion in emotionResult)
                {
                    Log("Emotion[" + emotionResultCount + "]");
                    Log("  .FaceRectangle = left: " + emotion.FaceRectangle.Left
                             + ", top: " + emotion.FaceRectangle.Top
                             + ", width: " + emotion.FaceRectangle.Width
                             + ", height: " + emotion.FaceRectangle.Height);

                    Log("  Anger    : " + emotion.Scores.Anger.ToString());
                    Log("  Contempt : " + emotion.Scores.Contempt.ToString());
                    Log("  Disgust  : " + emotion.Scores.Disgust.ToString());
                    Log("  Fear     : " + emotion.Scores.Fear.ToString());
                    Log("  Happiness: " + emotion.Scores.Happiness.ToString());
                    Log("  Neutral  : " + emotion.Scores.Neutral.ToString());
                    Log("  Sadness  : " + emotion.Scores.Sadness.ToString());
                    Log("  Surprise  : " + emotion.Scores.Surprise.ToString());
                    Log("");
                    emotionResultCount++;
                }
            }
            else
            {
                Log("No emotion is detected. This might be due to:\n" +
                    "    image is too small to detect faces\n" +
                    "    no faces are in the images\n" +
                    "    faces poses make it difficult to detect emotions\n" +
                    "    or other factors");
            }
        }
        public void ListEmotionResult(Uri imageUri, ListBox resultListBox, Emotion[] emotionResult)
        {
            if (emotionResult != null)
            {
                EmotionResultDisplay[] resultDisplay = new EmotionResultDisplay[8];
                List<EmotionResultDisplayItem> itemSource = new List<EmotionResultDisplayItem>();
                for (int i = 0; i < emotionResult.Length; i++)
                {
                    Emotion emotion = emotionResult[i];
                    resultDisplay[0] = new EmotionResultDisplay { EmotionString = "Anger", Score = emotion.Scores.Anger };
                    resultDisplay[1] = new EmotionResultDisplay { EmotionString = "Contempt", Score = emotion.Scores.Contempt };
                    resultDisplay[2] = new EmotionResultDisplay { EmotionString = "Disgust", Score = emotion.Scores.Disgust };
                    resultDisplay[3] = new EmotionResultDisplay { EmotionString = "Fear", Score = emotion.Scores.Fear };
                    resultDisplay[4] = new EmotionResultDisplay { EmotionString = "Happiness", Score = emotion.Scores.Happiness };
                    resultDisplay[5] = new EmotionResultDisplay { EmotionString = "Neutral", Score = emotion.Scores.Neutral };
                    resultDisplay[6] = new EmotionResultDisplay { EmotionString = "Sadness", Score = emotion.Scores.Sadness };
                    resultDisplay[7] = new EmotionResultDisplay { EmotionString = "Surprise", Score = emotion.Scores.Surprise };

                    Array.Sort(resultDisplay, CompareDisplayResults);

                    String[] emotionStrings = new String[3];
                    for (int j = 0; j < 3; j++)
                    {
                        emotionStrings[j] = resultDisplay[j].EmotionString + ":" + resultDisplay[j].Score.ToString("0.000000"); ;
                    }

                    itemSource.Add(new EmotionResultDisplayItem
                    {
                        ImageSource = imageUri,
                        UIRect = new Int32Rect(emotion.FaceRectangle.Left, emotion.FaceRectangle.Top, emotion.FaceRectangle.Width, emotion.FaceRectangle.Height),
                        Emotion1 = emotionStrings[0],
                        Emotion2 = emotionStrings[1],
                        Emotion3 = emotionStrings[2]
                    });
                }
                resultListBox.ItemsSource = itemSource;
            }
        }
        public void DrawFaceRectangle(Image image, BitmapImage bitmapSource, Emotion[] emotionResult)
        {
            if (emotionResult != null && emotionResult.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 emotion in emotionResult)
                {
                    Microsoft.ProjectOxford.Common.Rectangle faceRect = emotion.FaceRectangle;

                    drawingContext.DrawRectangle(
                        Brushes.Transparent,
                        new Pen(Brushes.Cyan, 4),
                        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);

                image.Source = faceWithRectBitmap;
            }
        }
        public void ListEmotionResult(Uri imageUri, ListBox resultListBox, Emotion[] emotionResult)
        {
            if (emotionResult != null)
            {
                EmotionResultDisplay[] resultDisplay = new EmotionResultDisplay[8];
                List<EmotionResultDisplayItem> itemSource = new List<EmotionResultDisplayItem>();
                for (int i = 0; i < emotionResult.Length; i++)
                {
                    Emotion emotion = emotionResult[i];
                    resultDisplay[0] = new EmotionResultDisplay { EmotionString = "Anger", Score = emotion.Scores.Anger };
                    resultDisplay[1] = new EmotionResultDisplay { EmotionString = "Contempt", Score = emotion.Scores.Contempt };
                    resultDisplay[2] = new EmotionResultDisplay { EmotionString = "Disgust", Score = emotion.Scores.Disgust };
                    resultDisplay[3] = new EmotionResultDisplay { EmotionString = "Fear", Score = emotion.Scores.Fear };
                    resultDisplay[4] = new EmotionResultDisplay { EmotionString = "Happiness", Score = emotion.Scores.Happiness };
                    resultDisplay[5] = new EmotionResultDisplay { EmotionString = "Neutral", Score = emotion.Scores.Neutral };
                    resultDisplay[6] = new EmotionResultDisplay { EmotionString = "Sadness", Score = emotion.Scores.Sadness };
                    resultDisplay[7] = new EmotionResultDisplay { EmotionString = "Surprise", Score = emotion.Scores.Surprise };

                    Array.Sort(resultDisplay, delegate (EmotionResultDisplay result1, EmotionResultDisplay result2)
                    {
                        return ((result1.Score == result2.Score) ? 0 : ((result1.Score < result2.Score) ? 1 : -1));
                    });

                    StackPanel itemPanel = new StackPanel();
                    itemPanel.Orientation = Orientation.Horizontal;

                    String[] emotionStrings = new String[3];
                    for (int j = 0; j < 3; j++)
                    {
                        StackPanel emotionPanel = new StackPanel();
                        emotionPanel.Orientation = Orientation.Vertical;

                        TextBlock emotionText = new TextBlock();
                        emotionText.Text = resultDisplay[j].EmotionString + ":" + resultDisplay[j].Score.ToString("0.000000");

                        itemPanel.Children.Add(emotionPanel);
                        emotionStrings[j] = resultDisplay[j].EmotionString + ":" + resultDisplay[j].Score.ToString("0.000000"); ;
                    }
                    itemSource.Add(new EmotionResultDisplayItem
                    {
                        ImageSource = imageUri,
                        UIRect = new Int32Rect(emotion.FaceRectangle.Left, emotion.FaceRectangle.Top, emotion.FaceRectangle.Width, emotion.FaceRectangle.Height),
                        Emotion1 = emotionStrings[0],
                        Emotion2 = emotionStrings[1],
                        Emotion3 = emotionStrings[2]
                    });
                }
                resultListBox.ItemsSource = itemSource;
            }
        }