forked from zzzxxxttt/simple_kitti_visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbox_to_img.py
46 lines (37 loc) · 1.46 KB
/
bbox_to_img.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
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from skimage import io
from matplotlib.patches import Rectangle
colors = sns.color_palette('Paired', 9 * 2)
names = ['Car', 'Van', 'Truck', 'Pedestrian', 'Person_sitting', 'Cyclist', 'Tram', 'Misc', 'DontCare']
file_id = '000010'
if __name__ == '__main__':
# load image
img = np.array(io.imread(f'examples\\kitti\\image_2\\{file_id}.png'), dtype=np.int32)
# load labels
with open(f'examples\\kitti\\label_2\\{file_id}.txt', 'r') as f:
labels = f.readlines()
# load calibration file
with open(f'examples\\kitti\\calib\\{file_id}.txt', 'r') as f:
lines = f.readlines()
P2 = np.array(lines[2].strip().split(' ')[1:], dtype=np.float32).reshape(3, 4)
fig = plt.figure()
# draw image
plt.imshow(img)
for line in labels:
line = line.split()
lab, _, _, _, x1, y1, x2, y2, _, _, _, _, _, _, _ = line
x1, y1, x2, y2 = map(float, [x1, y1, x2, y2])
if lab != 'DontCare':
plt.gca().add_patch(Rectangle((x1, y1), x2 - x1, y2 - y1,
linewidth=2,
edgecolor=colors[names.index(lab)],
facecolor='none'))
plt.text(x1 + 3, y1 + 3, lab,
bbox=dict(facecolor=colors[names.index(lab)], alpha=0.5),
fontsize=7, color='k')
plt.axis('off')
plt.tight_layout()
plt.savefig('examples/kitti_bbox_to_img.png', bbox_inches='tight')
plt.show()