Use This Python Code and Microsoft Azure Detect Emotion

September 16, 2018 5:20 pm Published by Leave your thoughts

A while ago, I used a collection of DC and Marvel film posters , the Microsoft Azure engine and Python to compare emotion between the two comic book based universes.

I wanted to use this post to support those who are looking to do a similar thing.

Microsoft Azure Emotion API …

… takes a facial expression in an image as an input, and returns the confidence across a set of emotions for each face in the image, as well as bounding box for the face, using the Face API. If a user has already called the Face API, they can submit the face rectangle as an optional input.

The emotions detected are anger, contempt, disgust, fear, happiness, neutral, sadness and surprise. These emotions are understood to be communicated universally across cultures with particular facial expressions.

If you are looking for a one off detection and analysis you can use their website to ‘browse’ for an image.

Note that as Face API now integrates emotion recognition capability in general availability, Microsoft are deprecating Emotion API preview on 15 February 2019 for existing customers. You can check out the Face API details here.

On a side note, I wonder how many applications change the state of behaviour based on the return from the Emotion and / or Face API?

Anyway, back to task.

Step 1:

You’ll need a Microsoft Azure account. These are free for a trial period or at a cost for a license.

Step 2:

Gather as many images as possible and store them within a folder. In my case, I named one folder: Marvel and another folder: DC. I then used sub-folders for phases and years. This would then allow me to look at sub-categories. Add these as URLS (Saves space!) within a notepad document with the extension .txt.

Step 3:

It’s now necessary to gather as many different images for each film as possible. This is due to the fact that certain posters may not contain a character face, therefore rendering the system unable to detect a face, or posters designed for one-off themes or audiences. Collecting a larger sample of posters will ensure an increase in accuracy.

Step 4:

The sample code from the API, in Python

 

##### Python 2.7 #############
import httplib, urllib, base64

headers = {

# Request headers
‘Content-Type’: ‘application/json’,
‘Ocp-Apim-Subscription-Key’: ‘{subscription key}’,
}

params = urllib.urlencode({
})

 

try:
conn = httplib.HTTPSConnection(
‘westus.api.cognitive.microsoft.com’)
conn.request(
“POST”, “/emotion/v1.0/recognize?%s” % params, “{body}”, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(
“[Errno {0}] {1}”.format(e.errno, e.strerror))

 

####################################

 

########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {

# Request headers
‘Content-Type’: ‘application/json’,
‘Ocp-Apim-Subscription-Key’: ‘{subscription key}’,
}

params = urllib.parse.urlencode({
})

 

try:
conn = http.client.HTTPSConnection(
‘westus.api.cognitive.microsoft.com’)
conn.request(
“POST”, “/emotion/v1.0/recognize?%s” % params, “{body}”, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(
“[Errno {0}] {1}”.format(e.errno, e.strerror))

 

####################################

You’ll need to make some changes to this code.

I wanted to change the Python code to go through each of the posters and to average out the overall output for each emotion that was detected.

You’ll need to edit 1) The filename that contains the poster URLs. 2) You’ll be able to gain the subscription key from the Emotion API page, which connects to your account. You’ll need to make sure that you use this within the trial period, if you don’t want to purchase a license. Add this where it says ADD KEY HERE.

and that’s it 🙂 Enjoy.

Python 2.7 code – with a loop, using filenames within text document.

import httplib, urllib, base64, json, ast, time
angerlst = []
contemptlst = []
disgustlst = []
fearlst = []
happinesslst = []
neutrallst = []
sadnesslst = []
surpriselst = []
counter = 1

with open(‘NAMEOFFILE.txt’) as fp:

body = “”
for line in fp:
print counter

headers = {
‘Content-Type’: ‘application/json’,
‘Ocp-Apim-Subscription-Key’: ‘ADD YOUR KEY HERE’,
}

params = urllib.urlencode({
})

body = “{ ‘url’: ‘” +line+”‘ }”

conn = httplib.HTTPSConnection(‘westus.api.cognitive.microsoft.com’)
conn.request(“POST”, “/emotion/v1.0/recognize?%s” % params, body, headers)
response = conn.getresponse()
data = response.read()
b = data.decode(“utf-8”) #bytes to string conversion
c = ast.literal_eval(b) #string to list conversion

anger= (c[0][‘scores’][‘anger’]) #parsing
#print “anger ” + str(anger)
angerlst.append(anger)

contempt= (c[0][‘scores’][‘contempt’]) #parsing
contemptlst.append(contempt)

disgust= (c[0][‘scores’][‘disgust’]) #parsing
disgustlst.append(disgust)

fear= (c[0][‘scores’][‘fear’]) #parsing
fearlst.append(fear)

happiness= (c[0][‘scores’][‘happiness’]) #parsing
happinesslst.append(happiness)

neutral= (c[0][‘scores’][‘neutral’]) #parsing
neutrallst.append(neutral)

sadness= (c[0][‘scores’][‘sadness’]) #parsing
sadnesslst.append(sadness)

surprise= (c[0][‘scores’][‘surprise’]) #parsing
surpriselst.append(surprise)

conn.close()
counter +=1
if counter %19 == 0:
time.sleep(59)

print (“anger: “,(reduce(lambda x, y: x + y, angerlst) / len(angerlst)))
print (“contempt: “,(reduce(lambda x, y: x + y, contemptlst) / len(contemptlst)))
print (“disgust: “,(reduce(lambda x, y: x + y, disgustlst) / len(disgustlst)))
print (“fear: “,(reduce(lambda x, y: x + y, fearlst) / len(fearlst)))
print (“happiness: “,(reduce(lambda x, y: x + y, happinesslst) / len(happinesslst)))
print (“neutral: “,(reduce(lambda x, y: x + y, neutrallst) / len(neutrallst)))
print (“sadness: “,(reduce(lambda x, y: x + y, sadnesslst) / len(sadnesslst)))
print (“surprise: “,(reduce(lambda x, y: x + y, surpriselst) / len(surpriselst)))

Please follow and like us:
error
Tags: , , , , ,

Categorised in: , ,

This post was written by noxford

Leave a Reply