Achindra Bhatnagar
Published © MIT

Azure Cognitive Service Emotion API

Discover seven emotions in an image or video with Azure Cognitive Services.

IntermediateProtip4 hours2,107
Azure Cognitive Service Emotion API

Things used in this project

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Microsoft Azure
Microsoft Azure

Story

Read more

Code

Emotions as JSON Output

JSON
[
  {
    "faceRectangle": {
      "left": 68,
      "top": 97,
      "width": 64,
      "height": 97
    },
    "scores": {
      "anger": 0.00300731952,
      "contempt": 5.14648448E-08,
      "disgust": 9.180124E-06,
      "fear": 0.0001912825,
      "happiness": 0.9875571,
      "neutral": 0.0009861537,
      "sadness": 1.889955E-05,
      "surprise": 0.008229999
    }
  }
]

Image URL in Request Body

C#
string uri = "https://api.projectoxford.ai/emotion/v1.0/recognize";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);

HttpContent content = new StringContent("{\"url\":\"" + blobUrl + "\"}", Encoding.UTF8, "application/json");

HttpResponseMessage response = await client.PostAsync(uri, content);

Image Binary Stream in Request Body

C#
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.IO;

namespace AzureFunctionGetEmotions
{
    public class FaceRectangle
    {
        public int left { get; set; }
        public int top { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Scores
    {
        public double anger { get; set; }
        public double contempt { get; set; }
        public double disgust { get; set; }
        public double fear { get; set; }
        public double happiness { get; set; }
        public double neutral { get; set; }
        public double sadness { get; set; }
        public double surprise { get; set; }
    }

    public class FaceObject
    {
        public FaceRectangle faceRectangle { get; set; }
        public Scores scores { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string SubscriptionKey = "";
            Task t = MakeRequest(SubscriptionKey);
            t.Wait();
        }

        static async Task MakeRequest(string SubscriptionKey)
        {
            List faceObjects;
            string uri = "https://api.projectoxford.ai/emotion/v1.0/recognize";
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
            
            byte[] imageData = File.ReadAllBytes(@"");
            var content = new ByteArrayContent(imageData);
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");

            HttpResponseMessage response = await httpClient.PostAsync(uri, content);
            if (response.IsSuccessStatusCode)
            {
                string result = await response.Content.ReadAsStringAsync();
                faceObjects = JsonConvert.DeserializeObject(result);
                foreach(FaceObject faceObject in faceObjects)
                    Console.WriteLine(faceObject.scores.happiness.ToString());
            }
        }
    }    
}

Credits

Achindra Bhatnagar

Achindra Bhatnagar

20 projects • 126 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer

Comments