/// <summary> /// Sending the picture synchrone to the Azure FaceAPI and parse the response into the FaceModel /// </summary> /// <param name="imageStream"></param> /// <returns>multiple FaceModels (one for each face)</returns> private FaceModel[] SendPictureForAnalyseSync(MemoryStream imageStream) { HttpResponseMessage response = BuildClient().PostAsync(BuildUri(), buildContent(imageStream)).GetAwaiter().GetResult(); string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); FaceModel[] result = null; result = FaceModel.FromJson(responseString); return(result); }
/// <summary> /// Extracts the output text from one given FaceModel and outputs the most 3 emotions and the age /// </summary> /// <param name="fm"></param> /// <returns></returns> private string ExtractFacemodelText(FaceModel fm) { Emotion emotion = fm.FaceAttributes.Emotion; List <EmotionElement> emotions = new List <EmotionElement>(); emotions.Add(new EmotionElement("Anger", emotion.Anger)); emotions.Add(new EmotionElement("Contemp", emotion.Contempt)); emotions.Add(new EmotionElement("Disgust", emotion.Disgust)); emotions.Add(new EmotionElement("Fear", emotion.Fear)); emotions.Add(new EmotionElement("Happiness", emotion.Happiness)); emotions.Add(new EmotionElement("Neutral", emotion.Neutral)); emotions.Add(new EmotionElement("Sadness", emotion.Sadness)); emotions.Add(new EmotionElement("Surprise", emotion.Surprise)); emotions.Sort(delegate(EmotionElement a, EmotionElement b) { if (a.EmotionValue < b.EmotionValue) { return(1); } if (a.EmotionValue > b.EmotionValue) { return(-1); } return(0); }); string result = $"Age: {fm.FaceAttributes.Age}\n"; for (int i = 0; i <= 2; i++) { if (emotions[i].EmotionValue >= 0.1) { result += $"{emotions[i].EmotionName}: {emotions[i].EmotionValue}\n"; } } return(result); }
void PostAsyncCallback(HttpResponseMessage response) { string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); faceResults = FaceModel.FromJson(responseString); }