-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemotion.py
59 lines (53 loc) · 1.58 KB
/
emotion.py
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
58
59
import httplib, urllib, base64
import sys
import re
import base64
import numpy as np
def emotion_detection(img_data):
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '604dccf2aff542d180f47921c40646d3',
}
params = urllib.urlencode({
})
# try:
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params,
img_data, headers)
response = conn.getresponse()
data = response.read()
data = re.findall(r'{.{1,200}}}',data)
result = []
for element in data:
category = re.findall(r'".{1,10}"', element)
#allstate = re.finall(r'".{1,10}":.{1,15},', data)
num = ""
score_list = []
add = False
for char in element:
if char == ',' or char == '}':
add = False
score_list.append(float(num))
if char == '}':
break;
num = ""
if add:
num = num + "" + char
if char == ':':
add = True
max_index = score_list.index(max(score_list))
max_emotion = category[max_index]
result.append(max_emotion.replace('"', ""))
return result
conn.close()
# except Exception as e:
# print("[Errno {0}] {1}".format(e.errno, e.strerror))
####################################
'''
def main():
img_data = open(sys.argv[1], 'rb')
emotion_detection(img_data)
if __name__ == '__main__':
main()
'''