Previously, we kickstarted our new article series “AI HowTo” and talked how to register for Emotions API Preview (Project Oxford) through Azure Portal.
Now, I’d suggest we should start coding and learning the ways of emotions via codes.
Programming emotions
Analyzing Emotions in Images
This project requires an image to be processed at startup, makes some calculations on which emotion is stronger, and then according to that calculation, it posts the emotion on the screen. Let’s start with step by step.
Create a new Visual Studio project, preferably a console application. Install NewtonSoft JSON Parser from NuGeT command line.
- Install-Package Newtonsoft.Json
- static string ShowMeEmotion(Dictionary<string,dynamic> results)
- {
- KeyValuePair<string, dynamic> max = new KeyValuePair<string, dynamic>();
- foreach (var kvp in results)
- {
- if (kvp.Value > max.Value)
- max = kvp;
- }
- return max.Key;
- }
- static byte[] GetImageAsByteArray(string imageFilePath)
- {
- FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
- BinaryReader binaryReader = new BinaryReader(fileStream);
- return binaryReader.ReadBytes((int)fileStream.Length);
- }
- static async void MakeRequest(string imageFilePath)
- {
- var client = new HttpClient();
- client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "Update here with your own Emotion API Key");
- 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;
- }
- dynamic d = JsonConvert.DeserializeObject(responseContent);
- Dictionary<string, dynamic> emotions = new Dictionary<string, dynamic>()
- {
- {"angry", d[0].scores.anger},
- {"contempt", d[0].scores.contempt},
- {"disgust", d[0].scores.disgust},
- {"fear", d[0].scores.fear},
- {"happiness", d[0].scores.happiness},
- {"neutral", d[0].scores.neutral},
- {"sadness", d[0].scores.sadness},
- {"surprise", d[0].scores.surprise}
- };
- Console.WriteLine(ShowMeEmotion(emotions));
- }
- [STAThread]
- static void Main()
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.ShowDialog();
- string imageFilePath = ofd.FileName;
- MakeRequest(imageFilePath);
- Console.Read();
- }

That’s all!
Well done in kickstarting Azure Cognitive Services Emotions API. Remember that Emotions API(Project Oxford) is still in “Preview Stage” so not all your images are meant to work (Tried like 10 happiness emotion images and only 1 got processed).
Final thoughts
Emotion analysis is essential for all industries. We live in a world where emotions are changed instantly so if we analyze and take precautions before bad things happen, we can avoid dramas or even deaths.
There used to be emotion reading in police departments. These men/women could easily read one’s emotions in an interrogation. I don’t know if police stations have been upgraded to use AI but Artificial Intelligence can also help to avoid crime.
Emotion-reading AI spots if potential criminals are stressed or nervous, and alerts the police BEFORE they act. Read it from here.
Analyzing emotions must be a top priority for all companies and governmental organizations. For governments; to secure public life and avoid terrorist, anarchist attacks. For companies; to increase happiness among employees and solving their problems before it gets bigger.
Companies, nowadays, are trying to convince employees not to leave by increasing their wage, gifting cars, or improving working style but the companies most of the time miss the point where problems occur. Emotion Analysis may help the managers too to discover if employees are happy with the company or not.
The future is AI, use it!
<<Click here for previous part

Former memberPosted Jan 15, 2018, 10:18 AM
Awesome article and good job.
Arvind SinghPosted Jan 12, 2018, 10:51 PM
Very nice article.......