-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·132 lines (99 loc) · 4.41 KB
/
main.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
126
127
128
129
130
131
132
import base64
import logging
import requests
import time
from flask import Flask, render_template, request, jsonify, make_response
from pyarlo import PyArlo
app = Flask(__name__)
# entry point method
@app.route('/')
def arlo_snapshot():
return render_template('index.html', **locals())
# method designed to extract the config data from a file.
def read_file(filename, string=True):
with open(filename, 'rb') as input:
ciphertext = input.read()
plaintext = base64.b64decode(ciphertext)
if string:
return plaintext.decode('utf8')
# objective: method designed to connect and pass a picture to TensorPhotoXRay.
def connect_tensor_xray():
try:
arlo = PyArlo('user', read_file("pass.txt")) # connect to pyarlo library
cam = arlo.cameras[2] # selecting cam
cam.schedule_snapshot() # take picture
time.sleep(3) # wait if is necesarry
r = requests.get("url_endpoint" + cam.snapshot_url) # todo: change for a post call
return r
except Exception as e:
return [{"error"}]
def connect_tensor_xray_testing():
# todo: use if needed check a typical response.
try:
return [{"person": 97}, {"chair": 95}, {"person": 95}, {"couch": 92}, {"chair": 82}, {"tv": 81}, {"tv": 81}, {"chair": 79}, {"dog": 76}, {"book": 65}]
except Exception as e:
return [{"error"}]
# objective: Google Assistant structure method using use a particular cam, to take a picture and provide
# the picture to TensorPhotoXRay service.
@app.route('/arlo', methods=['POST'])
def tensor_photo():
try:
# connect to Arlo using PyArlo library.
req = request.get_json(silent=True, force=True)
action = req.get('result').get('action')
# detect action from DialogFlow agent description.
if action == 'image.analysis':
tags = connect_tensor_xray() # method to use the integration to TensorPhotoXRay
for element in tags:
for key, value in element.iteritems():
if "dog" in key:
# Compose the response to API.AI
res = {'speech': 'Your pet is inside your house in the main room',
'displayText': 'Your pet is inside your house in the main room',
'contextOut': req['result']['contexts']}
if res != {'speech': 'Your pet is inside your house in the main room',
'displayText': 'Your pet is inside your house in the main room',
'contextOut': 'demo'}:
res = {'speech': 'I can not find your pet at home',
'displayText': 'I can not find your pet at home',
'contextOut': 'demo'}
else:
res = {'speech': 'nothing', 'displayText': 'nothing'}
final = make_response(jsonify(res))
return final
except Exception as e:
res = {'speech': 'error', 'displayText': 'error'}
final = make_response(jsonify(res))
return final
@app.route('/arlo2', methods=['GET'])
def tensor_photo2():
try:
tags = connect_tensor_xray_testing()
for element in tags:
for key, value in element.iteritems():
if "dog" in key:
# Compose the response to API.AI
res = {'speech': 'Your pet is inside your house in the main room',
'displayText': 'Your pet is inside your house in the main room',
'contextOut': 'demo'}
if res != {'speech': 'Your pet is inside your house in the main room',
'displayText': 'Your pet is inside your house in the main room',
'contextOut': 'demo'}:
res = {'speech': 'I can not find your pet at home',
'displayText': 'I can not find your pet at home',
'contextOut': 'demo'}
final = make_response(jsonify(res))
return final
except Exception as e:
res = {'speech': 'error', 'displayText': 'error'}
final = make_response(jsonify(res))
return final
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)