/
server.py
139 lines (119 loc) · 3.74 KB
/
server.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
from flask import Flask, render_template
import requests
import gopigo
import time
import picamera
from gtts import gTTS
import os
import argparse
import base64
import json
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
camera = picamera.PiCamera()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('homepage.html')
@app.route('/label')
def label():
print('labeling picture')
credentials = GoogleCredentials.get_application_default()
service = discovery.build('vision', 'v1', credentials=credentials)
with open('label.jpg', 'rb') as image:
image_content = base64.b64encode(image.read())
service_request = service.images().annotate(body={
'requests': [{
'image': {
'content': image_content.decode('UTF-8')
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 2
}]
}]
})
response = service_request.execute()
def parse_response(response):
label = json.dumps(response, indent=4, sort_keys=True)
items = json.loads(label)['responses'][0]['labelAnnotations']
item = items[0]['description']
item2 = items[1]['description']
print("Welcome! We are identifying the item now")
print
print
print
print("This is likely a " + item)
print
print("This is likely a " + item2)
print
print("Have a good day!")
print("______________________________________________________")
print
string = item+ " or a " +item2
tts = gTTS("Welcome! We are identifying this now. This is likely a" + string, lang='en')
tts.save("label.mp3")
os.system("mpg321 label.mp3")
with open("label.json", "w") as response2:
json.dump(response, response2)
parse_response(response)
return render_template('label.html')
@app.route('/dutch')
def dutch():
print("dutch")
tts=gTTS(text='Please take my vest off! I want to be pet!', lang='en')
tts.save("dutch.mp3")
@app.route('/picture')
def picture():
print("Taking Picture")
camera.capture('label.jpg')
print("Picture taken")
return render_template('picture.html')
@app.route('/forward')
def forward():
print("Forward!")
gopigo.fwd() # Send the GoPiGo Forward
time.sleep(1) # for 1 second.
gopigo.stop() # the stop the GoPiGo
return render_template('forward.html')
@app.route('/backward')
def backward():
print("Backward!")
gopigo.bwd() # Send the GoPiGo Backward
time.sleep(1) # for 1 second
gopigo.stop() # and then stop the GoPiGo.
return render_template('backward.html')
@app.route('/left')
def left():
print("Left!")
gopigo.left()
time.sleep(1)
gopigo.stop()
return render_template('left.html')
@app.route('/right')
def right():
print("Right!")
gopigo.right()
time.sleep(1)
gopigo.stop()
return render_template('right.html')
@app.route('/dance')
def dance():
print("Dance!")
for each in range(0,5):
gopigo.right()
time.sleep(0.25)
gopigo.left()
time.sleep(0.25)
gopigo.bwd()
time.sleep(0.25)
gopigo.stop()
return 'Dance!'
@app.route('/hello/<name>')
def hello(name):
return render_template('name.html', name=name)
@app.route('/<command>')
def command(command):
return render_template('command.html', command=command)
if __name__ == '__main__':
app.run(host='0.0.0.0', port='4000')