-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvisualize.py
53 lines (50 loc) · 1.45 KB
/
visualize.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
import numpy as np
from tqdm import tqdm
import math
from PIL import Image
import os
def imread(path):
label = np.array(Image.open(path))
return label
def get_palette():
unlabelled = [0, 0, 0]
car = [64, 0, 128]
person = [64, 64, 0]
bike = [0, 128, 192]
curve = [0, 0, 192]
car_stop = [128, 128, 0]
guardrail = [64, 64, 128]
color_cone = [192, 128, 128]
bump = [192, 64, 0]
palette = np.array(
[
unlabelled,
car,
person,
bike,
curve,
car_stop,
guardrail,
color_cone,
bump,
]
)
return palette
def visualize(save_name, label):
palette = get_palette()
pred = label
img = np.zeros((pred.shape[0], pred.shape[1], 3), dtype=np.uint8)
for cid in range(1, int(label.max()+1)):
img[pred == cid] = palette[cid]
img = Image.fromarray(np.uint8(img))
img.save(save_name)
if __name__ == '__main__':
label_dir = r'./test/Segmentation_labels' #label所在文件夹
save_dir = r'./test/Segmentation_visualize' ## 可视化结果保存的文件夹
os.makedirs(save_dir, exist_ok=True)
file_list = os.listdir(label_dir)
for item in file_list:
file_path = os.path.join(label_dir, item)
save_path = os.path.join(save_dir, item)
label = imread(file_path)
visualize(save_path, label)