forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python script to analyze emotion of image
58 lines (48 loc) · 1.45 KB
/
Python script to analyze emotion of image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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)