Get emotions of images using Microsoft emotion API in Python

The emotions of images like happy, sad, neutral, surprise, etc. can be extracted using Microsoft emotion API for any development purpose.

It is very simple to use and can be called via API through terminal or any of languages like Python or PHP. Microsoft provides free subscription of 30 days for making total of 30,000 requests.
The details of the end points and parameters can be found in the documentation.





# Python script to analyze
# emotion of image
import http.client, urllib.request
import urllib.parse, urllib.error
import base64, sys
import simplejson as json
# replace with subscription_key
# you obtained after registration
subscription_key = '12f29133caf4406493e81b6a31c47c1a'
headers = {
    # Request headers. Replace
    # the placeholder key
    # below with your
    # subscription key.
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscription_key,
params = urllib.parse.urlencode({
# Replace the URL
# below with the
# URL of the image
# you want to analyze.
body = { 'url': url1 }
newbody =str(body)
    # NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
    # For example, if you obtained your subscription keys from westcentralus, replace "westus" in the
    # URL below with "westcentralus".
    conn = http.client.HTTPSConnection('')
    conn.request("POST", "/emotion/v1.0/recognize?%s" % params, newbody, headers)
    response = conn.getresponse()
    data =
    parsed = json.loads(data)
    print ("Response:")
    print (json.dumps(parsed, sort_keys=True, indent=2))
    # the emotion of image
    # will the max value of
    # any emotion obtained
    # from the different
    # scores of each emotion
    val = parsed[0]["scores"]
    res = max(val, key = val.get)
    print ("\nEmotion :: ",res)
except Exception as e:


The sample project using this api is available on SnapLook

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.