-
Notifications
You must be signed in to change notification settings - Fork 5
/
visualizer.py
430 lines (354 loc) · 19 KB
/
visualizer.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
from pathlib import Path
import cv2
import numpy as np
from collections import defaultdict
import os
from kalmanfilter import KalmanFilter
from heading_angle import Angle
class Minimap():
def __init__(self, minimap_type='Terrain', minimap_coords=((1423, 710), (1865, 1030)), trajectory_update_rate=30, trajectory_retain_duration=250):
self.homography_CameraToMap = np.load('./map_files/homography_CameraToMap.npy')
if minimap_type == 'Terrain':
self.Minimap = cv2.imread('./map_files/map_satellite_cropped.png')
elif minimap_type == 'Road':
self.Minimap = cv2.imread('./map_files/map_cropped.png')
else:
print("Wrong Minimap type...defaulting to 'Terrain'")
self.Minimap = cv2.imread('./map_files/map_satellite_cropped.png')
# Location in the main image to insert minimap
self.locationMinimap = minimap_coords
original_width = self.Minimap.shape[1]
original_height = self.Minimap.shape[0]
# Resizing the minimap accordingly
resize_width = self.locationMinimap[1][0] - self.locationMinimap[0][0]
resize_height = self.locationMinimap[1][1] - self.locationMinimap[0][1]
self.Minimap = cv2.resize(self.Minimap, (resize_width, resize_height))
self.width_scaling = resize_width/original_width
self.height_scaling = resize_height/original_height
self.realtime_trajectory = defaultdict(list)
self.updateRate = trajectory_update_rate
self.trajectory_retain_duration = trajectory_retain_duration
def projection_image_to_map(self, x, y):
"""Converts image coordinates to minimap coordinates using loaded the homography matrix
Returns:
(int, int): x, y coordinates with respective to scaled minimap
"""
pt1 = np.array([x, y, 1])
pt1 = pt1.reshape(3, 1)
pt2 = np.dot(self.homography_CameraToMap, pt1)
pt2 = pt2 / pt2[2]
return (int(pt2[0]*self.width_scaling), int(pt2[1]*self.height_scaling))
def projection_image_to_map_noScaling(self, x, y):
"""Converts image coordinates to minimap coordinates using loaded the homography matrix
Returns:
(int, int): x, y coordinates with respective to scaled minimap
"""
pt1 = np.array([x, y, 1])
pt1 = pt1.reshape(3, 1)
pt2 = np.dot(self.homography_CameraToMap, pt1)
pt2 = pt2 / pt2[2]
return (int(pt2[0]), int(pt2[1]))
def update_realtime_trajectory(self, current_frameNumber):
"""Responsible for deleting trajectory points for each tracker id after 'self.trajectory_retain_duration' frames
Returns:
None
"""
if self.realtime_trajectory:
for keys, values in list(self.realtime_trajectory.items()):
if len(values) == 0:
del self.realtime_trajectory[keys]
elif current_frameNumber - values[0][3] > self.trajectory_retain_duration:
del self.realtime_trajectory[keys][0]
class Visualizer():
def __init__(self, minimap, trajectory_mode, trajectory_update_rate, trajectory_retain_duration, save_class_frames):
self.classID_dict = {
-1: ("FrameDiff", (0, 0, 0)),
0: ("Escooter", (0, 90, 255)),
1: ("Pedestrians", (255, 90, 0)),
2: ("Cyclists", (90, 255, 0)),
3: ("Motorcycle", (204, 0, 102)),
4: ("Car", (0, 0, 255)),
5: ("Truck", (0, 102, 204)),
6: ("Bus", (0, 255, 255))
}
self.textColor = (0, 0, 0)
self.count = 0 # variable to update default_dict after certain number of count
self.track_count = 0
self.save_class_frames = save_class_frames
self.kf = KalmanFilter()
self.angle = Angle()
if minimap:
self.showMinimap = True
self.Minimap_obj = Minimap(trajectory_update_rate=trajectory_update_rate, trajectory_retain_duration=trajectory_retain_duration)
self.showTrajectory = trajectory_mode
else:
self.showMinimap = False
self.showTrajectory = False
def draw_realtime_trajectory(self, minimap_img):
"""Displays the recorded trajectory onto the minimap
Returns:
None
"""
if self.Minimap_obj.realtime_trajectory:
for keys, values in self.Minimap_obj.realtime_trajectory.items():
for v in values:
color = self.classID_dict[v[2]][1]
cv2.circle(minimap_img, (int(v[0]),int(v[1])), 1, color, -1, cv2.LINE_AA)
return minimap_img
def drawEmpty(self, frame, frameCount):
"""For images with no detections, displaying minimap and updating trajectory values
Returns:
frame (image): Image with minimap (if minimap enabled)
"""
if self.showMinimap:
minimap_img = self.Minimap_obj.Minimap.copy()
if self.showTrajectory:
minimap_img = self.draw_realtime_trajectory(minimap_img)
self.Minimap_obj.update_realtime_trajectory(frameCount)
frame[self.Minimap_obj.locationMinimap[0][1]:self.Minimap_obj.locationMinimap[1][1], self.Minimap_obj.locationMinimap[0][0]:self.Minimap_obj.locationMinimap[1][0]] = minimap_img
return frame
else:
return frame
def drawBBOX(self, xyxy, frame, frameCount):
"""Draws just the BBOX with the class name and confidence score
Args:
xyxy (array): output from inference
frame (image): Image to draw
Returns:
frame (image): Image with all the BBOXes
"""
if self.showMinimap:
minimap_img = self.Minimap_obj.Minimap.copy()
if self.showTrajectory:
minimap_img = self.draw_realtime_trajectory(minimap_img)
self.Minimap_obj.update_realtime_trajectory(frameCount)
for detection in xyxy:
x1, y1, x2, y2 = detection[0:4]
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
try:
conf_score = round(detection[4].item() * 100, 1)
except AttributeError:
conf_score = round(detection[4] * 100, 1)
try:
classID = int(detection[5].item())
except AttributeError:
classID = int(detection[5])
color = self.classID_dict[classID][1]
# Displays the main bbox and add overlay to make bbox transparent
overlay = frame.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), color, 2)
# Finds the space required for text
textLabel = f'{self.classID_dict[classID][0]} {conf_score}%'
(w1, h1), _ = cv2.getTextSize(
textLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
# Displays BG Box for the text and text itself
cv2.rectangle(overlay, (x1, y1 - 20), (x1 + w1, y1), color, -1, cv2.LINE_AA)
image = cv2.addWeighted(overlay, 0.6, frame, 0.4, 0)
frame = cv2.putText(
image, textLabel, (x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
if self.showMinimap:
# Converting coordinates from image to map
# Just using the larger y value because BBOX center is not were the foot/wheels of the classes are. So center point taken is the center of the bottom line of BBOX
_, max_y = sorted((y1, y2))
point_coordinates = self.Minimap_obj.projection_image_to_map((x1+x2)/2, max_y)
cv2.circle(minimap_img, point_coordinates, 1, color, -1, cv2.LINE_AA)
if self.showMinimap:
frame[self.Minimap_obj.locationMinimap[0][1]:self.Minimap_obj.locationMinimap[1][1], self.Minimap_obj.locationMinimap[0][0]:self.Minimap_obj.locationMinimap[1][0]] = minimap_img
return frame
def drawTracker(self, trackers, frame, frameCount):
"""Draws the BBOX along with Tracker ID for each BBOX
Args:
trackers (array): SORT Tracker object
frame (image): Image to draw
Returns:
image: Image with tracker id and bbox
"""
if self.showMinimap:
minimap_img = self.Minimap_obj.Minimap.copy()
if self.showTrajectory:
minimap_img = self.draw_realtime_trajectory(minimap_img)
self.Minimap_obj.update_realtime_trajectory(frameCount)
for detection in trackers:
x1, y1, x2, y2 = detection[0:4]
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
conf_score = round(detection[4] * 100, 1)
classID = int(detection[5])
tracker_id = int(detection[9])
color = self.classID_dict[classID][1]
# Displays the main bbox and add overlay to make bbox transparent
overlay = frame.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), color, 2)
# Finds the space required for text
TrackerLabel = f'Track ID: {tracker_id}'
(w1, h1), _ = cv2.getTextSize(
TrackerLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
baseLabel = f'{self.classID_dict[classID][0]} {conf_score}%'
(w2, h2), _ = cv2.getTextSize(
baseLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
# Displays BG Box for the text and text itself
cv2.rectangle(overlay, (x1, y1 - 40), (x1 + w1, y1), color, -1, cv2.LINE_AA)
cv2.rectangle(overlay, (x1, y1 - 20), (x1 + w2, y1), color, -1, cv2.LINE_AA)
image = cv2.addWeighted(overlay, 0.6, frame, 0.4, 0)
frame = cv2.putText(
image, TrackerLabel, (x1, y1 - 24),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
frame = cv2.putText(
image, baseLabel, (x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
if self.showMinimap:
# Converting coordinates from image to map
# Just using the larger y value because BBOX center is not were the foot/wheels of the classes are. So center point taken is the center of the bottom line of BBOX
_, max_y = sorted((y1, y2))
point_coordinates = self.Minimap_obj.projection_image_to_map((x1+x2)/2, max_y)
if self.showTrajectory:
if frameCount % self.Minimap_obj.updateRate == 0:
self.Minimap_obj.realtime_trajectory[tracker_id].append((point_coordinates[0], point_coordinates[1], classID, frameCount))
cv2.circle(minimap_img, point_coordinates, 1, color, -1, cv2.LINE_AA)
# Plotting the text
textSize, _ = cv2.getTextSize(str(tracker_id), cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
rectangle_start_coord = (point_coordinates[0] + 3, point_coordinates[1] - textSize[1] - 5)
rectangle_end_coord = (point_coordinates[0] + textSize[0] + 3, point_coordinates[1])
cv2.rectangle(minimap_img, rectangle_start_coord, rectangle_end_coord, color, -1)
cv2.putText(minimap_img, str(tracker_id), tuple((point_coordinates[0] + 3, point_coordinates[1] - 3)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA)
if self.showMinimap:
frame[self.Minimap_obj.locationMinimap[0][1]:self.Minimap_obj.locationMinimap[1][1], self.Minimap_obj.locationMinimap[0][0]:self.Minimap_obj.locationMinimap[1][0]] = minimap_img
return frame
def drawAll(self, trackers, frame, frameCount, output):
"""Draws the BBOX along with Tracker ID and speed for every detection
Args:
trackers (array): SORT Tracker object (with speed(kmh) as the last element)
frame (image): Image to draw
Returns:
image: Image with tracker id, speed(kmh) and bbox
"""
if self.showMinimap:
minimap_img = self.Minimap_obj.Minimap.copy()
if self.showTrajectory:
minimap_img = self.draw_realtime_trajectory(minimap_img)
self.Minimap_obj.update_realtime_trajectory(frameCount)
self.count +=1
output_path = output
for detection in trackers:
x1, y1, x2, y2 = detection[0:4]
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
conf_score = round(detection[4] * 100, 1)
classID = int(detection[5])
tracker_id = int(detection[9])
speed = detection[10]
color = self.classID_dict[classID][1]
# variables for heading arrow
cx1, cy1 = int(detection[-3]), int(detection[-2]) # previous frame points
track_pts = detection[-1]
# Displays the main bbox and add overlay to make bbox transparent
overlay = frame.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), color, 2)
# Finds the space required for text
TrackerLabel = f'Track ID: {tracker_id}'
(w1, h1), _ = cv2.getTextSize(
TrackerLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
speedLabel = f'Speed: {speed}km/h'
(w2, h2), _ = cv2.getTextSize(
speedLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
baseLabel = f'{self.classID_dict[classID][0]} {conf_score}%'
(w3, h3), _ = cv2.getTextSize(
baseLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
# Displays BG Box for the text and text itself
cv2.rectangle(overlay, (x1, y1 - 60), (x1 + w1, y1), color, -1, cv2.LINE_AA)
cv2.rectangle(overlay, (x1, y1 - 40), (x1 + w2, y1), color, -1, cv2.LINE_AA)
cv2.rectangle(overlay, (x1, y1 - 20), (x1 + w3, y1), color, -1, cv2.LINE_AA)
image = cv2.addWeighted(overlay, 0.6, frame, 0.4, 0)
frame = cv2.putText(
image, TrackerLabel, (x1, y1 - 43),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
frame = cv2.putText(
image, speedLabel, (x1, y1 - 24),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
frame = cv2.putText(
image, baseLabel, (x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
# Save frames of required Class Instances in 30 frames consecutive for each tracker_id of that class.
if classID == self.save_class_frames and self.track_count<2:
_output_path_dir = os.path.join(output_path, "Save-frames")
_output_fileName = os.path.join(_output_path_dir, f"{self.classID_dict[classID][0]}-{tracker_id}_count-{self.track_count}.jpg")
cv2.imwrite(_output_fileName, frame)
self.track_count += 1
# Use kalman_filter to predict next point and draw heading arrow in that direction
if type(track_pts)==defaultdict:
for pt in track_pts[tracker_id]:
predicted = self.kf.predict(pt[0], pt[1])
pred = self.kf.predict(predicted[0], predicted[1])
pred2 = self.kf.predict(pred[0], pred[1])
cv2.arrowedLine(frame, (cx1,cy1), (int(pred2[0]),int(pred2[1])), (255,0,0),1)
points = [[cx1, cy1], [int(pred2[0]), int(pred2[1])], [1920, cy1]]
if speed>3:
angle = self.angle.findangle(points=points)
if angle>10 and angle<=80:
direction = "NE"
elif angle>80 and angle<=100:
direction = "N"
elif angle>100 and angle<=170:
direction = "NW"
elif angle>170 and angle<=190:
direction = "W"
elif angle>190 and angle<=260:
direction = "SW"
elif angle>260 and angle<=280:
direction = "S"
elif angle>280 and angle<=350:
direction = "SE"
elif angle>350 and angle<=360:
direction = "E"
elif angle>=0 and angle<=10:
direction = "E"
AngleLabel = f'Angle: {angle}, {direction}'
(w4, h4), _ = cv2.getTextSize(
AngleLabel, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1
)
cv2.rectangle(frame, (x2, y1 + 20), (x2 + w4, y1), color, -1, cv2.LINE_AA)
frame = cv2.putText(
image, AngleLabel, (x2, y1 + 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA
)
if self.showMinimap:
# Converting coordinates from image to map
# Just using the larger y value because BBOX center is not were the foot/wheels of the classes are. So center point taken is the center of the bottom line of BBOX
if classID in (0,1,2):
_, max_y = sorted((y1, y2))
elif classID in (3,4,5,6):
max_y = int((y1+y2)/2) # Center of bbox for classes other than Escooter, Cyclist, and Pedestrian
point_coordinates = self.Minimap_obj.projection_image_to_map((x1+x2)/2, max_y)
if self.showTrajectory:
if frameCount % self.Minimap_obj.updateRate == 0:
self.Minimap_obj.realtime_trajectory[tracker_id].append((point_coordinates[0], point_coordinates[1], classID, frameCount))
cv2.circle(minimap_img, point_coordinates, 1, color, -1, cv2.LINE_AA)
# Plotting the text
textSize, _ = cv2.getTextSize(str(tracker_id), cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
rectangle_start_coord = (point_coordinates[0] + 3, point_coordinates[1] - textSize[1] - 5)
rectangle_end_coord = (point_coordinates[0] + textSize[0] + 3, point_coordinates[1])
cv2.rectangle(minimap_img, rectangle_start_coord, rectangle_end_coord, color, -1)
cv2.putText(minimap_img, str(tracker_id), tuple((point_coordinates[0] + 3, point_coordinates[1] - 3)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.textColor, 1, cv2.LINE_AA)
if self.showMinimap:
frame[self.Minimap_obj.locationMinimap[0][1]:self.Minimap_obj.locationMinimap[1][1], self.Minimap_obj.locationMinimap[0][0]:self.Minimap_obj.locationMinimap[1][0]] = minimap_img
return frame