-
Notifications
You must be signed in to change notification settings - Fork 1
/
gobot.py
149 lines (122 loc) · 3 KB
/
gobot.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask
from flask_ask import Ask, statement, question
import serial
import cv2
import time
import requests
import base64
import json
inHand = "Nothing"
#camera functions
def save_image():
camera_port=0
ramp_frames = 30
camera = cv2.VideoCapture(camera_port)
for i in xrange(ramp_frames):
temp = get_image(camera)
camera_capture = get_image(camera)
file = "/home/linaro/Desktop/image.jpg"
cv2.imwrite(file, camera_capture)
del(camera)
def get_image(cam):
retval, im = cam.read()
return im
#POST function
def post(r):
with open("/home/linaro/Desktop/image.jpg", "rb") as f:
data = f.read()
image = data.encode("base64")
r = requests.post("http://34.201.113.132:3000/post_data", data={"img":image})
return r
#start of flask app
app = Flask(__name__)
ask = Ask(app, '/')
ser = serial.Serial("/dev/ttyUSB0", 9600)
@ask.launch
def default():
inHand = "Nothing"
ser.write(b'i') #initialize
return question("Hi, I am Alexa Go Bot!")
@ask.intent("bringObject", mapping={'Object': 'Object'})
def bringObject(Object):
global inHand
ser.write(b't')
time.sleep(5)
inHand = str(Object)
return question("The can has been picked up.")
@ask.intent("putObject", mapping={'Object': 'Object'})
def putObject(Object):
global inHand
if (Object == inHand):
ser.write('d') #put_down
inHand = "Nothing"
else: return question("I am not holding " + str(Object))
return question("The " + str(Object) + " has been put down.")
@ask.intent("turnAround")
def turnAround():
ser.write("o")
time.sleep(1)
return question("Oh, hello there.")
@ask.intent("comeBack")
def comeBack():
ser.write("q")
time.sleep(2)
return statement("Don't worry, I am right here.")
@ask.intent("backward")
def backward():
ser.write("y")
time.sleep(1)
return statement("Done")
@ask.intent("forward")
def forward():
ser.write("x")
time.sleep(1)
return statement("Done")
@ask.intent("turnLeft")
def turnLeft():
ser.write("l")
time.sleep(1)
return statement("Done")
@ask.intent("turnRight")
def turnRight():
ser.write("r")
time.sleep(1)
return statement("Done")
'''@ask.intent("captureImage")
def captureImage():
r = ""
save_image()
r = json.loads(post(r).text)["data"]
print(r[0]["name"])
if (r[0]["name"] == "Coca Cola" or r[0]["name"] == "cup" or r[0]["name"] == "bottle" or r[0]["name"] == "orange soda"):
box = r[0]["box"]
x_diff = box[3] - box[1]
x_center = box[1] + x_diff/2
x_offset = x_center - 320
if (x_diff < 0):
ser.write(b'x')
x_diff = -x_diff
ser.write(x_diff)
else:
ser.write(b'y')
if (x_offset >= 0):
ser.write(b'l')
ser.write(x_offset/5)
else:
ser.write(b'r')
x_offset = -x_offset
ser.write(x_offset/5)
return question("image has been captured and uploaded")'''
@ask.intent("patMe")
def patMe():
ser.write(b'p')
time.sleep(2)
return question("There, there, are you alright?")
@ask.intent('AMAZON.StopIntent')
def stop():
return statement("good bye!")
@ask.session_ended
def session_ended():
return "{}", 200
if __name__ == "__main__":
app.run(debug=True)