-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathvis.py
128 lines (102 loc) · 4.37 KB
/
vis.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
import argparse
import numpy as np
import h5py
from mayavi import mlab
def getTransform(x, y, z, pitch, yaw, roll, degrees=True):
'''Given location x,y,z and pitch, yaw, roll, obtain the matrix that convert from local to global CS using the left-handed system from UE4'''
if degrees:
pitch, yaw, roll = [np.radians(x) for x in [pitch, yaw, roll]]
cy,sy = np.cos(yaw), np.sin(yaw)
cr,sr = np.cos(roll), np.sin(roll)
cp,sp = np.cos(pitch), np.sin(pitch)
mat = np.array([cp * cy, cy * sp * sr - sy * cr, -cy * sp * cr - sy * sr, x, \
cp * sy, sy * sp * sr + cy * cr, -sy * sp * cr + cy * sr, y, \
sp, -cp * sr, cp * cr, z, \
0, 0, 0, 1], dtype=np.float).reshape(4,4)
return mat
def transformPoints(transformMatrix, pts, inverse=False):
'''Given a transformation matrix [4,4] convert pts [N,3] or [N,4] (last coordinate is intensity)'''
#Check if intensity is available
if pts.shape[1] == 4:
#split intensity from 3D coordinates, add homogeneus coordinate
intensity = pts[:,-1,np.newaxis].copy()
pts[:,-1] = 1
else:
#add homogeneus coordinate
intensity = None
pts = np.concatenate([pts, np.ones((pts.shape[0],1))], axis=1)
#perform transformation
mat = np.array(transformMatrix)
if inverse:
mat = np.linalg.inv(mat)
ptst = pts @ mat.T
ptst = ptst[:,:3]
#merge intensity back
if intensity is not None:
ptst = np.concatenate([ptst,intensity], axis=1)
return ptst
def updateBoundingBox(x,y,z,yaw,pitch,w,l,h, vis_bb):
#Create 8 corner points
cpts = 0.5*np.array([[-1,1,-1],[1,1,-1],[1,-1,-1],[-1,-1,-1],[-1,1,1],[1,1,1],[1,-1,1],[-1,-1,1]])
cpts *= np.array([[w,l,h]])
cpts = transformPoints(getTransform(x,y,z,pitch,yaw,0), cpts)
#list of 16 points to create whole BB
pts = cpts[[0,3,7,3,2,6,2,1,5,1,0,4,7,6,5,4],:]
#update vis
vis_bb.mlab_source.reset(x=pts[:,0], y=pts[:,1], z=pts[:,2])
def main(args):
#Load file/data
f = h5py.File(args.filename, 'r')
pcls = f['point_cloud']
lidar_pose = f['lidar_pose']
vbbs = f['vehicle_boundingbox']
pbbs = f['pedestrian_boundingbox']
nframes = pcls.shape[0]
nvehicles = pcls.shape[1]
npedestrians = pbbs.shape[1]
#Create Mayavi Visualisation
fig = mlab.figure(size=(960,540), bgcolor=(0.05,0.05,0.05))
zeros = np.zeros(pcls.shape[1]*pcls.shape[2])
vis = mlab.points3d(zeros, zeros, zeros, zeros, mode='point', figure=fig)
zeros = np.zeros(16)
vis_vbbs = [mlab.plot3d(zeros, zeros, zeros, zeros, color=(0,1,0), tube_radius=None, line_width=1, figure=fig) for i in range(nvehicles)]
vis_pbbs = [mlab.plot3d(zeros, zeros, zeros, zeros, color=(0,1,1), tube_radius=None, line_width=1, figure=fig) for i in range(npedestrians)]
#Iterate through frames
@mlab.animate(delay=100)
def anim():
for frame in range(nframes):
print(f'Frame {frame}')
#Update pedestrian bounding boxes
for i in range(npedestrians):
updateBoundingBox(*pbbs[frame, i].tolist(), vis_pbbs[i])
fusedPCL = []
for i in range(nvehicles):
#Get PCL for the given vehicle in the global Coordinate System
pcl = pcls[frame,i]
transform = getTransform(*lidar_pose[frame,i].tolist())
pcl_global = transformPoints(transform, pcl)
fusedPCL.append(pcl_global)
#Update the vehicle BB visualisation
updateBoundingBox(*vbbs[frame,i].tolist(), vis_vbbs[i])
#Update PCL visualisation with Mayavi
fusedPCL = np.concatenate(fusedPCL, axis=0)
vis.mlab_source.set(x=fusedPCL[:,0], y=fusedPCL[:,1], z=fusedPCL[:,2], scalars=fusedPCL[:,3])
#Set top-view if first frame
if frame == 0:
mlab.gcf().scene.z_plus_view()
yield
anim()
mlab.show()
if __name__ == '__main__':
argparser = argparse.ArgumentParser()
argparser.add_argument(
'filename',
type=str,
help='Path to snippet to be visualised')
args = argparser.parse_args()
try:
main(args)
except KeyboardInterrupt:
pass
finally:
print('Finished visualisation!')