-
Notifications
You must be signed in to change notification settings - Fork 2
/
adas_cas.py
executable file
·478 lines (382 loc) · 20.5 KB
/
adas_cas.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python3
"""
The python object detection from OpenVINO demos is taken and extensively
modified to add the ADAS functionality. This module will get the imagea
and identity the bounding boxes. Then it will publish an MQTT message with
the theta min and max, label, time info on "object/getdistance" topic. The
data is decoded by other lidar_getdist.py node to get the distance of the
object and it will send a message to pulse_mqtt node to flash the warning.
The stub code is taken from the OpenVINO demos and extensively modified to
add the above functions.
Copyright (C) 2018-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import colorsys
import logging
import random
import os
import sys
from collections import deque
from argparse import ArgumentParser, SUPPRESS
from pathlib import Path
from time import perf_counter
import time
import paho.mqtt.client as mqtt
import cv2
import numpy as np
from openvino.inference_engine import IECore
# sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
sys.path.append('/home/pi/open_model_zoo/demos/common/python')
sys.path.append('/opt/intel/openvino_2021/deployment_tools/')
import models
import monitors
from pipelines import get_user_config, AsyncPipeline
from images_capture import open_images_capture
from performance_metrics import PerformanceMetrics
from helpers import resolution
logging.basicConfig(format='[ %(levelname)s ] %(message)s', level=logging.INFO, stream=sys.stdout)
log = logging.getLogger()
objLastFrames = deque(maxlen=30)
objMidsLastFrames = deque(maxlen=30)
noObjFrames = 0
# This is the Publisher
client = mqtt.Client()
client.connect("localhost", 1883, 600)
def build_argparser():
parser = ArgumentParser(add_help=False)
args = parser.add_argument_group('Options')
args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
args.add_argument('-m', '--model', help='Required. Path to an .xml file with a trained model.',
required=True, type=Path)
args.add_argument('-at', '--architecture_type', help='Required. Specify model\' architecture type.',
type=str, required=True, choices=('ssd', 'yolo', 'yolov4', 'faceboxes', 'centernet', 'ctpn',
'retinaface', 'ultra_lightweight_face_detection',
'retinaface-pytorch'))
args.add_argument('-i', '--input', required=True,
help='Required. An input to process. The input must be a single image, '
'a folder of images, video file or camera id.')
args.add_argument('-d', '--device', default='CPU', type=str,
help='Optional. Specify the target device to infer on; CPU, GPU, HDDL or MYRIAD is '
'acceptable. The demo will look for a suitable plugin for device specified. '
'Default value is CPU.')
common_model_args = parser.add_argument_group('Common model options')
common_model_args.add_argument('--labels', help='Optional. Labels mapping file.', default=None, type=str)
common_model_args.add_argument('-t', '--prob_threshold', default=0.6, type=float,
help='Optional. Probability threshold for detections filtering.')
common_model_args.add_argument('--keep_aspect_ratio', action='store_true', default=False,
help='Optional. Keeps aspect ratio on resize.')
common_model_args.add_argument('--input_size', default=(600, 600), type=int, nargs=2,
help='Optional. The first image size used for CTPN model reshaping. '
'Default: 600 600. Note that submitted images should have the same resolution, '
'otherwise predictions might be incorrect.')
infer_args = parser.add_argument_group('Inference options')
infer_args.add_argument('-nireq', '--num_infer_requests', help='Optional. Number of infer requests',
default=0, type=int)
infer_args.add_argument('-nstreams', '--num_streams',
help='Optional. Number of streams to use for inference on the CPU or/and GPU in throughput '
'mode (for HETERO and MULTI device cases use format '
'<device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>).',
default='', type=str)
infer_args.add_argument('-nthreads', '--num_threads', default=None, type=int,
help='Optional. Number of threads to use for inference on CPU (including HETERO cases).')
io_args = parser.add_argument_group('Input/output options')
io_args.add_argument('--loop', default=False, action='store_true',
help='Optional. Enable reading the input in a loop.')
io_args.add_argument('-o', '--output', required=False,
help='Optional. Name of the output file(s) to save.')
io_args.add_argument('-limit', '--output_limit', required=False, default=1000, type=int,
help='Optional. Number of frames to store in output. '
'If 0 is set, all frames are stored.')
io_args.add_argument('--no_show', help="Optional. Don't show output.", action='store_true')
io_args.add_argument('--output_resolution', default=None, type=resolution,
help='Optional. Specify the maximum output window resolution '
'in (width x height) format. Example: 1280x720. '
'Input frame size used by default.')
io_args.add_argument('-u', '--utilization_monitors', default='', type=str,
help='Optional. List of monitors to show initially.')
input_transform_args = parser.add_argument_group('Input transform options')
input_transform_args.add_argument('--reverse_input_channels', default=False, action='store_true',
help='Optional. Switch the input channels order from '
'BGR to RGB.')
input_transform_args.add_argument('--mean_values', default=None, type=float, nargs=3,
help='Optional. Normalize input by subtracting the mean '
'values per channel. Example: 255 255 255')
input_transform_args.add_argument('--scale_values', default=None, type=float, nargs=3,
help='Optional. Divide input by scale values per channel. '
'Division is applied after mean values subtraction. '
'Example: 255 255 255')
debug_args = parser.add_argument_group('Debug options')
debug_args.add_argument('-r', '--raw_output_message', help='Optional. Output inference results raw values showing.',
default=False, action='store_true')
return parser
class ColorPalette:
def __init__(self, n, rng=None):
assert n > 0
if rng is None:
rng = random.Random(0xACE)
candidates_num = 100
hsv_colors = [(1.0, 1.0, 1.0)]
for _ in range(1, n):
colors_candidates = [(rng.random(), rng.uniform(0.8, 1.0), rng.uniform(0.5, 1.0))
for _ in range(candidates_num)]
min_distances = [self.min_distance(hsv_colors, c) for c in colors_candidates]
arg_max = np.argmax(min_distances)
hsv_colors.append(colors_candidates[arg_max])
self.palette = [self.hsv2rgb(*hsv) for hsv in hsv_colors]
@staticmethod
def dist(c1, c2):
dh = min(abs(c1[0] - c2[0]), 1 - abs(c1[0] - c2[0])) * 2
ds = abs(c1[1] - c2[1])
dv = abs(c1[2] - c2[2])
return dh * dh + ds * ds + dv * dv
@classmethod
def min_distance(cls, colors_set, color_candidate):
distances = [cls.dist(o, color_candidate) for o in colors_set]
return np.min(distances)
@staticmethod
def hsv2rgb(h, s, v):
return tuple(round(c * 255) for c in colorsys.hsv_to_rgb(h, s, v))
def __getitem__(self, n):
return self.palette[n % len(self.palette)]
def __len__(self):
return len(self.palette)
def get_model(ie, args):
input_transform = models.InputTransform(args.reverse_input_channels, args.mean_values, args.scale_values)
common_args = (ie, args.model, input_transform)
if args.architecture_type in ('ctpn', 'yolo', 'yolov4', 'retinaface',
'retinaface-pytorch') and not input_transform.is_trivial:
raise ValueError("{} model doesn't support input transforms.".format(args.architecture_type))
if args.architecture_type == 'ssd':
return models.SSD(*common_args, labels=args.labels, keep_aspect_ratio_resize=args.keep_aspect_ratio)
elif args.architecture_type == 'ctpn':
return models.CTPN(ie, args.model, input_size=args.input_size, threshold=args.prob_threshold)
elif args.architecture_type == 'yolo':
return models.YOLO(ie, args.model, labels=args.labels,
threshold=args.prob_threshold, keep_aspect_ratio=args.keep_aspect_ratio)
elif args.architecture_type == 'yolov4':
return models.YoloV4(ie, args.model, labels=args.labels,
threshold=args.prob_threshold, keep_aspect_ratio=args.keep_aspect_ratio)
elif args.architecture_type == 'faceboxes':
return models.FaceBoxes(*common_args, threshold=args.prob_threshold)
elif args.architecture_type == 'centernet':
return models.CenterNet(*common_args, labels=args.labels, threshold=args.prob_threshold)
elif args.architecture_type == 'retinaface':
return models.RetinaFace(ie, args.model, threshold=args.prob_threshold)
elif args.architecture_type == 'ultra_lightweight_face_detection':
return models.UltraLightweightFaceDetection(*common_args, threshold=args.prob_threshold)
elif args.architecture_type == 'retinaface-pytorch':
return models.RetinaFacePyTorch(ie, args.model, threshold=args.prob_threshold)
else:
raise RuntimeError('No model type or invalid model type (-at) provided: {}'.format(args.architecture_type))
def isAnnounced(label, x_mid, y_mid):
for idx, objs in enumerate(objLastFrames):
for idy, lbl in enumerate(objs):
if lbl == label:
lastMidPt = objMidsLastFrames[idx][idy]
# Increase this value to reduce repeated announcements and vice versa
if np.abs(lastMidPt[0] - x_mid) + np.abs(lastMidPt[1] - y_mid) < 100:
return True
return False
def announceDetection(frame, frameCount, detections, labels, threshold, output_transform):
global noObjFrames
size = frame.shape[:2]
x_width = 1280
y_height = 720
objectsInFrame = []
objectMidPts = []
for detection in detections:
if detection.score > threshold:
class_id = int(detection.id)-1
# Potential Objects for self driving car
# person
# bicycle
# car
# motorcycle
# bus
# truck
# traffic light
# street sign
# stop sign
if class_id not in [0, 1, 2, 3, 5, 7, 9, 11, 12]:
continue
det_label = labels[class_id] if labels and len(labels) >= class_id else '#{}'.format(class_id)
xmin = max(int(detection.xmin), 0)
ymin = max(int(detection.ymin), 0)
xmax = min(int(detection.xmax), x_width)
ymax = min(int(detection.ymax), y_height)
if xmin > xmax:
swap = xmin
xmin = xmax
xmax = swap
x_mid = np.mean([xmin, xmax])
y_mid = np.mean([ymin, ymax])
if not isAnnounced(det_label, x_mid, y_mid):
# theta min and max corresponds to Pi cam FoV angle
# Picam has 62 degrees horizontal FoV. Need to
# convert to LIDAR angles at LIDAR node.
theta_min = xmin / (x_width / 62)
theta_max = xmax / (x_width / 62)
# print('X pixels =' + str(xmin) + ' image width = ' + str(size[1]) +
# ' theta_min = ' + str(theta_min) + ' theta_max = ' + str(theta_max))
now = time.localtime()
client.publish("object/getdistance", str(det_label) + '|' +
str(theta_min) + '|' + str(theta_max) + '|' + str(now.tm_min * 60 + now.tm_sec))
objectsInFrame.append(det_label)
objectMidPts.append((x_mid, y_mid))
# List of objects and its mid points in last 30 frames will eb stored in dqueue
if len(objectsInFrame) > 0:
objLastFrames.extend([objectsInFrame])
objMidsLastFrames.extend([objectMidPts])
noObjFrames = 0
else:
noObjFrames += 1
if noObjFrames >= 30:
objMidsLastFrames.clear()
objLastFrames.clear()
noObjFrames = 0
def draw_detections(frame, detections, palette, labels, threshold, output_transform):
size = frame.shape[:2]
frame = output_transform.resize(frame)
boxcount = 0
for detection in detections:
if detection.score > threshold:
boxcount = boxcount + 1
class_id = int(detection.id)-1
color = palette[class_id]
det_label = labels[class_id] if labels and len(labels) >= class_id else '#{}'.format(class_id)
xmin = max(int(detection.xmin), 0)
ymin = max(int(detection.ymin), 0)
xmax = min(int(detection.xmax), size[1])
ymax = min(int(detection.ymax), size[0])
xmin, ymin, xmax, ymax = output_transform.scale([xmin, ymin, xmax, ymax])
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
cv2.putText(frame, '{} {:.1%} {}'.format(det_label, detection.score, boxcount),
(xmin, ymin - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
if isinstance(detection, models.DetectionWithLandmarks):
for landmark in detection.landmarks:
landmark = output_transform.scale(landmark)
cv2.circle(frame, (int(landmark[0]), int(landmark[1])), 2, (0, 255, 255), 2)
return frame
def print_raw_results(size, detections, labels, threshold):
log.info(' Class ID | Confidence | XMIN | YMIN | XMAX | YMAX ')
for detection in detections:
if detection.score > threshold:
xmin = max(int(detection.xmin), 0)
ymin = max(int(detection.ymin), 0)
xmax = min(int(detection.xmax), size[1])
ymax = min(int(detection.ymax), size[0])
class_id = int(detection.id)-1
det_label = labels[class_id] if labels and len(labels) >= class_id else '#{}'.format(class_id)
log.info('{:^9} | {:10f} | {:4} | {:4} | {:4} | {:4} '
.format(det_label, detection.score, xmin, ymin, xmax, ymax))
def main():
args = build_argparser().parse_args()
log.info('Initializing Inference Engine...')
ie = IECore()
plugin_config = get_user_config(args.device, args.num_streams, args.num_threads)
log.info('Loading network...')
model = get_model(ie, args)
detector_pipeline = AsyncPipeline(ie, model, plugin_config,
device=args.device, max_num_requests=args.num_infer_requests)
cap = open_images_capture(args.input, args.loop)
# print(cap)
next_frame_id = 0
next_frame_id_to_show = 0
log.info('Starting inference...')
print("To close the application, press 'CTRL+C' here or switch to the output window and press ESC key")
palette = ColorPalette(len(model.labels) if model.labels else 100)
metrics = PerformanceMetrics()
presenter = None
output_transform = None
video_writer = cv2.VideoWriter()
while True:
if detector_pipeline.callback_exceptions:
raise detector_pipeline.callback_exceptions[0]
# Process all completed requests
results = detector_pipeline.get_result(next_frame_id_to_show)
# print('test')
# print(results)
if results:
objects, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if len(objects) and args.raw_output_message:
print_raw_results(frame.shape[:2], objects, model.labels, args.prob_threshold)
presenter.drawGraphs(frame)
frame = draw_detections(frame, objects, palette, model.labels, args.prob_threshold, output_transform)
announceDetection(frame, next_frame_id, objects, model.labels, args.prob_threshold, output_transform)
metrics.update(start_time, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
next_frame_id_to_show += 1
# print(args.no_show)
if not args.no_show:
cv2.imshow('Detection Results', frame)
key = cv2.waitKey(1)
ESC_KEY = 27
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
continue
if detector_pipeline.is_ready():
# Get new image/frame
start_time = perf_counter()
frame = cap.read()
if frame is None:
if next_frame_id == 0:
raise ValueError("Can't read an image from the input")
break
if next_frame_id == 0:
output_transform = models.OutputTransform(frame.shape[:2], args.output_resolution)
if args.output_resolution:
output_resolution = output_transform.new_resolution
else:
output_resolution = (frame.shape[1], frame.shape[0])
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(output_resolution[0] / 4), round(output_resolution[1] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
cap.fps(), output_resolution):
raise RuntimeError("Can't open video writer")
# Submit for inference
detector_pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
next_frame_id += 1
else:
# Wait for empty request
detector_pipeline.await_any()
detector_pipeline.await_all()
# Process completed requests
for next_frame_id_to_show in range(next_frame_id_to_show, next_frame_id):
results = detector_pipeline.get_result(next_frame_id_to_show)
while results is None:
results = detector_pipeline.get_result(next_frame_id_to_show)
objects, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if len(objects) and args.raw_output_message:
print_raw_results(frame.shape[:2], objects, model.labels, args.prob_threshold)
presenter.drawGraphs(frame)
frame = draw_detections(frame, objects, palette, model.labels, args.prob_threshold, output_transform)
metrics.update(start_time, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
if not args.no_show:
cv2.imshow('Detection Results', frame)
key = cv2.waitKey(1)
ESC_KEY = 27
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
metrics.print_total()
print(presenter.reportMeans())
if __name__ == '__main__':
sys.exit(main() or 0)