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. url1 = 'IMAGE URL TO BE ADDED HERE'body = { 'url': url1 } newbody =str(body) try: # 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('westus.api.cognitive.microsoft.com') conn.request("POST", "/emotion/v1.0/recognize?%s" % params, newbody, headers) response = conn.getresponse() data = response.read() 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) conn.close() except Exception as e: print(e.args) |
The sample project using this api is available on SnapLook
Recommended Posts:
- Emotion classification using NRC Lexicon in Python
- Movie recommendation based on emotion in Python
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Python | Create video using multiple images using OpenCV
- Python | Get a set of places according to search query using Google Places API
- Python | Get a google map image of specified location using Google Static Maps API
- Erosion and Dilation of images using OpenCV in python
- Addition and Blending of images using OpenCV in Python
- Python | Denoising of colored images using opencv
- How to download Google Images using Python
- Python | Grayscaling of Images using OpenCV
- Python | Working with PNG Images using Matplotlib
- Python | Copy and Paste Images onto other Image using Pillow
- Apply changes to all the images in given folder - Using Python PIL
- Python - Process images of a video using OpenCV
- Working with Images in Python using Matplotlib
- Drawing with Mouse on Images using Python-OpenCV
- Measure similarity between images using Python-OpenCV
- Concatenate images using OpenCV in Python
- How to compress images using Python and PIL?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

