-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
46 lines (29 loc) · 1.31 KB
/
sentiment.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
import requests
import config
_url = 'https://api.projectoxford.ai/emotion/v1.0/recognize'
def process_request(json, headers):
result = None
while True:
response = requests.request('post', _url, json=json, headers=headers)
if response.status_code == 200 or response.status_code == 201:
if 'content-length' in response.headers and int(response.headers['content-length']) == 0:
result = None
elif 'content-type' in response.headers and isinstance(response.headers['content-type'], str):
if 'application/json' in response.headers['content-type'].lower():
result = response.json() if response.content else None
elif 'image' in response.headers['content-type'].lower():
result = response.content
else:
print("Error code: %d" % (response.status_code))
print("Message: %s" % (response.json()['error']['message']))
break
return result
def analize(imgUrl):
headers = dict()
headers['Ocp-Apim-Subscription-Key'] = config.SENTIMENT_IMAGE_KEY
headers['Content-Type'] = 'application/json'
json = {'url': imgUrl}
result = process_request(json, headers)
return result
def analyze_multiple(imgs):
return [analize(img) for img in imgs]