private async Task GetEmotion(string imageUri) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "Key_Bing_Emotion_API"); // Request parameters var uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?" + queryString; EmotionRequest request = new EmotionRequest(); request.url = imageUri; byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await client.PostAsync(uri, content); var json = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { List <EmotionResponse> emotionResponse = JsonConvert.DeserializeObject <List <EmotionResponse> >(json); if (emotionResponse != null && emotionResponse.Count > 0) { var scores = emotionResponse[0].scores; Dictionary <string, double> dScores = new Dictionary <string, double>(); dScores.Add("anger", scores.anger); dScores.Add("contempt", scores.contempt); dScores.Add("disgust", scores.disgust); dScores.Add("fear", scores.fear); dScores.Add("happiness", scores.happiness); dScores.Add("neutral", scores.neutral); dScores.Add("sadness", scores.sadness); dScores.Add("surprise", scores.surprise); var highestScore = dScores.Values.OrderByDescending(score => score).First(); //probably a more elegant way to do this. var highestEmotion = dScores.Keys.First(key => dScores[key] == highestScore); await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action( () => { this.MySpeechSentiment.Text = $"Emotion: {highestEmotion},"; this.MySpeechSentimentConfidence.Text = $"confidence: {Convert.ToInt16(highestScore * 100)}%"; })); // await // this.Speak( // $"I'm {Convert.ToInt16(highestScore * 100)}% sure that this person's emotion is {highestEmotion}"); } else { //await // this.Speak( // $"I'm not able to get the emotion, sorry."); } } else { await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action(() => { this.MySpeechSentiment.Text = "Could not get emotion from this image"; })); //await // this.Speak( // $"Could not get emotion from this image."); } } }
private async Task GetEmotion(string imageUri) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "COPY-KEY-HERE"); queryString["returnFaceAttributes"] = "emotion"; // Request parameters //var uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?" + queryString; var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?" + queryString; EmotionRequest request = new EmotionRequest(); request.url = imageUri; byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await client.PostAsync(uri, content); var json = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var faces = JsonConvert.DeserializeObject <List <Face> >(json); if (faces != null && faces.Count > 0) { var scores = faces[0].FaceAttributes.Emotion; Dictionary <string, double> dScores = new Dictionary <string, double>(); dScores.Add("anger", scores.Anger); dScores.Add("contempt", scores.Contempt); dScores.Add("disgust", scores.Disgust); dScores.Add("fear", scores.Fear); dScores.Add("happiness", scores.Happiness); dScores.Add("neutral", scores.Neutral); dScores.Add("sadness", scores.Sadness); dScores.Add("surprise", scores.Surprise); var highestScore = dScores.OrderByDescending(score => score.Value).First(); await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action( () => { this.MySpeechSentiment.Text = $"Emotion: {highestScore.Key},"; this.MySpeechSentimentConfidence.Text = $"confidence: {Convert.ToInt16(highestScore.Value * 100)}%"; })); await this.Speak( $"I'm {Convert.ToInt16(highestScore.Value * 100)}% sure that this person's emotion is {highestScore.Key}"); } else { await this.Speak( $"I'm not able to get the emotion, sorry."); } } else { await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action(() => { this.MySpeechSentiment.Text = "Could not get emotion from this image"; })); await this.Speak( $"Could not get emotion from this image."); } } }