forked from zineanteoh/i24-overhead-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverhead_visualizer.py
311 lines (255 loc) · 11.8 KB
/
overhead_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 1 15:42:19 2022
@author: teohz
"""
from i24_database_api.db_reader import DBReader
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import mplcursors
import numpy as np
import cmd
import json
import os
import pymongo
class OverheadVisualizer():
"""
Overhead Visualizer utilizes i24_database_api to query trajectories
information and plot a visualization of the vehicles.
"""
def __init__(self, config, MODE,
vehicle_database, vehicle_collection,
timestamp_database, timestamp_collection,
x_start=2000, x_end=1000,
framerate=25):
"""
Initializes an Overhead Traffic VIsualizer object
Parameters
----------
config : object
"""
self.timestamp_dbr = DBReader(host=config["host"],
port=config["port"],
username=config["username"],
password=config["password"],
database_name=timestamp_database,
collection_name=timestamp_collection)
self.vehicle_dbr = DBReader(host=config["host"],
port=config["port"],
username=config["username"],
password=config["password"],
database_name=vehicle_database,
collection_name=vehicle_collection)
self.anim = None
self.MODE = MODE
if self.MODE != "RAW" and self.MODE != "RECONCILED":
raise ValueError("MODE must be either 'RAW' or 'RECONCILED'")
# TODO dynamically set the most appropriate start and end
self.x_start = x_start
self.x_end = x_end
self.framerate = framerate
self.y_start = -12
self.y_end = 11*12
self.paused = False
self.cursor = None
self.vehicle_collection = vehicle_collection
def visualize(self, frames=20000, save=False, verbose=False):
"""
params:
frames (int):
number of frames to plot. if exceeds max number
of frames in database, then animation will stop
save (boolean):
whether to save animation as a .mp4 file
verbose (boolean):
whether to print messages related to visualization
i.e. vehicle off the road plotted
"""
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_aspect('equal', 'box')
ax1.set(ylim=[self.y_start, self.y_end])
ax1.set_title("Bird-eye view")
ax1.set(xlim=[self.x_start, self.x_end])
# connect key press event to toggle pause
fig.canvas.mpl_connect('key_press_event', self.toggle_pause)
def on_xlims_change(event_ax):
# print("updated xlims: ", event_ax.get_xlim())
new_xlim = event_ax.get_xlim()
# ax1.set(xlim=new_xlim)
self.x_start = new_xlim[0]
self.x_end = new_xlim[1]
ax1.callbacks.connect('xlim_changed', on_xlims_change)
def init():
# plot lanes
for i in range(-1, 12):
if i in (-1, 5, 11):
plt.axhline(y=i*12, linewidth=0.5, color='k')
else:
plt.axhline(y=i*12, linewidth=0.1, color='k')
return ax1,
def animate_reconciled(i, cursor, cache_vehicle, cache_colors):
if (i % self.framerate > self.framerate):
return ax1,
ax1.set_title("{} | Frame {}".format(self.vehicle_collection, i))
doc = cursor.next()
# remove all car_boxes
for box in list(ax1.patches):
box.set_visible(False)
box.remove()
# query for vehicle dimensions
traj_cursor = self.vehicle_dbr.collection.find({"_id": {"$in": doc["id"]} },
{"width":1, "length":1, "coarse_vehicle_class": 1})
# add vehicle dimension to cache
for index, traj in enumerate(traj_cursor):
# print("index: {} is {}".format(index, traj))
# { ObjectId('...') : [length, width, coarse_vehicle_class] }
cache_vehicle[traj["_id"]] = [traj["length"], traj["width"], traj["coarse_vehicle_class"]]
if traj["_id"] not in cache_colors:
cache_colors[traj["_id"]] = np.random.rand(3,)
# plot vehicles
for index in range(len(doc["position"])):
car_x_pos = doc["position"][index][0]
car_y_pos = doc["position"][index][1]
car_length = cache_vehicle[doc["id"][index]][0]
car_width = cache_vehicle[doc["id"][index]][1]
if isinstance(car_width, list):
# vehicle width and length are lists
# TODO: currently just take the first item from the array
car_length = car_length[0]
car_width = car_width[0]
# print("index {} at ({},{})".format(index, car_x_pos, car_y_pos))
if car_x_pos <= self.x_start and car_x_pos >= self.x_end:
box = patches.Rectangle((car_x_pos, car_y_pos),
car_length, car_width,
color=cache_colors[doc["id"][index]],
label=doc["id"][index])
ax1.add_patch(box)
if verbose:
if car_y_pos > self.y_end or car_y_pos < self.y_start:
print("Vehicle off the road at coordinate ({}, {}) at frame={}".format(car_x_pos, car_y_pos, i))
return ax1,
def animate_raw(i, cursor, cache_colors):
if (i % self.framerate > self.framerate):
return ax1,
ax1.set_title("{} | Frame {}".format(self.vehicle_collection, i))
doc = cursor.next()
# remove all car_boxes
for box in list(ax1.patches):
box.set_visible(False)
box.remove()
# plot vehicles
for index in range(len(doc["id"])):
car_id = doc["id"][index]
if car_id not in cache_colors:
cache_colors[car_id] = np.random.rand(3,)
car_x_pos = doc["position"][index][0]
car_y_pos = doc["position"][index][1]
car_length = doc["dimensions"][index][0]
car_width = doc["dimensions"][index][1]
# print("index {} at ({},{})".format(index, car_x_pos, car_y_pos))
if car_x_pos <= self.x_start and car_x_pos >= self.x_end:
box = patches.Rectangle((car_x_pos, car_y_pos),
car_length, car_width,
color=cache_colors[car_id],
label=car_id)
ax1.add_patch(box)
if verbose:
if car_y_pos > self.y_end or car_y_pos < self.y_start:
print("Vehicle off the road at coordinate ({}, {}) at frame={}".format(car_x_pos, car_y_pos, i))
return ax1,
# maintains a cache of vehicle information (width, height, class)
# cache_vehicle[car_id] = [width, height, coarse_vehicle_class]
cache_vehicle = {}
cache_colors = {}
cursor = self.timestamp_dbr.collection.find().sort([("timestamp", pymongo.ASCENDING)]).limit(frames)
if self.MODE == "RAW":
to_animate = animate_raw
to_args = (cursor, cache_colors,)
else:
to_animate = animate_reconciled
to_args = (cursor, cache_vehicle, cache_colors,)
self.anim = animation.FuncAnimation(fig, func=to_animate,
init_func=init,
frames=frames,
repeat=False,
interval=2,
fargs=to_args,
blit=False)
if save:
self.anim.save('animation.mp4', writer='ffmpeg', fps=self.framerate)
plt.show()
print("complete")
"""
press spacebar to pause/resume animation
"""
def toggle_pause(self, event):
if event.key == " ":
if self.paused:
self.anim.resume()
print("Animation Resumed")
self.cursor.remove()
else:
self.anim.pause()
print("Animation Paused")
self.cursor = mplcursors.cursor(hover=True)
def on_add(sel):
if self.paused and (sel.artist.get_label()[0] != "_"):
print(sel.artist.get_label())
sel.annotation.set_text(sel.artist.get_label())
# connect mouse event to hover for car ID
self.cursor.connect("add", lambda sel: on_add(sel))
self.paused = not self.paused
if True and __name__=="__main__":
os.chdir("/isis/home/teohz/Desktop/videowall")
with open('config.json') as f:
config = json.load(f)
test = 4
if test == 1:
# Collection with vehicle ID index
vehicle_database = "zitest"
vehicle_collection = "ground_truth_two"
# Collection with timestamp index
timestamp_database = "zitest"
timestamp_collection = "ground_truth_two_transformed"
x_start=16000
x_end=15000
MODE = "RECONCILED"
elif test == 2:
# Collection with vehicle ID index
vehicle_database = "zitest"
vehicle_collection = "tracking_v1"
# Collection with timestamp index
timestamp_database = "zitest"
timestamp_collection = "tracking_v1_transformed"
x_start=2000
x_end=1000
MODE = "RAW"
elif test == 3:
# Collection with vehicle ID index
vehicle_database = "zitest"
vehicle_collection = "tracking_v1_reconciled_l1"
# Collection with timestamp index
timestamp_database = "zitest"
timestamp_collection = "tracking_v1_reconciled_l1_transformed"
x_start=2000
x_end=1000
MODE = "RECONCILED"
elif test == 4:
# Collection with vehicle ID index
vehicle_database = "zitest"
vehicle_collection = "batch_5_07072022"
# Collection with timestamp index
timestamp_database = "zitest"
timestamp_collection = "batch_5_07072022_transformed"
x_start=2000
x_end=1000
MODE = "RAW"
# Plot and visualize
viz = OverheadVisualizer(config, MODE,
vehicle_database, vehicle_collection,
timestamp_database, timestamp_collection,
x_start, x_end, framerate=25)
viz.visualize(frames=500, verbose=True, save=False)