forked from dctian/DeepPiCar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects_on_road_processor.py
More file actions
247 lines (204 loc) · 9.26 KB
/
Copy pathobjects_on_road_processor.py
File metadata and controls
247 lines (204 loc) · 9.26 KB
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
import cv2
import logging
import datetime
import time
import edgetpu.detection.engine
from PIL import Image
from traffic_objects import *
_SHOW_IMAGE = False
#ssd_mobilenet_v2 model: road_signs_quantized_edgetpu.tflite
class ObjectsOnRoadProcessor(object):
"""
This class 1) detects what objects (namely traffic signs and people) are on the road
and 2) controls the car navigation (speed/steering) accordingly
"""
def __init__(self,
car=None,
speed_limit=40,
model='/home/pi/DeepPiCar/models/object_detection/data/model_result/road_signs_quantized_edgetpu.tflite',
label='/home/pi/DeepPiCar/models/object_detection/data/model_result/road_sign_labels.txt',
width=640,
height=480):
# model: This MUST be a tflite model that was specifically compiled for Edge TPU.
# https://coral.withgoogle.com/web-compiler/
logging.info('Creating a ObjectsOnRoadProcessor...')
self.width = width
self.height = height
# initialize car
self.car = car
self.speed_limit = speed_limit
self.speed = speed_limit
# initialize TensorFlow models
with open(label, 'r') as f:
pairs = (l.strip().split(maxsplit=1) for l in f.readlines())
self.labels = dict((int(k), v) for k, v in pairs)
# initial edge TPU engine
logging.info('Initialize Edge TPU with model %s...' % model)
self.engine = edgetpu.detection.engine.DetectionEngine(model)
self.min_confidence = 0.30
self.num_of_objects = 3
logging.info('Initialize Edge TPU with model done.')
# initialize open cv for drawing boxes
self.font = cv2.FONT_HERSHEY_SIMPLEX
self.bottomLeftCornerOfText = (10, height - 10)
self.fontScale = 1
self.fontColor = (255, 255, 255) # white
self.boxColor = (0, 0, 255) # RED
self.boxLineWidth = 1
self.lineType = 2
self.annotate_text = ""
self.annotate_text_time = time.time()
self.time_to_show_prediction = 1.0 # ms
#
self.traffic_objects = {0: GreenTrafficLight(),
1: Person(),
2: RedTrafficLight(),
3: SpeedLimit(25),
4: SpeedLimit(40),
5: StopSign()}
def process_objects_on_road(self, frame):
# Main entry point of the Road Object Handler
logging.debug('Processing objects.................................')
objects, final_frame = self.detect_objects(frame)
self.control_car(objects)
logging.debug('Processing objects END..............................')
return final_frame
def control_car(self, objects):
logging.debug('Control car...')
car_state = {"speed": self.speed_limit, "speed_limit": self.speed_limit}
if len(objects) == 0:
logging.debug('No objects detected, drive at speed limit of %s.' % self.speed_limit)
contain_stop_sign = False
for obj in objects:
obj_label = self.labels[obj.label_id]
processor = self.traffic_objects[obj.label_id]
if processor.is_close_by(obj, self.height):
processor.set_car_state(car_state)
else:
logging.debug("[%s] object detected, but it is too far, ignoring. " % obj_label)
if obj_label == 'Stop':
contain_stop_sign = True
if not contain_stop_sign:
self.traffic_objects[5].clear()
self.resume_driving(car_state)
def resume_driving(self, car_state):
old_speed = self.speed
self.speed_limit = car_state['speed_limit']
self.speed = car_state['speed']
if self.speed == 0:
self.set_speed(0)
else:
self.set_speed(self.speed_limit)
logging.debug('Current Speed = %d, New Speed = %d' % (old_speed, self.speed))
if self.speed == 0:
logging.debug('full stop for 1 seconds')
time.sleep(1)
def set_speed(self, speed):
# Use this setter, so we can test this class without a car attached
self.speed = speed
if self.car is not None:
logging.debug("Actually setting car speed to %d" % speed)
self.car.back_wheels.speed = speed
############################
# Frame processing steps
############################
def detect_objects(self, frame):
logging.debug('Detecting objects...')
# call tpu for inference
start_ms = time.time()
frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(frame_RGB)
objects = self.engine.DetectWithImage(img_pil, threshold=self.min_confidence, keep_aspect_ratio=True,
relative_coord=False, top_k=self.num_of_objects)
if objects:
for obj in objects:
height = obj.bounding_box[1][1]-obj.bounding_box[0][1]
width = obj.bounding_box[1][0]-obj.bounding_box[0][0]
logging.debug("%s, %.0f%% w=%.0f h=%.0f" % (self.labels[obj.label_id], obj.score * 100, width, height))
box = obj.bounding_box
coord_top_left = (int(box[0][0]), int(box[0][1]))
coord_bottom_right = (int(box[1][0]), int(box[1][1]))
cv2.rectangle(frame, coord_top_left, coord_bottom_right, self.boxColor, self.boxLineWidth)
annotate_text = "%s %.0f%%" % (self.labels[obj.label_id], obj.score * 100)
coord_top_left = (coord_top_left[0], coord_top_left[1] + 15)
cv2.putText(frame, annotate_text, coord_top_left, self.font, self.fontScale, self.boxColor, self.lineType)
else:
logging.debug('No object detected')
elapsed_ms = time.time() - start_ms
annotate_summary = "%.1f FPS" % (1.0/elapsed_ms)
logging.debug(annotate_summary)
cv2.putText(frame, annotate_summary, self.bottomLeftCornerOfText, self.font, self.fontScale, self.fontColor, self.lineType)
#cv2.imshow('Detected Objects', frame)
return objects, frame
############################
# Utility Functions
############################
def show_image(title, frame, show=_SHOW_IMAGE):
if show:
cv2.imshow(title, frame)
############################
# Test Functions
############################
def test_photo(file):
object_processor = ObjectsOnRoadProcessor()
frame = cv2.imread(file)
combo_image = object_processor.process_objects_on_road(frame)
show_image('Detected Objects', combo_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def test_stop_sign():
# this simulates a car at stop sign
object_processor = ObjectsOnRoadProcessor()
frame = cv2.imread('/home/pi/DeepPiCar/driver/data/objects/stop_sign.jpg')
combo_image = object_processor.process_objects_on_road(frame)
show_image('Stop 1', combo_image)
time.sleep(1)
frame = cv2.imread('/home/pi/DeepPiCar/driver/data/objects/stop_sign.jpg')
combo_image = object_processor.process_objects_on_road(frame)
show_image('Stop 2', combo_image)
time.sleep(2)
frame = cv2.imread('/home/pi/DeepPiCar/driver/data/objects/stop_sign.jpg')
combo_image = object_processor.process_objects_on_road(frame)
show_image('Stop 3', combo_image)
time.sleep(1)
frame = cv2.imread('/home/pi/DeepPiCar/driver/data/objects/green_light.jpg')
combo_image = object_processor.process_objects_on_road(frame)
show_image('Stop 4', combo_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def test_video(video_file):
object_processor = ObjectsOnRoadProcessor()
cap = cv2.VideoCapture(video_file + '.avi')
# skip first second of video.
for i in range(3):
_, frame = cap.read()
video_type = cv2.VideoWriter_fourcc(*'XVID')
date_str = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
video_overlay = cv2.VideoWriter("%s_overlay_%s.avi" % (video_file, date_str), video_type, 20.0, (320, 240))
try:
i = 0
while cap.isOpened():
_, frame = cap.read()
cv2.imwrite("%s_%03d.png" % (video_file, i), frame)
combo_image = object_processor.process_objects_on_road(frame)
cv2.imwrite("%s_overlay_%03d.png" % (video_file, i), combo_image)
video_overlay.write(combo_image)
cv2.imshow("Detected Objects", combo_image)
i += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
video_overlay.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format='%(levelname)-5s:%(asctime)s: %(message)s')
# These processors contains no state
test_photo('/home/pi/DeepPiCar/driver/data/objects/red_light.jpg')
test_photo('/home/pi/DeepPiCar/driver/data/objects/person.jpg')
test_photo('/home/pi/DeepPiCar/driver/data/objects/limit_40.jpg')
test_photo('/home/pi/DeepPiCar/driver/data/objects/limit_25.jpg')
test_photo('/home/pi/DeepPiCar/driver/data/objects/green_light.jpg')
test_photo('/home/pi/DeepPiCar/driver/data/objects/no_obj.jpg')
# test stop sign, which carries state
test_stop_sign()