-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
125 lines (95 loc) · 3.88 KB
/
test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from __future__ import print_function
import os
import time
import requests
import cv2
import operator
import numpy as np
# Import library to display results
import matplotlib.pyplot as plt
# Variables
_url = 'https://api.projectoxford.ai/emotion/v1.0/recognize'
keys = ['b17d6992258e498d9de73f7425f4a230', 'c122cf34ec4e4a689c9a7ddd64863161']
_key = 'b17d6992258e498d9de73f7425f4a230'
_key = 'c122cf34ec4e4a689c9a7ddd64863161'
_maxNumRetries = 10
def processRequest( json, data, headers, params ):
"""
Helper function to process the request to Project Oxford
Parameters:
json: Used when processing images from its URL. See API Documentation
data: Used when processing image read from disk. See API Documentation
headers: Used to pass the key information and the data type request
"""
retries = 0
result = None
while True:
response = requests.request( 'post', _url, json = json, data = data, headers = headers, params = params )
if response.status_code == 429:
print( "Message: %s" % ( response.json()['error']['message'] ) )
if retries <= _maxNumRetries:
time.sleep(1)
retries += 1
continue
else:
print( 'Error: failed after retrying!' )
break
elif 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 renderResultOnImage( result, img ):
"""Display the obtained results onto the input image"""
for currFace in result:
faceRectangle = currFace['faceRectangle']
cv2.rectangle( img,(faceRectangle['left'],faceRectangle['top']),
(faceRectangle['left']+faceRectangle['width'], faceRectangle['top'] + faceRectangle['height']),
color = (255,0,0), thickness = 5 )
for currFace in result:
faceRectangle = currFace['faceRectangle']
currEmotion = max(currFace['scores'].items(), key=operator.itemgetter(1))[0]
textToWrite = "%s" % ( currEmotion )
cv2.putText( img, textToWrite, (faceRectangle['left'],faceRectangle['top']-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 1 )
# img = os.path.expanduser('~/Development/sentiEdu/learning/confusedImgs/4.jpg')
folder = 'confusedImgs'
count = 0
data_file = open('confused.json', 'w')
for img in os.listdir(folder):
count += 1
if count < 20:
if img.endswith('.jpg'):
with open( os.path.join(folder, img), 'rb' ) as f:
data = f.read()
headers = dict()
headers['Ocp-Apim-Subscription-Key'] = _key
headers['Content-Type'] = 'application/octet-stream'
json = None
params = None
start = time.time()
print(start)
# result = processRequest( json, data, headers, params )
print('-------------')
print('request time: %s', time.time() - start)
print(os.path.join(folder, img))
# if result is not None:
# print(result)
# data_file.write(result)
print('-------------')
# Load the original image from disk
# data8uint = np.fromstring( data, np.uint8 ) # Convert string to an unsigned int array
# img = cv2.cvtColor( cv2.imdecode( data8uint, cv2.IMREAD_COLOR ), cv2.COLOR_BGR2RGB )
# renderResultOnImage( result, img )
# ig, ax = plt.subplots(figsize=(15, 20))
# ax.imshow( img )
else:
time.sleep(3)
count = 0