-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathlaserscanvis.py
313 lines (275 loc) · 11 KB
/
laserscanvis.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
#!/usr/bin/env python3
# This file is covered by the LICENSE file in the root of this project.
import vispy
from vispy.scene import visuals, SceneCanvas
import numpy as np
from matplotlib import pyplot as plt
from auxiliary.laserscan import LaserScan, SemLaserScan
class LaserScanVis:
"""Class that creates and handles a visualizer for a pointcloud"""
def __init__(self, scan, scan_names, label_names, offset=0,
semantics=True, semantics_only=False, instances=False, images=True, link=False):
self.scan = scan
self.scan_names = scan_names
self.label_names = label_names
self.offset = offset
self.total = len(self.scan_names)
self.semantics = semantics
self.semantics_only = semantics_only
self.instances = instances
self.images = images
self.link = link
# sanity check
if not self.semantics and self.instances:
print("Instances are only allowed in when semantics=True")
raise ValueError
self.reset()
self.update_scan()
def default_reset(self):
"""
Default reset with raw pointcloud, colored point cloud and/or instances
"""
self.scan_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.canvas.scene)
self.grid.add_widget(self.scan_view, 0, 0)
self.scan_vis = visuals.Markers()
self.scan_view.camera = 'turntable'
self.scan_view.add(self.scan_vis)
visuals.XYZAxis(parent=self.scan_view.scene)
# add semantics
if self.semantics:
print("Using semantics in visualizer")
self.sem_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.canvas.scene)
self.grid.add_widget(self.sem_view, 0, 1)
self.sem_vis = visuals.Markers()
self.sem_view.camera = 'turntable'
self.sem_view.add(self.sem_vis)
visuals.XYZAxis(parent=self.sem_view.scene)
if self.link:
self.sem_view.camera.link(self.scan_view.camera)
if self.instances:
print("Using instances in visualizer")
self.inst_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.canvas.scene)
self.grid.add_widget(self.inst_view, 0, 2)
self.inst_vis = visuals.Markers()
self.inst_view.camera = 'turntable'
self.inst_view.add(self.inst_vis)
visuals.XYZAxis(parent=self.inst_view.scene)
if self.link:
self.inst_view.camera.link(self.scan_view.camera)
# add a view for the depth
if self.images:
# img canvas size
self.multiplier = 1
self.canvas_W = 1024
self.canvas_H = 64
if self.semantics:
self.multiplier += 1
if self.instances:
self.multiplier += 1
# new canvas for img
self.img_canvas = SceneCanvas(keys='interactive', show=True,
size=(self.canvas_W, self.canvas_H * self.multiplier))
# grid
self.img_grid = self.img_canvas.central_widget.add_grid()
# interface (n next, b back, q quit, very simple)
self.img_canvas.events.key_press.connect(self.key_press)
self.img_canvas.events.draw.connect(self.draw)
self.img_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.img_canvas.scene)
self.img_grid.add_widget(self.img_view, 0, 0)
self.img_vis = visuals.Image(cmap='viridis')
self.img_view.add(self.img_vis)
# add image semantics
if self.semantics:
self.sem_img_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.img_canvas.scene)
self.img_grid.add_widget(self.sem_img_view, 1, 0)
self.sem_img_vis = visuals.Image(cmap='viridis')
self.sem_img_view.add(self.sem_img_vis)
# add instances
if self.instances and self.images:
self.inst_img_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.img_canvas.scene)
self.img_grid.add_widget(self.inst_img_view, 2, 0)
self.inst_img_vis = visuals.Image(cmap='viridis')
self.inst_img_view.add(self.inst_img_vis)
if self.link:
self.inst_view.camera.link(self.scan_view.camera)
def semantics_only_reset(self):
"""
Reset without raw pointcloud, colored point cloud and/or instances
"""
# add semantics
print("Using semantics in visualizer")
self.sem_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.canvas.scene)
self.grid.add_widget(self.sem_view, 0, 0)
self.sem_vis = visuals.Markers()
self.sem_view.camera = 'turntable'
self.sem_view.add(self.sem_vis)
visuals.XYZAxis(parent=self.sem_view.scene)
if self.link and not self.semantics_only:
self.sem_view.camera.link(self.scan_view.camera)
if self.instances:
print("Using instances in visualizer")
self.inst_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.canvas.scene)
self.grid.add_widget(self.inst_view, 0, 1)
self.inst_vis = visuals.Markers()
self.inst_view.camera = 'turntable'
self.inst_view.add(self.inst_vis)
visuals.XYZAxis(parent=self.inst_view.scene)
if self.link and not self.semantics_only:
self.inst_view.camera.link(self.scan_view.camera)
else:
self.inst_view.camera.link(self.sem_view.camera)
# add a view for the depth
if self.images:
# img canvas size
self.multiplier = 1
self.canvas_W = 1024
self.canvas_H = 64
self.multiplier += 1 # for semantics, which are definitely wanted in this case, because semantics_only=True
if self.instances:
self.multiplier += 1
# new canvas for img
self.img_canvas = SceneCanvas(keys='interactive', show=True,
size=(self.canvas_W, self.canvas_H * self.multiplier))
# grid
self.img_grid = self.img_canvas.central_widget.add_grid()
# interface (n next, b back, q quit, very simple)
self.img_canvas.events.key_press.connect(self.key_press)
self.img_canvas.events.draw.connect(self.draw)
# add image semantics
if self.semantics:
self.sem_img_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.img_canvas.scene)
self.img_grid.add_widget(self.sem_img_view, 0, 0)
self.sem_img_vis = visuals.Image(cmap='viridis')
self.sem_img_view.add(self.sem_img_vis)
# add instances
if self.instances and self.images:
self.inst_img_view = vispy.scene.widgets.ViewBox(
border_color='white', parent=self.img_canvas.scene)
self.img_grid.add_widget(self.inst_img_view, 1, 0)
self.inst_img_vis = visuals.Image(cmap='viridis')
self.inst_img_view.add(self.inst_img_vis)
if self.link:
self.inst_view.camera.link(self.sem_view.camera)
def reset(self):
""" Reset. """
# last key press (it should have a mutex, but visualization is not
# safety critical, so let's do things wrong)
self.action = "no" # no, next, back, quit are the possibilities
# new canvas prepared for visualizing data
self.canvas = SceneCanvas(keys='interactive', show=True)
# interface (n next, b back, q quit, very simple)
self.canvas.events.key_press.connect(self.key_press)
self.canvas.events.draw.connect(self.draw)
# grid
self.grid = self.canvas.central_widget.add_grid()
# laserscan part
if not self.semantics_only:
self.default_reset()
else:
self.semantics_only_reset()
def get_mpl_colormap(self, cmap_name):
cmap = plt.get_cmap(cmap_name)
# Initialize the matplotlib color map
sm = plt.cm.ScalarMappable(cmap=cmap)
# Obtain linear color range
color_range = sm.to_rgba(np.linspace(0, 1, 256), bytes=True)[:, 2::-1]
return color_range.reshape(256, 3).astype(np.float32) / 255.0
def update_scan(self):
# first open data
self.scan.open_scan(self.scan_names[self.offset])
if self.semantics:
self.scan.open_label(self.label_names[self.offset])
self.scan.colorize()
# then change names
title = "scan " + str(self.offset)
self.canvas.title = title
if self.images:
self.img_canvas.title = title
# then do all the point cloud stuff
# plot scan
power = 16
if not self.semantics_only:
range_data = np.copy(self.scan.unproj_range)
# print(range_data.max(), range_data.min())
range_data = range_data ** (1 / power)
# print(range_data.max(), range_data.min())
viridis_range = ((range_data - range_data.min()) /
(range_data.max() - range_data.min()) *
255).astype(np.uint8)
viridis_map = self.get_mpl_colormap("viridis")
viridis_colors = viridis_map[viridis_range]
self.scan_vis.set_data(self.scan.points,
face_color=viridis_colors[..., ::-1],
edge_color=viridis_colors[..., ::-1],
size=1)
# plot semantics
if self.semantics:
self.sem_vis.set_data(self.scan.points,
face_color=self.scan.sem_label_color[..., ::-1],
edge_color=self.scan.sem_label_color[..., ::-1],
size=1)
# plot instances
if self.instances:
self.inst_vis.set_data(self.scan.points,
face_color=self.scan.inst_label_color[..., ::-1],
edge_color=self.scan.inst_label_color[..., ::-1],
size=1)
if self.images:
# now do all the range image stuff
# plot range image
if not self.semantics_only:
data = np.copy(self.scan.proj_range)
# print(data[data > 0].max(), data[data > 0].min())
data[data > 0] = data[data > 0] ** (1 / power)
data[data < 0] = data[data > 0].min()
# print(data.max(), data.min())
data = (data - data[data > 0].min()) / \
(data.max() - data[data > 0].min())
# print(data.max(), data.min())
self.img_vis.set_data(data)
self.img_vis.update()
if self.semantics:
self.sem_img_vis.set_data(self.scan.proj_sem_color[..., ::-1])
self.sem_img_vis.update()
if self.instances:
self.inst_img_vis.set_data(self.scan.proj_inst_color[..., ::-1])
self.inst_img_vis.update()
# interface
def key_press(self, event):
self.canvas.events.key_press.block()
if self.images:
self.img_canvas.events.key_press.block()
if event.key == 'N':
self.offset += 1
if self.offset >= self.total:
self.offset = 0
self.update_scan()
elif event.key == 'B':
self.offset -= 1
if self.offset < 0:
self.offset = self.total - 1
self.update_scan()
elif event.key == 'Q' or event.key == 'Escape':
self.destroy()
def draw(self, event):
if self.canvas.events.key_press.blocked():
self.canvas.events.key_press.unblock()
if self.images and self.img_canvas.events.key_press.blocked():
self.img_canvas.events.key_press.unblock()
def destroy(self):
# destroy the visualization
self.canvas.close()
if self.images:
self.img_canvas.close()
vispy.app.quit()
def run(self):
vispy.app.run()