Ejemplo n.º 1
0
        static async void MakeRequest(string imageFilePath)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<inset your own key here>"); //
            string uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?";
            HttpResponseMessage response;
            string responseContent;

            byte[] byteData = GetImageAsByteArray(imageFilePath);

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response = await client.PostAsync(uri, content);

                responseContent = response.Content.ReadAsStringAsync().Result;
            }

            Console.WriteLine(responseContent);
            try
            {
                JToken rootToken          = JArray.Parse(responseContent).First;
                JToken faceRectangleToken = rootToken.First;

                JToken scoresToken = rootToken.Last;

                JEnumerable <JToken> scoreList = scoresToken.First.Children();

                string angerString     = scoreList.ToArray()[0].ToString().Split(':')[1];
                string contemptString  = scoreList.ToArray()[1].ToString().Split(':')[1];
                string disgustString   = scoreList.ToArray()[2].ToString().Split(':')[1];
                string fearString      = scoreList.ToArray()[3].ToString().Split(':')[1];
                string happinessString = scoreList.ToArray()[4].ToString().Split(':')[1];
                string neutralString   = scoreList.ToArray()[5].ToString().Split(':')[1];
                string sadnessString   = scoreList.ToArray()[6].ToString().Split(':')[1];
                string surpriseString  = scoreList.ToArray()[7].ToString().Split(':')[1];

                double anger     = Double.Parse(angerString);
                double contempt  = Double.Parse(contemptString);
                double disgust   = Double.Parse(disgustString);
                double fear      = Double.Parse(fearString);
                double happiness = Double.Parse(happinessString);
                double neutral   = Double.Parse(neutralString);
                double sadness   = Double.Parse(sadnessString);
                double surprise  = Double.Parse(surpriseString);

                sadness = sadness * 0.8;

                Console.WriteLine($"anger : {anger}");
                Console.WriteLine($"contempt : {contempt}");
                Console.WriteLine($"disgust: {disgust}");
                Console.WriteLine($"fear : {fear}");
                Console.WriteLine($"happiness : {happiness}");
                Console.WriteLine($"neutral : {neutral}");
                Console.WriteLine($"sadness : {sadness}");
                Console.WriteLine($"surprise : {surprise}");

                if (anger > Math.Max(contempt, disgust) && anger > Math.Max(fear, happiness) && anger > Math.Max(sadness, surprise))
                {
                    targetColor = ColorEmotion.Anger;
                }

                if (contempt > Math.Max(anger, disgust) && contempt > Math.Max(fear, happiness) && contempt > Math.Max(sadness, surprise))
                {
                    targetColor = ColorEmotion.Contempt;
                }

                if (disgust > Math.Max(contempt, anger) && disgust > Math.Max(fear, happiness) && disgust > Math.Max(sadness, surprise))
                {
                    targetColor = ColorEmotion.Disgust;
                }

                if (fear > Math.Max(contempt, disgust) && fear > Math.Max(anger, happiness) && fear > Math.Max(sadness, surprise))
                {
                    targetColor = ColorEmotion.Fear;
                }

                if (happiness > Math.Max(contempt, disgust) && happiness > Math.Max(fear, anger) && happiness > Math.Max(sadness, surprise))
                {
                    targetColor = ColorEmotion.Happiness;
                }

                if (sadness > Math.Max(contempt, disgust) && sadness > Math.Max(fear, happiness) && sadness > Math.Max(anger, surprise))
                {
                    targetColor = ColorEmotion.Sadness;
                }

                if (surprise > Math.Max(contempt, disgust) && surprise > Math.Max(fear, happiness) && surprise > Math.Max(sadness, anger))
                {
                    targetColor = ColorEmotion.Surprise;
                }
            }
            catch { }
        }