private ObservableCollection <EmoEmotion> ProcessEmotions(Microsoft.ProjectOxford.Common.Contract.EmotionScores scores, EmoFace emoFace) { var emotionList = new ObservableCollection <EmoEmotion>(); var properties = scores.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); // var filterProperties = properties.Where(p => p.PropertyType == typeof(float)); var filterProperties = from prop in properties where prop.PropertyType == typeof(float) select prop; var emotype = EmoEmotionEnum.Untertermind; foreach (var propertie in filterProperties) { if (!Enum.TryParse <EmoEmotionEnum>(propertie.Name, out emotype)) { emotype = EmoEmotionEnum.Untertermind; } var emoEmotion = new EmoEmotion(); emoEmotion.Score = (float)propertie.GetValue(scores); emoEmotion.EmotionType = emotype; emoEmotion.Face = emoFace; emotionList.Add(emoEmotion); } return(emotionList); }
public static string SummarizeEmotion(Microsoft.ProjectOxford.Common.Contract.EmotionScores scores) { var bestEmotion = Aggregation.GetDominantEmotion(scores); return(string.Format("{0}: {1:N1}", bestEmotion.Item1, bestEmotion.Item2)); }
/// <summary> /// Gets the emotion. /// </summary> /// <param name="emotion">The emotion.</param> /// <returns></returns> private string GetEmotion(Microsoft.ProjectOxford.Common.Contract.EmotionScores emotion) { string emotionType = string.Empty; double emotionValue = 0.0; if (emotion.Anger > emotionValue) { emotionValue = emotion.Anger; emotionType = "Anger"; } if (emotion.Contempt > emotionValue) { emotionValue = emotion.Contempt; emotionType = "Contempt"; } if (emotion.Disgust > emotionValue) { emotionValue = emotion.Disgust; emotionType = "Disgust"; } if (emotion.Fear > emotionValue) { emotionValue = emotion.Fear; emotionType = "Fear"; } if (emotion.Happiness > emotionValue) { emotionValue = emotion.Happiness; emotionType = "Happiness"; } if (emotion.Neutral > emotionValue) { emotionValue = emotion.Neutral; emotionType = "Neutral"; } if (emotion.Sadness > emotionValue) { emotionValue = emotion.Sadness; emotionType = "Sadness"; } if (emotion.Surprise > emotionValue) { emotionValue = emotion.Surprise; emotionType = "Surprise"; } return($"{emotionType}"); }
private void _setEmotionsToInterface(Microsoft.ProjectOxford.Common.Contract.EmotionScores emotions) { HapinessTextBlock.Text = Math.Round((double)(emotions.Happiness) * 100, 4) + " %"; SadnessTextBlock.Text = Math.Round((double)(emotions.Sadness) * 100, 4) + " %"; SurpriseTextBlock.Text = Math.Round((double)(emotions.Surprise) * 100, 4) + " %"; NeutralTextBlock.Text = Math.Round((double)(emotions.Neutral) * 100, 4) + " %"; AngerTextBlock.Text = Math.Round((double)(emotions.Anger) * 100, 4) + " %"; ContemptTextBlock.Text = Math.Round((double)(emotions.Contempt) * 100, 4) + " %"; DisgustTextBlock.Text = Math.Round((double)(emotions.Disgust) * 100, 4) + " %"; FearTextBlock.Text = Math.Round((double)(emotions.Fear) * 100, 4) + " %"; var bestEmotion = emotions.ToRankedList().FirstOrDefault().Key; BestEmotionTextBlock.Text = $"You are {bestEmotion}!"; _setEmotionIcon(bestEmotion); }
/// <summary> /// Get the dominant emotion /// </summary> /// <param name="value"></param> /// <returns></returns> private string get_emotion(Microsoft.ProjectOxford.Common.Contract.EmotionScores value) { Dictionary <string, double> emotion = new Dictionary <string, double>(); emotion.Add("Anger", value.Anger); emotion.Add("Contempt", value.Contempt); emotion.Add("Disgust", value.Disgust); emotion.Add("Fear", value.Fear); emotion.Add("Happiness", value.Happiness); emotion.Add("Neutral", value.Neutral); emotion.Add("Sadness", value.Sadness); emotion.Add("Surprise", value.Surprise); // Get max value for the dominant emotion double max = emotion.Values.Max(); // Return the dominant emotion return(emotion.FirstOrDefault(x => x.Value == max).Key); }
async void DetectEmotions() { // tablica emocji - zawiera emocje dla każdej osoby na zdjęciu Emotion[] emotionResult; var storageFile = photo; // zamieniamy zdjęcie na ciąg bitów var randomAccessStream = await storageFile.OpenReadAsync(); // wysyłamy zdjęcie do Azure i dostajemy spowrotem emocje emotionResult = await emotionServiceClient.RecognizeAsync(randomAccessStream.AsStream()); // zapisujemy wyniki do zmiennej Microsoft.ProjectOxford.Common.Contract.EmotionScores score = emotionResult[0].Scores; // wpisujemy do textboxa wynik emocji w procentach zaokrąglone do 4 miejsc po przecinku mojTextbox.Text = "Your Emotions are : \n" + "Happiness: " + Math.Round((double)(score.Happiness) * 100, 4) + " %" + "\n" + "Sadness: " + Math.Round((double)(score.Sadness) * 100, 4) + " %" + "\n" + "Surprise: " + Math.Round((double)(score.Surprise) * 100, 4) + " %" + "\n" + "Neutral: " + Math.Round((double)(score.Neutral) * 100, 4) + " %" + "\n" + "Anger: " + Math.Round((double)(score.Anger) * 100, 4) + " %" + "\n" + "Contempt: " + Math.Round((double)(score.Contempt) * 100, 4) + " %" + "\n" + "Disgust: " + Math.Round((double)(score.Disgust) * 100, 4) + " %" + "\n" + "Fear: " + Math.Round((double)(score.Fear) * 100, 4) + " %" + "\n"; // Linq które wybiera najbardziej znaczący wynik, czyli emocję która ma najwięcej punktów var lista = score.ToRankedList().FirstOrDefault().Key; moj2.Text = (lista.ToString()); }
public static Tuple <string, float> GetDominantEmotion(Microsoft.ProjectOxford.Common.Contract.EmotionScores scores) { var Item = scores.ToRankedList().OrderByDescending(o => o.Value).Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).FirstOrDefault(); if (Item != null && Item.Item1 == "Neutral") { var score2 = scores.ToRankedList().OrderByDescending(o => o.Value).Take(2).Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).ToList(); if (score2 != null) { var nonWinner = score2.OrderBy(o => o.Item2).FirstOrDefault(); var Winner = score2.OrderByDescending(o => o.Item2).FirstOrDefault(); if (Winner.Item2 - nonWinner.Item2 < 0.9 && Winner.Item2 < 0.7) { return(nonWinner); } } } return(scores.ToRankedList().OrderByDescending(o => o.Value).Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).First()); }
private async void getemotion_Click(object sender, RoutedEventArgs e) { try { emotionResult = await emotionServiceClient.RecognizeAsync(imageStream.AsStream()); if (emotionResult != null) { Microsoft.ProjectOxford.Common.Contract.EmotionScores score = emotionResult[0].Scores; output.Text = "Your emotions are: \n" + "Happiness: " + score.Happiness.ToString("0.00000") + "\n" + "Sadness: " + score.Sadness.ToString("0.00000") + "\n" + "Surprise: " + score.Surprise.ToString("0.00000") + "\n" + "Fear: " + score.Fear.ToString("0.00000") + "\n" + "Anger: " + score.Anger.ToString("0.00000") + "\n" + "Disgust: " + score.Disgust.ToString("0.00000") + "\n" + "Neutral: " + score.Neutral.ToString("0.00000") + "\n"; } } catch { output.Text = "Error calling emotion"; } }
private static (EmotionValue emotionValue, double emotionScore) GetEmotionValueFromScores(Microsoft.ProjectOxford.Common.Contract.EmotionScores scores) { var highestEmotionScore = scores.ToRankedList().First(); return(emotionValue : (EmotionValue)Enum.Parse(typeof(EmotionValue), highestEmotionScore.Key), emotionScore : highestEmotionScore.Value); }
public static Tuple <string, float> GetDominantEmotion(Microsoft.ProjectOxford.Common.Contract.EmotionScores scores) { return(scores.ToRankedList().Select(kv => new Tuple <string, float>(kv.Key, kv.Value)).First()); }
/// <summary> /// Pick image, detect and identify all faces detected /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private async void Identify_Click(object sender, RoutedEventArgs e) { // Show file picker 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) { // User picked one image // Clear previous detection and identification results TargetFaces.Clear(); var pickedImagePath = dlg.FileName; var renderingImage = UIHelper.LoadImageAppliedOrientation(pickedImagePath); var imageInfo = UIHelper.GetImageInfoForRendering(renderingImage); SelectedFile = renderingImage; var sw = Stopwatch.StartNew(); MainWindow mainWindow = Window.GetWindow(this) as MainWindow; string subscriptionKey = "7f9f9eb9d73e4606bbfca54abcf93996"; string subscriptionEndpoint = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0"; var faceServiceClient = new FaceServiceClient(subscriptionKey, subscriptionEndpoint); var requiredFaceAttributes = new FaceAttributeType[] { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Smile, FaceAttributeType.Hair, FaceAttributeType.HeadPose, FaceAttributeType.Glasses, FaceAttributeType.Emotion }; // Call detection REST API using (var fStream = File.OpenRead(pickedImagePath)) { try { var faces = await faceServiceClient.DetectAsync(fStream, true, true, requiredFaceAttributes); // Convert detection result into UI binding object for rendering foreach (var face in UIHelper.CalculateFaceRectangleForRendering(faces, MaxImageSize, imageInfo)) { TargetFaces.Add(face); } MainWindow.Log("Request: Identifying {0} face(s) in group \"{1}\"", faces.Length, this.GroupId); // Identify each face // Call identify REST API, the result contains identified person information var identifyResult = await faceServiceClient.IdentifyAsync(faces.Select(ff => ff.FaceId).ToArray(), largePersonGroupId : this.GroupId); for (int idx = 0; idx < faces.Length; idx++) { // Update identification result for rendering var face = TargetFaces[idx]; var res = identifyResult[idx]; if (res.Candidates.Length > 0 && Persons.Any(p => p.PersonId == res.Candidates[0].PersonId.ToString())) { face.PersonName = Persons.Where(p => p.PersonId == res.Candidates[0].PersonId.ToString()).First().PersonName; } else { face.PersonName = "Unknown"; } } var outString = new StringBuilder(); foreach (var face in faces) { MainWindow.Log("\nFace ID : {0}", face.FaceId.ToString()); StringBuilder sb = new StringBuilder(); // Add the gender, age, and smile. sb.Append("Gender: "); sb.Append(face.FaceAttributes.Gender); sb.Append("\n "); sb.Append("Age: "); sb.Append(face.FaceAttributes.Age); sb.Append("\n "); sb.Append(String.Format("smile {0:F1}%\n ", face.FaceAttributes.Smile * 100)); // Add the emotions. Display all emotions over 10%. sb.Append("Emotion: "); Microsoft.ProjectOxford.Common.Contract.EmotionScores emotionScores = face.FaceAttributes.Emotion; if (emotionScores.Anger >= 0.1f) { sb.Append( String.Format("anger {0:F1}%, ", emotionScores.Anger * 100)); } if (emotionScores.Contempt >= 0.1f) { sb.Append( String.Format("contempt {0:F1}%, ", emotionScores.Contempt * 100)); } if (emotionScores.Disgust >= 0.1f) { sb.Append( String.Format("disgust {0:F1}%, ", emotionScores.Disgust * 100)); } if (emotionScores.Fear >= 0.1f) { sb.Append( String.Format("fear {0:F1}%, ", emotionScores.Fear * 100)); } if (emotionScores.Happiness >= 0.1f) { sb.Append( String.Format("happiness {0:F1}%, ", emotionScores.Happiness * 100)); } if (emotionScores.Neutral >= 0.1f) { sb.Append( String.Format("neutral {0:F1}%, ", emotionScores.Neutral * 100)); } if (emotionScores.Sadness >= 0.1f) { sb.Append( String.Format("sadness {0:F1}%, ", emotionScores.Sadness * 100)); } if (emotionScores.Surprise >= 0.1f) { sb.Append( String.Format("surprise {0:F1}%, ", emotionScores.Surprise * 100)); } sb.Append("\n "); // Add glasses. sb.Append(face.FaceAttributes.Glasses); sb.Append("\n "); // Add hair. sb.Append("Hair: "); var hair = face.FaceAttributes.Hair; if (hair.Bald >= 0.01f) { sb.Append(String.Format("bald {0:F1}% ", hair.Bald * 100)); } // Display all hair color attributes over 10%. var hairColors = hair.HairColor; foreach (var hairColor in hairColors) { if (hairColor.Confidence >= 0.1) { sb.Append(hairColor.Color.ToString()); sb.Append(String.Format(" {0:F1}% ", hairColor.Confidence * 100)); } } /*if (hair.HairColor.Length == 0) * { * if (hair.Invisible) * sb.Append("Invisible"); * else * sb.Append("Bald"); * } * else * { * Contract.HairColorType returnColor = Contract.HairColorType.Unknown; * double maxConfidence = 0.0f; * * for (int i = 0; i < hair.HairColor.Length; ++i) * { * if (hair.HairColor[i].Confidence > maxConfidence) * { * maxConfidence = hair.HairColor[i].Confidence; * returnColor = hair.HairColor[i].Color; * } * } * * sb.Append(returnColor.ToString()); * }*/ sb.Append("\n"); MainWindow.Log("Face Attributes : {0}", sb); MainWindow.Log("Face MouthLeft(X) : {0}", face.FaceLandmarks.MouthLeft.X); } foreach (var face in TargetFaces) { outString.AppendFormat("Face {0} is identified as {1}. ", face.FaceId, face.PersonName); } MainWindow.Log("Response: Success. {0}", outString); } catch (FaceAPIException ex) { MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage); } } } GC.Collect(); }