Upload 3 files
Browse files- data/anno_val_direct.json +0 -0
- data/anno_val_indirect.json +0 -0
- visualize_data.py +146 -0
data/anno_val_direct.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
data/anno_val_indirect.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
visualize_data.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from pathlib import Path
|
7 |
+
from pprint import pprint
|
8 |
+
from omegaconf import OmegaConf
|
9 |
+
from PIL import Image, ImageDraw
|
10 |
+
import streamlit as st
|
11 |
+
# %%
|
12 |
+
|
13 |
+
os.environ['ROOT'] = '/mnt/Documents/traffic_var_server/visualization'
|
14 |
+
print("os.environ['ROOT'] :",os.environ['ROOT'])
|
15 |
+
# %%
|
16 |
+
class ImageRetriever:
|
17 |
+
|
18 |
+
def __init__(self, root_path, anno_path):
|
19 |
+
self.root_path = Path(root_path)
|
20 |
+
self.anno = json.load(open(anno_path))
|
21 |
+
|
22 |
+
def key2img_path(self, key):
|
23 |
+
file_paths = [
|
24 |
+
self.root_path / f"var_images/{key}.jpg",
|
25 |
+
self.root_path / f"var_images/{key}.png",
|
26 |
+
self.root_path / f"images/{key}.jpg",
|
27 |
+
self.root_path / f"img/train/{key.split('_')[0]}/{key}.png",
|
28 |
+
self.root_path / f"img/val/{key.split('_')[0]}/{key}.png",
|
29 |
+
self.root_path / f"img/test/{key.split('_')[0]}/{key}.png",
|
30 |
+
self.root_path / f"img/{key}.png",
|
31 |
+
self.root_path / f"img/{key}.jpg",
|
32 |
+
self.root_path / f"{key}.png",
|
33 |
+
self.root_path / f"{key}.jpg",
|
34 |
+
]
|
35 |
+
print("file_paths!!!!!!!!", file_paths)
|
36 |
+
for file_path in file_paths:
|
37 |
+
if file_path.exists():
|
38 |
+
return file_path
|
39 |
+
|
40 |
+
|
41 |
+
def key2img(self, key, draw_bbox=True):
|
42 |
+
file_path = self.key2img_path(key)
|
43 |
+
|
44 |
+
print("file_path!!@@@@", key, file_path)
|
45 |
+
|
46 |
+
image = Image.open(file_path)
|
47 |
+
if draw_bbox:
|
48 |
+
meta = self.anno[key]['details'][-1]
|
49 |
+
bboxes = [meta['bounding_box'].get(str(box_idx + 1), None) for box_idx in range(3)]
|
50 |
+
image = self.hide_region(image, bboxes)
|
51 |
+
return image
|
52 |
+
|
53 |
+
def hide_region(self, image, bboxes):
|
54 |
+
self.hide_true_bbox = 2
|
55 |
+
|
56 |
+
image = image.convert('RGBA')
|
57 |
+
|
58 |
+
if self.hide_true_bbox == 1: # hide mode
|
59 |
+
draw = ImageDraw.Draw(image, 'RGBA')
|
60 |
+
|
61 |
+
if self.hide_true_bbox in [2, 5, 7, 8, 9]: #highlight mode
|
62 |
+
overlay = Image.new('RGBA', image.size, '#00000000')
|
63 |
+
draw = ImageDraw.Draw(overlay, 'RGBA')
|
64 |
+
|
65 |
+
if self.hide_true_bbox == 3 or self.hide_true_bbox == 6: #blackout mode or position only mode
|
66 |
+
overlay = Image.new('RGBA', image.size, '#7B7575ff')
|
67 |
+
draw = ImageDraw.Draw(overlay, 'RGBA')
|
68 |
+
|
69 |
+
color_fill_list = ['#ff05cd3c', '#00F1E83c', '#F2D4003c'] # Green, Blue, Yellow?
|
70 |
+
|
71 |
+
for idx, bbox in enumerate(bboxes):
|
72 |
+
if bbox == None:
|
73 |
+
continue
|
74 |
+
|
75 |
+
color_fill = color_fill_list[idx]
|
76 |
+
x, y = bbox['left'], bbox['top']
|
77 |
+
|
78 |
+
if self.hide_true_bbox == 1: # hide mode
|
79 |
+
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill='#7B7575')
|
80 |
+
elif self.hide_true_bbox in [2, 5, 7, 8, 9]: # highlight mode
|
81 |
+
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill=color_fill, outline='#05ff37ff',
|
82 |
+
width=3) # Fill with Pink 60% ##00F1E8
|
83 |
+
elif self.hide_true_bbox == 3: # blackout mode
|
84 |
+
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill='#00000000')
|
85 |
+
elif self.hide_true_bbox == 6: # position only mode
|
86 |
+
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill=color_fill)
|
87 |
+
|
88 |
+
if self.hide_true_bbox in [2, 3, 5, 6, 7, 8, 9]:
|
89 |
+
image = Image.alpha_composite(image, overlay)
|
90 |
+
return image
|
91 |
+
|
92 |
+
def retrive_data(file_index, mode='direct'):
|
93 |
+
split = 'val'
|
94 |
+
mode = mode.lower()
|
95 |
+
main_dataset = json.load(open(os.path.join(os.environ['ROOT'], f"data/anno_{split}_{mode}.json")))
|
96 |
+
temp_data = main_dataset[list(main_dataset.keys())[file_index]]['details'][-1]
|
97 |
+
|
98 |
+
message_dict = {}
|
99 |
+
|
100 |
+
message_dict['img'] = temp_data['img']
|
101 |
+
message_dict['plausible_speed'] = temp_data['plausible_speed']
|
102 |
+
message_dict['bounding_box'] = temp_data['bounding_box']
|
103 |
+
try:
|
104 |
+
message_dict['hazard'] = temp_data['hazard']
|
105 |
+
except:
|
106 |
+
message_dict['hazard'] = temp_data['rationale']
|
107 |
+
message_dict['Entity #1'] = temp_data['Entity #1']
|
108 |
+
message_dict['Entity #2'] = temp_data['Entity #2']
|
109 |
+
message_dict['Entity #3'] = temp_data['Entity #3']
|
110 |
+
|
111 |
+
img_retriever = ImageRetriever(
|
112 |
+
root_path=os.path.join(os.environ['ROOT'], ''),
|
113 |
+
anno_path=os.path.join(os.environ['ROOT'], f'data/anno_{split}_{mode}.json'),
|
114 |
+
)
|
115 |
+
img = img_retriever.key2img(list(main_dataset.keys())[file_index])
|
116 |
+
img = img.resize((img.width // 2, img.height // 2))
|
117 |
+
|
118 |
+
return img, message_dict
|
119 |
+
|
120 |
+
# %%
|
121 |
+
if __name__ == '__main__':
|
122 |
+
st.title("DHPR: Driving Hazard Prediction and Reasoning")
|
123 |
+
st.subheader("Data Visualization")
|
124 |
+
|
125 |
+
option = st.selectbox(
|
126 |
+
'Select the hazard type',
|
127 |
+
('Direct', 'Indirect'))
|
128 |
+
|
129 |
+
st.write('You selected:', option)
|
130 |
+
|
131 |
+
image_index = st.slider('Please Select The Image Index', 0, 999, 0)
|
132 |
+
st.write("You select the data index of ", image_index," for visualization from the validation set")
|
133 |
+
|
134 |
+
img, message_dict = retrive_data(image_index, option)
|
135 |
+
|
136 |
+
st.write("---")
|
137 |
+
|
138 |
+
st.image(img)
|
139 |
+
st.subheader("Annotation Details")
|
140 |
+
st.json(message_dict)
|
141 |
+
st.write('---')
|
142 |
+
# !streamlit run visualize_data.py --server.fileWatcherType none
|
143 |
+
|
144 |
+
|
145 |
+
# %%
|
146 |
+
|