Esempio n. 1
0
        public async void SaveCurrentEmotionsToFolder(EmotionScores emotions, Geoposition currentPosition, string photoFileName, string folderName = "EmotionDataFolder")
        {
            var currentEmotionData = new EmotionsData()
            {
                Anger    = emotions.Anger,
                Sadness  = emotions.Sadness,
                Neutral  = emotions.Neutral,
                Fear     = emotions.Fear,
                Disgust  = emotions.Disgust,
                Contempt = emotions.Contempt,
                Hapiness = emotions.Happiness,
                Suprise  = emotions.Surprise,
            };

            var bestEmotionKey   = emotions.ToRankedList().FirstOrDefault().Key;
            var bestEmotionValue = emotions.ToRankedList().FirstOrDefault().Value;

            currentEmotionData.BestEmotionName  = bestEmotionKey;
            currentEmotionData.BestEmotionScore = bestEmotionValue;
            //currentEmotionData.position = currentPosition;
            currentEmotionData.Longitude     = currentPosition.Coordinate.Longitude;
            currentEmotionData.Latitude      = currentPosition.Coordinate.Latitude;
            currentEmotionData.PhotoFileName = photoFileName;

            currentEmotionData.DateTaken = DateTime.Now;
            string fileName = DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".xml";
            await SerializationController.SaveObjectToXml <EmotionsData>(currentEmotionData, fileName, folderName);
        }
        public async Task <string> Face_Analyse(Stream imageFileStream)
        {
            //Count Faces
            Face[] faces = await faceServiceClient.DetectAsync(imageFileStream, returnFaceId : true, returnFaceLandmarks : false, returnFaceAttributes : faceAttributes);

            if (faces.Count() > 1)
            {
                throw new Exception("Zu viele Gesichter erkannt...");
            }
            if (faces.Count() < 1)
            {
                throw new Exception("Kein Gesicht erkannt...");
            }

            //Emotion für Gesicht auslesen
            EmotionScores emotionScores            = faces[0].FaceAttributes.Emotion;
            Dictionary <string, float> emotionList = new Dictionary <string, float>(emotionScores.ToRankedList());

            //Zusammensetzen der Rückgabe
            StringBuilder sb = new StringBuilder();

            sb.Append("<div><table>");
            foreach (KeyValuePair <string, float> element in emotionList)
            {
                sb.Append(String.Format("<tr><td>{0}: " + "</td><td>" + " {1}%" + "</td></tr>", Translate(element.Key), (element.Value * 100).ToString()));
            }
            sb.Append("</table></div>");

            //Rückgabe
            return(Newtonsoft.Json.JsonConvert.SerializeObject(sb.ToString()));
        }
Esempio n. 3
0
        public void UpdateEmotion(EmotionScores scores)
        {
            var topEmotion = scores.ToRankedList().First();

            this.filledBar.Background            = this.emotionToColorMapping[topEmotion.Key];
            this.emptySpaceRowDefinition.Height  = new GridLength(1 - topEmotion.Value, Windows.UI.Xaml.GridUnitType.Star);
            this.filledSpaceRowDefinition.Height = new GridLength(topEmotion.Value, Windows.UI.Xaml.GridUnitType.Star);
        }
Esempio n. 4
0
        public string ParseEmotions(EmotionScores scores)
        {
            if (scores == null)
            {
                return(null);
            }

            return(scores.ToRankedList().FirstOrDefault().Key);
        }
Esempio n. 5
0
        public void UpdateEmotion(EmotionScores scores)
        {
            var    topEmotion = scores.ToRankedList().First();
            string label = "", emoji = "";

            switch (topEmotion.Key)
            {
            case "Anger":
                label = "Angry";
                emoji = "\U0001f620";
                break;

            case "Contempt":
                label = "Contemptuous";
                emoji = "\U0001f612";
                break;

            case "Disgust":
                label = "Disgusted";
                emoji = "\U0001f627";
                break;

            case "Fear":
                label = "Afraid";
                emoji = "\U0001f628";
                break;

            case "Happiness":
                label = "Happy";
                emoji = "\U0001f60a";
                break;

            case "Neutral":
                label = "Neutral";
                emoji = "\U0001f614";
                break;

            case "Sadness":
                label = "Sad";
                emoji = "\U0001f622";
                break;

            case "Surprise":
                label = "Surprised";
                emoji = "\U0001f632";
                break;

            default:
                label = "";
                emoji = "\U0001f60a";
                break;
            }

            this.emotionEmoji.Text = emoji;
            this.emotionText.Text  = label;
        }
Esempio n. 6
0
        public void ShowEmotionData(EmotionScores emotion)
        {
            this.EmotionData = emotion.ToRankedList().ToArray();

            this.DataContext = this;

            this.genderAgeGrid.Visibility = Visibility.Collapsed;
            this.emotionGrid.Visibility   = Visibility.Visible;
            this.captionCanvas.Visibility = Visibility.Visible;
        }
        private System.Windows.Shapes.Rectangle ComposeRectangleBarFromEmotions(EmotionScores emotionScores)
        {
            var result = new System.Windows.Shapes.Rectangle();

            var emotionRankedList       = emotionScores.ToRankedList();
            var gradientStopsCollection = new GradientStopCollection();
            var occuringEmotions        = new List <KeyValuePair <string, float> >();

            foreach (var emotion in emotionRankedList.Reverse())
            {
                if (emotion.Value > 0.0)
                {
                    occuringEmotions.Add(emotion);
                }
            }
            var occuringEmotionsCount = occuringEmotions.Count;
            var previousValue         = 0.0f;

            for (int i = 0; i < occuringEmotionsCount; i++)
            {
                var emotion      = occuringEmotions[i];
                var emotionColor = GetEmotionColor(emotion.Key);
                var emotionValue = emotion.Value;

                if (i == 0 || i == occuringEmotionsCount - 1)
                {
                    if (i == 0)
                    {
                        previousValue = emotionValue;
                    }
                    var gradientStop = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStop);
                }
                else
                {
                    var gradientStopStartPoint = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStopStartPoint);
                    previousValue += emotionValue;
                    var gradientStopEndPoint = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStopEndPoint);
                }
            }
            var linearGradientBrush = new LinearGradientBrush(gradientStopsCollection, new System.Windows.Point(0.0, 0.0), new System.Windows.Point(0.0, 1.0));
            var composedBar         = new System.Windows.Shapes.Rectangle();

            composedBar.Fill   = linearGradientBrush;
            composedBar.Width  = 5.0;
            composedBar.Margin = new Thickness(1.0, 0, 1.0, 0);
            result             = composedBar;

            return(result);
        }
        public static Result ToMostProbable(this EmotionScores scores)
        {
            Contract.Assert(scores != null);

            var first = scores.ToRankedList().FirstOrDefault();

            if (first.Equals(default(KeyValuePair <string, float>)))
            {
                return(Result.Empty);
            }

            return(Result.FromScore(first.Key, first.Value));
        }
Esempio n. 9
0
        private string getEmotion(EmotionScores emotion)
        {
            KeyValuePair <string, float> bestMatch = new KeyValuePair <string, float>("", 0);

            foreach (KeyValuePair <string, float> item in emotion.ToRankedList())
            {
                if (item.Value > bestMatch.Value)
                {
                    bestMatch = item;
                }
            }
            return(bestMatch.Key + " (" + NumberHelpers.ToPercent(bestMatch.Value) + ")");
        }
Esempio n. 10
0
        public static KeyValuePair <string, float> getDominantEmotion(EmotionScores scores)
        {
            var scoreList = scores.ToRankedList();
            KeyValuePair <string, float> maxPair = scoreList.First();

            foreach (var score in scoreList)
            {
                if (score.Value > maxPair.Value)
                {
                    maxPair = score;
                }
            }
            return(maxPair);
        }
Esempio n. 11
0
        private void DrawEmotionText(Face face)
        {
            EmotionScores emotionScores = face.FaceAttributes.Emotion;
            KeyValuePair <string, float> highestScoreEmotion = emotionScores.ToRankedList().FirstOrDefault();
            TextBlock emotionText = new TextBlock
            {
                FontSize   = 30,
                Foreground = new SolidColorBrush(Colors.Yellow),
                Text       = $"{highestScoreEmotion.Key} ({highestScoreEmotion.Value * 100:0.##)}%"
            };

            Canvas.SetLeft(emotionText, face.FaceRectangle.Left);
            Canvas.SetTop(emotionText, face.FaceRectangle.Top + face.FaceRectangle.Height + 2);

            EmotionsCanvas.Children.Add(emotionText);
        /// <summary>
        /// Function to analyze face emotions, based on a given result score
        /// </summary>
        /// <param name="analysisResult"><see cref="Scores"/> result object</param>
        /// <returns>A string with the current emotion</returns>
        private string AnalyseEmotions(EmotionScores analysisResult)
        {
            string emotion = string.Empty;

            var sortedEmotions = analysisResult.ToRankedList();

            string currentEmotion = sortedEmotions.First().Key;

            switch (currentEmotion)
            {
            case "Anger":
                emotion = "angry";
                break;

            case "Contempt":
                emotion = "contempt";
                break;

            case "Disgust":
                emotion = "disgusted";
                break;

            case "Fear":
                emotion = "scared";
                break;

            case "Happiness":
                emotion = "happy";
                break;

            case "Neutral":
            default:
                emotion = "neutral";
                break;

            case "Sadness":
                emotion = "sad";
                break;

            case "Suprise":
                emotion = "suprised";
                break;
            }

            return(emotion);
        }
        private Rectangle ComposeRectangleBarFromEmotions(EmotionScores emotionScores)
        {
            var emotionRankedList       = emotionScores.ToRankedList();
            var gradientStopsCollection = new GradientStopCollection();
            var occuringEmotions        = emotionRankedList.Reverse().Where(emotion => emotion.Value > 0.0).ToList();
            var occuringEmotionsCount   = occuringEmotions.Count;
            var previousValue           = 0.0f;

            for (var i = 0; i < occuringEmotionsCount; i++)
            {
                var emotion      = occuringEmotions[i];
                var emotionColor = GetEmotionColor(emotion.Key);
                var emotionValue = emotion.Value;

                if (i == 0 || i == occuringEmotionsCount - 1)
                {
                    if (i == 0)
                    {
                        previousValue = emotionValue;
                    }
                    var gradientStop = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStop);
                }
                else
                {
                    var gradientStopStartPoint = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStopStartPoint);
                    previousValue += emotionValue;
                    var gradientStopEndPoint = new GradientStop(emotionColor, previousValue);
                    gradientStopsCollection.Add(gradientStopEndPoint);
                }
            }

            var linearGradientBrush =
                new LinearGradientBrush(gradientStopsCollection, new Point(0.0, 0.0), new Point(0.0, 1.0));
            var composedBar = new Rectangle
            {
                Fill   = linearGradientBrush,
                Width  = 5.0,
                Margin = new Thickness(1.0, 0, 1.0, 0)
            };
            var result = composedBar;

            return(result);
        }
        private string GetStrongestEmotion(EmotionScores emotionScores)
        {
            var rankedList = emotionScores.ToRankedList().Take(2).ToList();

            if (rankedList[0].Key != "Neutral" && rankedList[0].Value > 0.7)
            {
                return(rankedList[0].Key);
            }
            if (rankedList[0].Key == "Neutral" && rankedList[1].Value > 0.2)
            {
                return(rankedList[1].Key);
            }
            if (rankedList[0].Value > 0.7)
            {
                return(rankedList[0].Key);
            }
            return(null);
        }
Esempio n. 15
0
        private async void Button_ClickAsync(object sender, RoutedEventArgs e)
        {
            using (var captureStream = new InMemoryRandomAccessStream())
            {
                await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream);

                captureStream.Seek(0);
                try
                {
                    var faces = await fClient.DetectAsync(captureStream.AsStream(), true, false, faceAttributes);

                    //get photo attributes
                    var           id            = faces[0].FaceId;
                    var           attributes    = faces[0].FaceAttributes;
                    var           age           = attributes.Age;
                    var           gender        = attributes.Gender;
                    EmotionScores emotionScores = faces[0].FaceAttributes.Emotion;
                    var           emotion       = emotionScores.ToRankedList().First();
                    var           facialHair    = attributes.FacialHair;
                    var           headPose      = attributes.HeadPose;
                    var           glasses       = attributes.Glasses;
                    txtid.Text      = "Id:- " + id.ToString();
                    txtage.Text     = "Age:- " + age.ToString() + " Years";
                    txtgender.Text  = "Gender:- " + gender.ToUpper();
                    txtemotion.Text = "Emotion:- " + emotion.Key.ToString();
                    txtglass.Text   = "Glasses:- " + glasses.ToString();
                    Addtodb(txtid.Text, txtage.Text, txtgender.Text, txtemotion.Text, txtglass.Text);
                }
                catch (FaceAPIException ex)
                {
                    var dialog = new MessageDialog(ex.ErrorMessage);
                    dialog.Title = ex.ErrorCode;
                    await dialog.ShowAsync();
                }
                catch (Exception exp)
                {
                    var dialog = new MessageDialog(exp.Message);
                    dialog.Title = exp.Source;
                    await dialog.ShowAsync();
                }
            }
        }
Esempio n. 16
0
        public static Emotion GetBestEmotion(this EmotionScores emotions)
        {
            var bestEmotion = emotions?.ToRankedList().FirstOrDefault().Key ?? nameof(EmotionScores.Neutral);

            return((Emotion)Enum.Parse(typeof(Emotion), bestEmotion, true));
        }
Esempio n. 17
0
 /// <summary>
 /// Get the dominant emotion as string
 /// </summary>
 /// <param name="scores"></param>
 /// <returns>The dominant emotion as a string</returns>
 public static string GetDominantEmotionAsString(EmotionScores scores) => scores.ToRankedList().First().Key;
Esempio n. 18
0
        private async Task ProcessCameraCapture(ImageAnalyzer e)
        {
            if (e == null)
            {
                this.lastDetectedFaceSample         = null;
                this.lastIdentifiedPersonSample     = null;
                this.lastSimilarPersistedFaceSample = null;
                this.lastEmotionSample = null;
                this.debugText.Text    = "";
                this.isProcessingPhoto = false;
                return;
            }

            DateTime start = DateTime.Now;

            // Compute Emotion, Age and Gender
            await Task.WhenAll(e.DetectEmotionAsync(), e.DetectFacesAsync(detectFaceAttributes: true));

            if (!e.DetectedEmotion.Any())
            {
                this.lastEmotionSample = null;
                this.ShowTimelineFeedbackForNoFaces();
            }
            else
            {
                this.lastEmotionSample = e.DetectedEmotion;

                averageScores = new EmotionScores
                {
                    Happiness = e.DetectedEmotion.Average(em => em.Scores.Happiness),
                    Anger     = e.DetectedEmotion.Average(em => em.Scores.Anger),
                    Sadness   = e.DetectedEmotion.Average(em => em.Scores.Sadness),
                    Contempt  = e.DetectedEmotion.Average(em => em.Scores.Contempt),
                    Disgust   = e.DetectedEmotion.Average(em => em.Scores.Disgust),
                    Neutral   = e.DetectedEmotion.Average(em => em.Scores.Neutral),
                    Fear      = e.DetectedEmotion.Average(em => em.Scores.Fear),
                    Surprise  = e.DetectedEmotion.Average(em => em.Scores.Surprise)
                };
                this.emotionDataTimelineControl.DrawEmotionData(averageScores);
            }

            if (e.DetectedFaces == null || !e.DetectedFaces.Any())
            {
                this.lastDetectedFaceSample = null;
            }
            else
            {
                this.lastDetectedFaceSample = e.DetectedFaces;
            }

            // Compute Face Identification and Unique Face Ids
            await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());

            if (!e.IdentifiedPersons.Any())
            {
                this.lastIdentifiedPersonSample = null;
            }
            else
            {
                this.lastIdentifiedPersonSample = e.DetectedFaces.Select(f => new Tuple <Face, IdentifiedPerson>(f, e.IdentifiedPersons.FirstOrDefault(p => p.FaceId == f.FaceId)));
            }

            if (!e.SimilarFaceMatches.Any())
            {
                this.lastSimilarPersistedFaceSample = null;
            }
            else
            {
                this.lastSimilarPersistedFaceSample = e.SimilarFaceMatches;
                if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("DeviceName"))
                {
                    deviceName = ApplicationData.Current.RoamingSettings.Values["DeviceName"].ToString();
                    deviceKey  = ApplicationData.Current.RoamingSettings.Values["DeviceKey"].ToString();

                    foreach (var item in e.SimilarFaceMatches)
                    {
                        if (string.Compare(item.Face.FaceAttributes.Gender, "male", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            dataToHub.SendEmotions(averageScores.ToRankedList(), "Male", item.Face.FaceAttributes.Age.ToString("0"), deviceName, deviceKey);
                        }
                        else
                        {
                            dataToHub.SendEmotions(averageScores.ToRankedList(), "Female", item.Face.FaceAttributes.Age.ToString("0"), deviceName, deviceKey);
                        }
                    }
                }
                else
                {
                    MessageDialog dialog = new MessageDialog("Make sure your device name and key are correct", "IoT Hub Parameters missing..");
                    await dialog.ShowAsync();
                }
            }

            this.UpdateDemographics(e);

            //   this.debugText.Text = string.Format("Latency: {0}ms", (int)(DateTime.Now - start).TotalMilliseconds);

            this.isProcessingPhoto = false;
        }
Esempio n. 19
0
        private async Task ProcessCameraCapture(ImageAnalyzer e)
        {
            if (e == null)
            {
                this.lastDetectedFaceSample         = null;
                this.lastIdentifiedPersonSample     = null;
                this.lastSimilarPersistedFaceSample = null;
                this.lastEmotionSample = null;
                this.debugText.Text    = "";
                this.isProcessingPhoto = false;
                return;
            }

            DateTime start = DateTime.Now;

            // Compute Emotion, Age and Gender
            await Task.WhenAll(e.DetectEmotionAsync(), e.DetectFacesAsync(detectFaceAttributes: true));

            if (!e.DetectedEmotion.Any())
            {
                this.lastEmotionSample = null;
                this.ShowTimelineFeedbackForNoFaces();
            }
            else
            {
                this.lastEmotionSample = e.DetectedEmotion;

                EmotionScores averageScores = new EmotionScores
                {
                    Happiness = e.DetectedEmotion.Average(em => em.Scores.Happiness),
                    Anger     = e.DetectedEmotion.Average(em => em.Scores.Anger),
                    Sadness   = e.DetectedEmotion.Average(em => em.Scores.Sadness),
                    Contempt  = e.DetectedEmotion.Average(em => em.Scores.Contempt),
                    Disgust   = e.DetectedEmotion.Average(em => em.Scores.Disgust),
                    Neutral   = e.DetectedEmotion.Average(em => em.Scores.Neutral),
                    Fear      = e.DetectedEmotion.Average(em => em.Scores.Fear),
                    Surprise  = e.DetectedEmotion.Average(em => em.Scores.Surprise)
                };

                this.emotionDataTimelineControl.DrawEmotionData(averageScores);
                CommunicateWithIoTHub emotions = new CommunicateWithIoTHub();
                emotions.SendEmotions(averageScores.ToRankedList());
            }

            if (e.DetectedFaces == null || !e.DetectedFaces.Any())
            {
                this.lastDetectedFaceSample = null;
            }
            else
            {
                this.lastDetectedFaceSample = e.DetectedFaces;
            }

            // Compute Face Identification and Unique Face Ids
            await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());

            if (!e.IdentifiedPersons.Any())
            {
                this.lastIdentifiedPersonSample = null;
            }
            else
            {
                this.lastIdentifiedPersonSample = e.DetectedFaces.Select(f => new Tuple <Face, IdentifiedPerson>(f, e.IdentifiedPersons.FirstOrDefault(p => p.FaceId == f.FaceId)));
            }

            if (!e.SimilarFaceMatches.Any())
            {
                this.lastSimilarPersistedFaceSample = null;
            }
            else
            {
                this.lastSimilarPersistedFaceSample = e.SimilarFaceMatches;
            }

            this.UpdateDemographics(e);

            this.debugText.Text = string.Format("Latency: {0}ms", (int)(DateTime.Now - start).TotalMilliseconds);

            this.isProcessingPhoto = false;
        }
Esempio n. 20
0
 public static Tuple <string, float> GetDominantEmotion(EmotionScores scores)
 {
     return(scores.ToRankedList().Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).First());
 }
Esempio n. 21
0
 private Tuple <string, float> GetDominantEmotion(EmotionScores emotionScores)
 {
     return(emotionScores.ToRankedList().Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).First());
 }