Things used in this project

Software apps and online services:
Vs2015logo
Microsoft Visual Studio 2015
W9gt7hzo
Microsoft Azure

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 BodyC#
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 BodyC#
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

418553
Achindra Bhatnagar
18 projects • 83 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog