forked from heechul/DeepPicar-v3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeeppicar.py
executable file
·399 lines (352 loc) · 13.6 KB
/
deeppicar.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/python
import os
import time
import atexit
import cv2
import math
import numpy as np
import sys
import params
import argparse
import array
from multiprocessing import Process, Lock, Array
from http.server import BaseHTTPRequestHandler, HTTPServer, ThreadingHTTPServer
from PIL import Image, ImageDraw
import input_stream
import json
import logging
import signal
import sys
import shutil
##########################################################
# import deeppicar's sensor/actuator modules
##########################################################
camera = __import__(params.camera)
actuator = __import__(params.actuator)
##########################################################
# global variable initialization
##########################################################
use_dnn = False
use_thread = True
view_video = False
fpv_video = False
enable_record = False
cfg_cam_res = (320, 240)
cfg_cam_fps = 30
cfg_throttle = 50 # 50% power.
frame_id = 0
angle = 0.0
period = 0.05 # sec (=50ms)
interpreter = None
input_index = None
output_index = None
finish = False
# Web stream and file handling
class stream_handler(BaseHTTPRequestHandler):
global cfg_cam_fps
streaming = True
def do_OPTIONS(self):
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Location', '/index.html')
self.end_headers()
if self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
period = 1./cfg_cam_fps
end_time = time.time() + period
try:
while stream_handler.streaming:
frame = camera.read_frame()
ret, frame = cv2.imencode('.jpg', frame)
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
tdiff = end_time - time.time()
if tdiff > 0:
time.sleep(tdiff)
end_time += period
print('streaming')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
elif self.path == '/download':
shutil.make_archive('./Dataset', 'zip', './data')
f = open('./Dataset.zip', 'rb')
self.send_response(200)
self.send_header('Content-Type', 'application/zip')
self.end_headers()
self.wfile.write(f.read())
f.close()
else:
self.send_error(404)
self.end_headers()
def do_POST(self):
global new_inp_type
if self.path == '/stream.mjpg':
self.send_response(201)
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
print(data)
stream_handler.streaming = data['params']['streaming']
elif self.path == '/upload':
filename = "large-200x66x3.tflite"
file_length = int(self.headers['Content-Length'])
read = 0
with open('./models/'+filename, 'wb') as output_file:
output_file.write(self.rfile.read(file_length))
self.send_response(201, 'Created')
self.end_headers()
reply_body = 'Saved "%s"\n' % filename
self.wfile.write(reply_body.encode('utf-8'))
load_model()
elif self.path == '/input_switch':
self.send_response(301)
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
new_inp_type = int(data['params']['input_type'])
else:
self.send_error(404)
self.end_headers()
##########################################################
# local functions
##########################################################
def deg2rad(deg):
return deg * math.pi / 180.0
def rad2deg(rad):
return 180.0 * rad / math.pi
def g_tick():
t = time.time()
count = 0
while True:
count += 1
yield max(t + count*period - time.time(),0)
def turn_off():
print('Finishing...')
stream_handler.streaming = False
server.server_close()
actuator.stop()
camera.stop()
cur_inp_stream.stop()
def preprocess(img):
img = img[img.shape[0]//2:]
img = cv2.resize(img, (params.img_width, params.img_height))
# Convert to grayscale and readd channel dimension
if params.img_channels == 1:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.reshape(img, (params.img_height, params.img_width, params.img_channels))
img = img / 255.
return img
def overlay_image(l_img, s_img, x_offset, y_offset):
assert y_offset + s_img.shape[0] <= l_img.shape[0]
assert x_offset + s_img.shape[1] <= l_img.shape[1]
l_img = l_img.copy()
for c in range(0, 3):
l_img[y_offset:y_offset+s_img.shape[0],
x_offset:x_offset+s_img.shape[1], c] = (
s_img[:,:,c] * (s_img[:,:,3]/255.0) +
l_img[y_offset:y_offset+s_img.shape[0],
x_offset:x_offset+s_img.shape[1], c] *
(1.0 - s_img[:,:,3]/255.0))
return l_img
def load_model():
global interpreter
global input_index
global output_index
##########################################################
# import deeppicar's DNN model
##########################################################
print ("Loading model: " + params.model_file)
try:
# Import TFLite interpreter from tflite_runtime package if it's available.
from tflite_runtime.interpreter import Interpreter
interpreter = Interpreter(params.model_file+'.tflite', num_threads=args.ncpu)
except ImportError:
# If not, fallback to use the TFLite interpreter from the full TF package.
import tensorflow as tf
interpreter = tf.lite.Interpreter(model_path=params.model_file+'.tflite', num_threads=args.ncpu)
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
def signal_handler(sig, frame):
global finish
finish = True
signal.signal(signal.SIGINT, signal_handler)
##########################################################
# program begins
##########################################################
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DeepPicar main')
parser.add_argument("-d", "--dnn", help="Enable DNN", action="store_true")
parser.add_argument("-t", "--throttle", help="throttle percent. [0-100]%", type=int)
parser.add_argument("-n", "--ncpu", help="number of cores to use.", type=int, default=1)
parser.add_argument("-f", "--hz", help="control frequnecy", type=int)
parser.add_argument("-g", "--gamepad", help="Use gamepad", action="store_true")
parser.add_argument("-w", "--web", help="Use webpage based input", action="store_true")
parser.add_argument("--fpvvideo", help="Take FPV video of DNN driving", action="store_true")
args = parser.parse_args()
if args.dnn:
print ("DNN is on")
use_dnn = True
if args.throttle:
cfg_throttle = args.throttle
print ("throttle = %d pct" % (args.throttle))
if args.hz:
period = 1.0/args.hz
print("new period: ", period)
if args.fpvvideo:
fpv_video = True
print("FPV video of DNN driving is on")
load_model()
if args.gamepad:
cur_inp_type= input_stream.input_type.GAMEPAD
elif args.web:
cur_inp_type= input_stream.input_type.WEB
else:
cur_inp_type= input_stream.input_type.KEYBOARD
new_inp_type=cur_inp_type
cur_inp_stream= input_stream.instantiate_inp_stream(cur_inp_type, cfg_throttle)
address = ('', 8001)
server = ThreadingHTTPServer(address, stream_handler)
server.timeout = 0
# initlaize deeppicar modules
actuator.init(cfg_throttle)
camera.init(res=cfg_cam_res, fps=cfg_cam_fps, threading=use_thread)
g = g_tick()
start_ts = time.time()
frame_arr = []
angle_arr = []
# enter main loop
while not finish:
if use_thread:
time.sleep(next(g))
frame = camera.read_frame()
ts = time.time()
if new_inp_type != cur_inp_type:
del cur_inp_stream
cur_inp_type= new_inp_type
cur_inp_stream= input_stream.instantiate_inp_stream(cur_inp_type, speed)
if view_video:
cv2.imshow('frame', frame)
ch = cv2.waitKey(1) & 0xFF
else:
command, direction, speed = cur_inp_stream.read_inp()
actuator.set_speed(speed)
if command == 'a':
actuator.ffw()
print ("accel")
elif command == 's':
actuator.stop()
print ("stop")
elif command == 'z':
actuator.rew()
print ("reverse")
elif command == 'r':
print ("toggle record mode")
if enable_record:
keyfile.close()
vidfile.release()
frame_id= 0
enable_record = not enable_record
elif command == 't':
print ("toggle video mode")
view_video = not view_video
elif command == 'd':
print ("toggle DNN mode")
use_dnn = not use_dnn
elif command == 'q':
finish = True
break
if use_dnn:
# 1. machine input
img = preprocess(frame)
img = np.expand_dims(img, axis=0).astype(np.float32)
interpreter.set_tensor(input_index, img)
interpreter.invoke()
angle = interpreter.get_tensor(output_index)[0][0]
action_limit = 10
if rad2deg(angle) < -action_limit:
actuator.left()
print ("left (CPU)")
elif rad2deg(angle) >= -action_limit and rad2deg(angle) <= action_limit:
actuator.center()
print ("center (CPU)")
elif rad2deg(angle) > action_limit:
actuator.right()
print ("right (CPU)")
else:
if direction < 0:
angle = deg2rad(direction * 30)
actuator.left(direction)
print ("left")
elif direction > 0:
angle = deg2rad(direction * 30)
actuator.right(direction)
print ("right")
else:
angle=0.
actuator.center()
print ("center")
dur = time.time() - ts
if dur > period:
print("%.3f: took %d ms - deadline miss."
% (ts - start_ts, int(dur * 1000)))
else:
print("%.3f: took %d ms" % (ts - start_ts, int(dur * 1000)))
if enable_record == True and frame_id == 0:
# ensure directory exists
os.makedirs(params.data_dir, exist_ok=True)
# create files for data recording
keyfile = open(params.rec_csv_file, 'w+')
keyfile.write("ts_micro,frame,wheel\n")
try:
fourcc = cv2.cv.CV_FOURCC(*'XVID')
except AttributeError as e:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vidfile = cv2.VideoWriter(params.rec_vid_file, fourcc,
cfg_cam_fps, cfg_cam_res)
if enable_record == True and frame is not None:
# increase frame_id
frame_id += 1
# write input (angle)
str = "{},{},{}\n".format(int(ts*1000), frame_id, angle)
keyfile.write(str)
if use_dnn and fpv_video:
textColor = (255,255,255)
bgColor = (0,0,0)
newImage = Image.new('RGBA', (100, 20), bgColor)
drawer = ImageDraw.Draw(newImage)
drawer.text((0, 0), "Frame #{}".format(frame_id), fill=textColor)
drawer.text((0, 10), "Angle:{}".format(angle), fill=textColor)
newImage = cv2.cvtColor(np.array(newImage), cv2.COLOR_BGR2RGBA)
frame = overlay_image(frame,
newImage,
x_offset = 0, y_offset = 0)
# write video stream
vidfile.write(frame)
#img_name = "cal_images/opencv_frame_{}.png".format(frame_id)
#cv2.imwrite(img_name, frame)
#if frame_id >= 1000:
# print ("recorded 1000 frames")
# break
print ("%.3f %d %.3f %d(ms)" %
(ts, frame_id, angle, int((time.time() - ts)*1000)))
server.handle_request()
turn_off()