-
Notifications
You must be signed in to change notification settings - Fork 40
/
picar-mini-kbd-common.py
254 lines (222 loc) · 6.95 KB
/
picar-mini-kbd-common.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
#!/usr/bin/python
import os
import time
import atexit
import cv2
import math
import numpy as np
import sys
import params
import argparse
import Image
import ImageDraw
import local_common as cm
import input_kbd
##########################################################
# 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
cfg_cam_res = (320, 240)
cfg_cam_fps = 30
cfg_throttle = 50 # 50% power.
NCPU = 2
frame_id = 0
angle = 0.0
btn = ord('k') # center
period = 0.05 # sec (=50ms)
##########################################################
# 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():
actuator.stop()
camera.stop()
keyfile.close()
keyfile_btn.close()
vidfile.release()
##########################################################
# program begins
##########################################################
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)
parser.add_argument("-f", "--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.ncpu > 0:
NCPU = args.ncpu
if args.fpvvideo:
fpv_video = True
# create files for data recording
keyfile = open('out-key.csv', 'w+')
keyfile_btn = open('out-key-btn.csv', 'w+')
keyfile.write("ts_micro,frame,wheel\n")
keyfile_btn.write("ts_micro,frame,btn,speed\n")
rec_start_time = 0
try:
fourcc = cv2.cv.CV_FOURCC(*'XVID')
except AttributeError as e:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vidfile = cv2.VideoWriter('out-video.avi', fourcc,
cfg_cam_fps, cfg_cam_res)
# initlaize deeppicar modules
actuator.init(cfg_throttle)
camera.init(res=cfg_cam_res, fps=cfg_cam_fps, threading=use_thread)
atexit.register(turn_off)
# initilize dnn model
if use_dnn == True:
print ("Load TF")
import tensorflow as tf
model = __import__(params.model)
import local_common as cm
import preprocess
print ("Load Model")
config = tf.ConfigProto(intra_op_parallelism_threads=NCPU,
inter_op_parallelism_threads=NCPU, \
allow_soft_placement=True,
device_count = {'CPU': 1})
sess = tf.InteractiveSession(config=config)
saver = tf.train.Saver()
model_load_path = cm.jn(params.save_dir, params.model_load_file)
saver.restore(sess, model_load_path)
print ("Done..")
# null_frame = np.zeros((cfg_cam_res[0],cfg_cam_res[1],3), np.uint8)
# cv2.imshow('frame', null_frame)
g = g_tick()
start_ts = time.time()
frame_arr = []
angle_arr = []
# enter main loop
while True:
if use_thread:
time.sleep(next(g))
frame = camera.read_frame()
ts = time.time()
# read a frame
# ret, frame = cap.read()
if view_video == True:
cv2.imshow('frame', frame)
ch = cv2.waitKey(1) & 0xFF
else:
ch = ord(input_kbd.read_single_keypress())
if ch == ord('j'):
actuator.left()
print ("left")
angle = deg2rad(-30)
btn = ord('j')
elif ch == ord('k'):
actuator.center()
print ("center")
angle = deg2rad(0)
btn = ord('k')
elif ch == ord('l'):
actuator.right()
print ("right")
angle = deg2rad(30)
btn = ord('l')
elif ch == ord('a'):
actuator.ffw()
print ("accel")
elif ch == ord('s'):
actuator.stop()
print ("stop")
btn = ch
elif ch == ord('z'):
actuator.rew()
print ("reverse")
elif ch == ord('q'):
break
elif ch == ord('r'):
print ("toggle record mode")
if rec_start_time == 0:
rec_start_time = ts
else:
rec_start_time = 0
elif ch == ord('t'):
print ("toggle video mode")
if view_video == False:
view_video = True
else:
view_video = False
elif ch == ord('d'):
print ("toggle DNN mode")
if use_dnn == False:
use_dnn = True
else:
use_dnn = False
if use_dnn == True:
# 1. machine input
img = preprocess.preprocess(frame)
angle = model.y.eval(feed_dict={model.x: [img]})[0][0]
car_angle = 0
degree = rad2deg(angle)
if degree < 15 and degree > -15:
actuator.center()
car_angle = 0
btn = ord('k')
elif degree >= 15:
actuator.right()
car_angle = 30
btn = ord('l')
elif degree <= -15:
actuator.left()
car_angle = -30
btn = ord('j')
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 rec_start_time > 0 and not frame is None:
# increase frame_id
frame_id += 1
# write input (angle)
str = "{},{},{}\n".format(int(ts*1000), frame_id, angle)
keyfile.write(str)
# write input (button: left, center, stop, speed)
str = "{},{},{},{}\n".format(int(ts*1000), frame_id, btn, cfg_throttle)
keyfile_btn.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(car_angle), fill=textColor)
newImage = cv2.cvtColor(np.array(newImage), cv2.COLOR_BGR2RGBA)
frame = cm.overlay_image(frame,
newImage,
x_offset = 0, y_offset = 0)
# write video stream
vidfile.write(frame)
if frame_id >= 1000:
print ("recorded 1000 frames")
break
print ("%.3f %d %.3f %d %d(ms)" %
(ts, frame_id, angle, btn, int((time.time() - ts)*1000)))
print ("Finish..")
turn_off()