-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
133 lines (105 loc) · 3.42 KB
/
demo.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
#! /usr/bin/python3
from glob import glob
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def rot(n):
n = np.asarray(n).flatten()
assert(n.size == 3)
theta = np.linalg.norm(n)
if theta:
n /= theta
K = np.array([[0, -n[2], n[1]], [n[2], 0, -n[0]], [-n[1], n[0], 0]])
return np.identity(3) + np.sin(theta) * K + (1 - np.cos(theta)) * K @ K
else:
return np.identity(3)
def get_bbox(p0, p1):
"""
Input:
* p0, p1
(3)
Corners of a bounding box represented in the body frame.
Output:
* v
(3, 8)
Vertices of the bounding box represented in the body frame.
* e
(2, 14)
Edges of the bounding box. The first 2 edges indicate the `front` side
of the box.
"""
v = np.array([
[p0[0], p0[0], p0[0], p0[0], p1[0], p1[0], p1[0], p1[0]],
[p0[1], p0[1], p1[1], p1[1], p0[1], p0[1], p1[1], p1[1]],
[p0[2], p1[2], p0[2], p1[2], p0[2], p1[2], p0[2], p1[2]]
])
e = np.array([
[2, 3, 0, 0, 3, 3, 0, 1, 2, 3, 4, 4, 7, 7],
[7, 6, 1, 2, 1, 2, 4, 5, 6, 7, 5, 6, 5, 6]
], dtype=np.uint8)
return v, e
classes = (
'Unknown', 'Compacts', 'Sedans', 'SUVs', 'Coupes',
'Muscle', 'SportsClassics', 'Sports', 'Super', 'Motorcycles',
'OffRoad', 'Industrial', 'Utility', 'Vans', 'Cycles',
'Boats', 'Helicopters', 'Planes', 'Service', 'Emergency',
'Military', 'Commercial', 'Trains'
)
files = glob('deploy/*/*/*_image.jpg')
idx = np.random.randint(0, len(files))
snapshot = files[idx]
print(snapshot)
img = plt.imread(snapshot)
xyz = np.fromfile(snapshot.replace('_image.jpg', '_cloud.bin'), dtype=np.float32)
xyz = xyz.reshape([3, -1])
proj = np.fromfile(snapshot.replace('_image.jpg', '_proj.bin'), dtype=np.float32)
proj.resize([3, 4])
try:
bbox = np.fromfile(snapshot.replace('_image.jpg', '_bbox.bin'), dtype=np.float32)
except FileNotFoundError:
print('[*] bbox not found.')
bbox = np.array([], dtype=np.float32)
bbox = bbox.reshape([-1, 11])
uv = proj @ np.vstack([xyz, np.ones_like(xyz[0, :])])
uv = uv / uv[2, :]
clr = np.linalg.norm(xyz, axis=0)
fig1 = plt.figure(1, figsize=(16, 9))
ax1 = fig1.add_subplot(1, 1, 1)
ax1.imshow(img)
ax1.scatter(uv[0, :], uv[1, :], c=clr, marker='.', s=1)
ax1.axis('scaled')
fig1.tight_layout()
fig2 = plt.figure(2, figsize=(8, 8))
ax2 = Axes3D(fig2)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
step = 5
ax2.scatter(
xyz[0, ::step], xyz[1, ::step], xyz[2, ::step],
c=clr[::step], marker='.', s=1
)
colors = ['C{:d}'.format(i) for i in range(10)]
for k, b in enumerate(bbox):
R = rot(b[0:3])
t = b[3:6]
sz = b[6:9]
vert_3D, edges = get_bbox(-sz / 2, sz / 2)
vert_3D = R @ vert_3D + t[:, np.newaxis]
vert_2D = proj @ np.vstack([vert_3D, np.ones(vert_3D.shape[1])])
vert_2D = vert_2D / vert_2D[2, :]
clr = colors[np.mod(k, len(colors))]
for e in edges.T:
ax1.plot(vert_2D[0, e], vert_2D[1, e], color=clr)
ax2.plot(vert_3D[0, e], vert_3D[1, e], vert_3D[2, e], color=clr)
c = classes[int(b[9])]
ignore_in_eval = bool(b[10])
if ignore_in_eval:
ax2.text(t[0], t[1], t[2], c, color='r')
else:
ax2.text(t[0], t[1], t[2], c)
ax2.auto_scale_xyz([-40, 40], [-40, 40], [0, 80])
ax2.view_init(elev=-30, azim=-90)
for e in np.identity(3):
ax2.plot([0, e[0]], [0, e[1]], [0, e[2]], color=e)
plt.show()