-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
62 lines (53 loc) · 1.96 KB
/
helper.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
import time
import requests
import pyttsx
def output_audio(aud):
engine = pyttsx.init()
engine.say(aud)
engine.runAndWait()
def capture():
from io import BytesIO
from time import sleep
from picamera import PiCamera
# Create an in-memory stream
my_stream = BytesIO()
camera = PiCamera()
camera.start_preview()
# Camera warm-up time
#sleep(2)
camera.capture(my_stream, 'jpeg')
return my_stream
def processRequest( jsonObj, _url, data, headers, params):
print('Uploading...')
retries = 0
result = None
while True:
response = requests.request( 'post', _url, json = jsonObj, data = data, headers = headers, params = params )
if response.status_code == 429:
print( "Message: %s" % ( response.json()['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
elif response.status_code == 401:
print "Invalid Subscription Key"
else:
print( "Error code: %d" % ( response.status_code ) )
print( "Message: %s" % ( response.json()['message'] ) )
break
print('Request Completed!')
return result
def renderResult (result) :
descriptionText = result['description']['captions'][0]['text']
return descriptionText