forked from log0/video_streaming_with_flask_example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.py
executable file
·241 lines (189 loc) · 5.67 KB
/
main2.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
###############################################################################
#
# Project : Video Streaming with Flask
# Author : Peter Guan, Xu Guo
# Date : 11/11/2015
#
# Description:
# Modified to support streaming out with webcams, and not just raw JPEGs.
# Most of the code credits to Miguel Grinberg.
# Credits : http://blog.miguelgrinberg.com/post/video-streaming-with-flask
# http://www.chioka.in/
#
#
###############################################################################
from __future__ import print_function
from flask import Flask, render_template, Response, send_from_directory, make_response, request, current_app
import smbus
import datetime
import time
from functools import update_wrapper
from datetime import timedelta
from mpu6050 import MPU6050
# for RPI version 1, use "bus = smbus.SMBus(0)"
xbus = smbus.SMBus(1)
tmp_address = 0x68 # This is the address value read via the i2cdetect command
bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
# This is the address we setup in the Arduino Program
address = 0x04
flag = 0
serverCmd = {'forward': 1,
'backward': 2,
'turnleft': 3,
'turnright': 4,
'stop': 5,
'request': 6
}
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
def writeNumber(value):
global flag
try:
xbus.write_byte(address, value)
except IOError:
subprocess.call('i2cdetect', '-y', '1')
flag = 1
return -1
def readNumber():
try:
num = xbus.read_byte(address)
except IOError:
# :subprocess.call('i2cdetect', '-y', '1')
print("io error")
flag = 1
return -1
print("num is {0}".format(num))
return num
app = Flask(__name__, static_url_path='')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/css/<path:path>')
def send_js(path):
return send_from_directory('css', path)
@app.route('/index2')
def index2():
return render_template('index2.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
def processRes(response):
if response is not None:
print(response)
return "True"
return "False"
@app.route('/startup')
@crossdomain(origin='*')
def startup():
# TODO call 0
writeNumber(0)
response = readNumber()
return processRes(response)
@app.route('/stop')
@crossdomain(origin='*')
def stop():
# TODO call 1
writeNumber(1)
response = readNumber()
return processRes(response)
@app.route('/tinyleft')
@crossdomain(origin='*')
def tinyleft():
# TODO call 8
writeNumber(7)
response = readNumber()
return processRes(response)
@app.route('/tinyright')
@crossdomain(origin='*')
def tinyright():
# TODO call 9
print("tiny right")
writeNumber(8)
response = readNumber()
return processRes(response)
@app.route('/left')
@crossdomain(origin='*')
def left():
# TODO call 3
writeNumber(2)
response = readNumber()
return processRes(response)
@app.route('/right')
@crossdomain(origin='*')
def right():
# TODO call 4
writeNumber(3)
response = readNumber()
return processRes(response)
@app.route('/speedup')
@crossdomain(origin='*')
def speedup():
# TODO call 5
writeNumber(4)
response = readNumber()
return processRes(response)
@app.route('/speeddown')
@crossdomain(origin='*')
def speeddown():
# TODO call 6
writeNumber(5)
response = readNumber()
return processRes(response)
@app.route('/backward')
@crossdomain(origin='*')
def backward():
# TODO call 6
writeNumber(6)
response = readNumber()
return processRes(response)
@app.route('/forward')
@crossdomain(origin='*')
def forward_world():
print("request start")
return "ok"
@app.route('/temp')
@crossdomain(origin='*')
def get_temp():
mpu = MPU6050(bus, tmp_address, "MPU6050")
mpu.read_all()
temp = mpu.read_temp()
print("temp is {0}".format(temp))
return str(temp)
if __name__ == '__main__':
app.run(host='10.1.0.233', port=5001, debug=True)
# app.run(host='0.0.0.0', port=5001, debug=True)