oValach commited on
Commit
cb4c2b2
·
verified ·
1 Parent(s): e325728

Delete example_vis.py

Browse files
Files changed (1) hide show
  1. example_vis.py +0 -118
example_vis.py DELETED
@@ -1,118 +0,0 @@
1
- # Copyright Oliver Zendel, AIT Austrian Institute of Technology Gmbh 2019
2
- # Example visualization tool to illustrate interpretation of RailSem19 data usage
3
-
4
- import sys, os, glob, math, argparse
5
- import numpy as np
6
- import cv2, json
7
- import matplotlib.pyplot as plt
8
-
9
- rs19_label2bgr = {"buffer-stop": (70,70,70),
10
- "crossing": (128,64,128),
11
- "guard-rail": (0,255,0),
12
- "train-car" : (100,80,0),
13
- "platform" : (232,35,244),
14
- "rail": (255,255,0),
15
- "switch-indicator": (127,255,0),
16
- "switch-left": (255,255,0),
17
- "switch-right": (127,127,0),
18
- "switch-unknown": (191,191,0),
19
- "switch-static": (0,255,127),
20
- "track-sign-front" : (0,220,220),
21
- "track-signal-front" : (30,170,250),
22
- "track-signal-back" : (0,85,125),
23
- #rail occluders
24
- "person-group" : (60,20,220),
25
- "car" : (142,0,0),
26
- "fence" : (153,153,190),
27
- "person" : (60,20,220),
28
- "pole" : (153,153,153),
29
- "rail-occluder" : (255,255,255),
30
- "truck" : (70,0,0)
31
- }
32
-
33
- def files_in_subdirs(start_dir, pattern = ["*.png","*.jpg","*.jpeg"]):
34
- files = []
35
- for p in pattern:
36
- for dir,_,_ in os.walk(start_dir):
37
- files.extend(glob.glob(os.path.join(dir,p)))
38
- return files
39
-
40
- def config_to_rgb(inp_path_config_json, default_col = [255,255,255]):
41
- lut = []
42
- inp_json = json.load(open(inp_path_config_json, 'r'))
43
- for c in range(3): #for each color channel
44
- lut_c =[l["color"][c] for l in inp_json["labels"]]+[default_col[c]]*(256-len(inp_json["labels"]))
45
- lut.append(np.asarray(lut_c, dtype=np.uint8))
46
- return lut
47
-
48
- def corss_hatch_rail(im_vis, coords, color_left=(255,255,0), color_right=(127,127,0)):
49
- ml = min(len(coords[0]), len(coords[1]))
50
- for i in range(ml):
51
- midpnt = ((coords[0][i][0]+coords[1][i][0])//2, (coords[0][i][1]+coords[1][i][1])//2)
52
- cv2.line(im_vis, tuple(coords[0][i]), midpnt, color_left)
53
- cv2.line(im_vis, midpnt, tuple(coords[1][i]), color_right)
54
-
55
- def json_to_img(inp_path_json, line_thickness=2):
56
- inp_json = json.load(open(inp_path_json, 'r'))
57
- im_json = np.zeros((inp_json["imgHeight"], inp_json["imgWidth"],3), dtype=np.uint8)
58
- for obj in inp_json["objects"]:
59
- col = rs19_label2bgr.get(obj["label"],[255,255,255])
60
- if "boundingbox" in obj:
61
- cv2.rectangle(im_json, tuple(obj["boundingbox"][0:2]), tuple(obj["boundingbox"][2:4]), col, line_thickness)
62
- elif "polygon" in obj:
63
- pnts_draw = np.around(np.array(obj["polygon"])).astype(np.int32)
64
- cv2.polylines(im_json, [pnts_draw], True, col, line_thickness)
65
- elif "polyline-pair" in obj:
66
- #left rail of a rail pair has index 0, right rail of a rail pair has index 1
67
- rails_draw = [np.around(np.array(obj["polyline-pair"][i])).astype(np.int32) for i in range(2)]
68
- corss_hatch_rail(im_json, obj["polyline-pair"], rs19_label2bgr['switch-left'], rs19_label2bgr['switch-right'])
69
- cv2.polylines(im_json, rails_draw, False, col)
70
- elif "polyline" in obj:
71
- rail_draw = np.around(np.array(obj["polyline"])).astype(np.int32)
72
- cv2.polylines(im_json, [rail_draw], False, col, line_thickness)
73
- return im_json, inp_json["frame"]
74
-
75
- def get_joined_img(inp_path_json, jpg_folder, uint8_folder, lut_bgr, blend_vals=[0.65,0.25,0.1]):
76
- im_json, frameid = json_to_img(inp_path_json, line_thickness=2) #visualize geometric annotations
77
- inp_path_jpg = os.path.join(jpg_folder,frameid+".jpg")
78
- inp_path_uint8 = os.path.join(uint8_folder,frameid+".png")
79
- im_jpg = cv2.imread(inp_path_jpg) #add intensity image as background
80
- im_id_map = cv2.imread(inp_path_uint8,cv2.IMREAD_GRAYSCALE) #get semantic label map
81
- im_id_col = np.zeros((im_id_map.shape[0], im_id_map.shape[1], 3), np.uint8)
82
- for c in range(3):
83
- im_id_col[:,:,c] = lut_bgr[c][im_id_map] #apply color coding
84
-
85
- #plt.imshow(im_id_col)
86
- #plt.show()
87
-
88
- blend_sources = (im_jpg*blend_vals[0]+im_id_col*blend_vals[1]+im_json*blend_vals[2]).astype(np.uint8)
89
- return (blend_sources) #blend all three data sources
90
-
91
- def vis_all_json(json_folder, jpg_folder, uint8_folder, inp_path_config_json):
92
- all_json = files_in_subdirs(json_folder, pattern = ["*.json"])
93
- lut_bgr = config_to_rgb(inp_path_config_json, default_col = [255,255,255])[::-1] #we need to swap color channels as opencv uses BGR
94
- curr_idx, retKey = 0, ord('a')
95
- while retKey > ord(' '):
96
- im_vis = get_joined_img(all_json[curr_idx], jpg_folder, uint8_folder, lut_bgr)
97
- cv2.putText(im_vis, all_json[curr_idx],(0,25), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255), 2)
98
- cv2.imshow("RailSem19 Annotation Visualization (use 'a' or 'd' to navigate, ESC to quit)",im_vis)
99
- retKey = cv2.waitKey(-1) #use 'a' and 'd' to scroll through frames
100
- curr_idx = curr_idx - 1 if retKey == ord('a') else curr_idx + 1
101
- curr_idx = (curr_idx + len(all_json)) % len(all_json) #wrap around
102
- return 0
103
-
104
- def main(argv=sys.argv[1:]):
105
- parser = argparse.ArgumentParser()
106
- parser.add_argument('--jpgs', type=str, default="RailNet_DT/rs19_val/jpgs/rs19_val",
107
- help="Folder containing all RGB intensity images")
108
- parser.add_argument('--jsons', type=str, default="RailNet_DT/rs19_val/jsons/rs19_val",
109
- help="Folder containing all geometry-based annotation files")
110
- parser.add_argument('--uint8', type=str, default="RailNet_DT/rs19_val/uint8/rs19_val",
111
- help="Folder containing all dense semantic segm. id maps")
112
- parser.add_argument('--config', type=str, default="RailNet_DT/rs19_val/rs19-config.json",
113
- help="Path to config json for dense label map interpretation")
114
- args = parser.parse_args(argv)
115
- return vis_all_json(args.jsons, args.jpgs, args.uint8, args.config)
116
-
117
- if __name__ == "__main__":
118
- sys.exit(main())