ercanburak commited on
Commit
58538bf
·
1 Parent(s): 8b5d61a

refactor, add cachce

Browse files
Files changed (1) hide show
  1. app.py +131 -127
app.py CHANGED
@@ -18,6 +18,135 @@ from utils import get_configs, get_display_names, get_path_for_viz, get_video_he
18
  # st.markdown("Page: " + paper_link, unsafe_allow_html=True)
19
  # st.markdown("Please see this video for instructions on how to use this tool: " + instructions_video, unsafe_allow_html=True)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  st.title("Result Analysis Tool")
22
 
23
  font_path = "font/Ubuntu-B.ttf"
@@ -73,134 +202,9 @@ selected_visualizations = [viz for viz in visualizations if viz['display_name']
73
  if not st.button('Get Results'):
74
  st.stop()
75
 
76
- st.write("Retrieving results...")
77
- progress_bar = st.progress(0)
78
-
79
-
80
- gt_only_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'gt_only']
81
- model_only_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'model_only']
82
- both_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'both']
83
-
84
- recon_viz = {"name": "recon", "display_name": "Reconstruction", "viz_type": "both", "gt_type": "frame"}
85
- ground_truth = {"name": "gt", "display_name": "Ground Truth", "model_id": "groundtruth"}
86
-
87
- model_viz = [recon_viz] + both_viz + selected_metrics + model_only_viz
88
- num_model_rows = len(model_viz)
89
-
90
- gt_viz = []
91
- if selected_dataset['has_frames']:
92
- gt_viz.append(recon_viz)
93
- gt_viz.extend([viz for viz in both_viz if viz['gt_type'] == 'frame'])
94
- gt_viz.extend([viz for viz in gt_only_viz if viz['gt_type'] == 'frame'])
95
-
96
- gt_viz.extend([viz for viz in both_viz if viz['gt_type'] == 'event'])
97
- gt_viz.extend([viz for viz in gt_only_viz if viz['gt_type'] == 'event'])
98
-
99
- num_gt_rows = len(gt_viz)
100
- num_rows = max(num_model_rows, num_gt_rows)
101
-
102
- total_videos_needed = len(selected_models) * num_model_rows + num_gt_rows
103
-
104
- if len(gt_viz) > 0:
105
- selected_models.append(ground_truth)
106
-
107
- padding = 2
108
- font_size = 20
109
- num_cols = len(selected_models)
110
- crop_str = "crop=trunc(iw/2)*2:trunc(ih/2)*2"
111
- pad_str = "pad=ceil(iw/2)*2+{}:ceil(ih/2)*2+{}:{}:{}:white".format(padding*2, padding*2, padding, padding)
112
- num_elements = num_rows * num_cols
113
-
114
- # remove previous temp data
115
- files = glob.glob('temp_data/temp_*.mp4')
116
- for f in files:
117
- os.remove(f)
118
-
119
- w = selected_dataset["width"]
120
- h = selected_dataset["height"]
121
- input_filter_parts = []
122
- xstack_input_parts = []
123
- layout_parts = []
124
- video_paths = []
125
- row_heights = [""]*num_rows
126
- gt_viz_indices = []
127
- if len(model_viz) > 1:
128
- left_pad = (font_size*0.7)*max([len(viz['display_name']) for viz in model_viz[1:]]) + padding*2
129
- else:
130
- left_pad = 0
131
- for row_idx in range(num_rows):
132
- for col_idx in range(num_cols):
133
- vid_idx = len(video_paths)
134
- progress_bar.progress(float(vid_idx) / total_videos_needed)
135
- cur_model = selected_models[col_idx]
136
- if cur_model['name'] == "gt":
137
- if row_idx < len(gt_viz):
138
- video_path = get_path_for_viz(base_data_dir, selected_dataset, selected_sequence, cur_model, gt_viz[row_idx])
139
- if not os.path.isfile(video_path):
140
- raise ValueError("Could not find video: " + video_path)
141
- gt_viz_indices.append(vid_idx)
142
- else:
143
- continue
144
- else:
145
- if row_idx < len(model_viz):
146
- video_path = get_path_for_viz(base_data_dir, selected_dataset, selected_sequence, cur_model, model_viz[row_idx])
147
- if not os.path.isfile(video_path):
148
- raise ValueError("Could not find video: " + video_path)
149
- else:
150
- continue
151
-
152
- if row_heights[row_idx] == "":
153
- row_heights[row_idx] = "h{}".format(vid_idx)
154
- if row_idx == 0:
155
- pad_height = font_size+padding*2
156
- pad_txt_str = ",pad={}:{}:0:{}:white".format(w+padding*2, h+font_size+padding*4, pad_height)
157
- text_str = get_text_str(pad_height, w, cur_model['display_name'], font_path, font_size)
158
- pad_txt_str = pad_txt_str + "," + text_str
159
- elif row_idx > 0 and col_idx == 0:
160
- pad_txt_str = ",pad={}:ih:{}:0:white".format(w + left_pad + padding*2, left_pad)
161
- text_str = get_text_str("h", left_pad, model_viz[row_idx]['display_name'], font_path, font_size)
162
- pad_txt_str = pad_txt_str + "," + text_str
163
- else:
164
- pad_txt_str = ""
165
-
166
- input_filter_part = "[{}:v]scale={}:-1,{}{}[v{}]".format(vid_idx, w, pad_str, pad_txt_str, vid_idx)
167
- input_filter_parts.append(input_filter_part)
168
- xstack_input_part = "[v{}]".format(vid_idx)
169
- xstack_input_parts.append(xstack_input_part)
170
- video_paths.append(video_path)
171
- if row_idx == 0 or col_idx > 0:
172
- layout_w_parts = [str(left_pad)] + ["w{}".format(i) for i in range(col_idx)]
173
- layout_w = "+".join(layout_w_parts)
174
- else:
175
- layout_w = "+".join(["w{}".format(i) for i in range(col_idx)]) if col_idx > 0 else "0"
176
- if cur_model['name'] == "gt":
177
- layout_h = "+".join(["h{}".format(i) for i in gt_viz_indices[:-1]]) if row_idx > 0 else "0"
178
- else:
179
- layout_h = "+".join(row_heights[:row_idx]) if row_idx > 0 else "0"
180
- layout_part = layout_w + "_" + layout_h
181
- layout_parts.append(layout_part)
182
-
183
- inputs_str = " ".join(["-i " + video_path for video_path in video_paths])
184
- num_inputs = len(video_paths)
185
-
186
- input_scaling_str = ";".join(input_filter_parts)
187
- xstack_input_str = "".join(xstack_input_parts)
188
- layout_str = "|".join(layout_parts)
189
-
190
- # opt = "-c:v libx264 -preset veryslow -crf 18 -c:a copy"
191
- opt = ""
192
- # opt_fill = ":fill=black"
193
- opt_fill = ":fill=white"
194
- # opt_fill = ""
195
- ffmpeg_command_str = "ffmpeg -y " + inputs_str + " -filter_complex \"" + input_scaling_str + ";" + xstack_input_str + "xstack=inputs=" + str(num_inputs) + ":layout=" + layout_str + opt_fill + "\"" + opt + " output.mp4"
196
- print(ffmpeg_command_str)
197
- ret = subprocess.call(ffmpeg_command_str, shell=True)
198
-
199
- if ret != 0:
200
  st.error("Error while generating video.")
201
  st.stop()
202
 
203
- video_file = open('output.mp4', 'rb')
204
- video_bytes = video_file.read()
205
-
206
  st.video(video_bytes)
 
18
  # st.markdown("Page: " + paper_link, unsafe_allow_html=True)
19
  # st.markdown("Please see this video for instructions on how to use this tool: " + instructions_video, unsafe_allow_html=True)
20
 
21
+
22
+ @st.cache_data(show_spinner="Retrieving results...")
23
+ def retrieve_results(selected_dataset, selected_sequence, selected_models, selected_metrics, selected_visualizations):
24
+ gt_only_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'gt_only']
25
+ model_only_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'model_only']
26
+ both_viz = [viz for viz in selected_visualizations if viz['viz_type'] == 'both']
27
+
28
+ recon_viz = {"name": "recon", "display_name": "Reconstruction", "viz_type": "both", "gt_type": "frame"}
29
+ ground_truth = {"name": "gt", "display_name": "Ground Truth", "model_id": "groundtruth"}
30
+
31
+ model_viz = [recon_viz] + both_viz + selected_metrics + model_only_viz
32
+ num_model_rows = len(model_viz)
33
+
34
+ gt_viz = []
35
+ if selected_dataset['has_frames']:
36
+ gt_viz.append(recon_viz)
37
+ gt_viz.extend([viz for viz in both_viz if viz['gt_type'] == 'frame'])
38
+ gt_viz.extend([viz for viz in gt_only_viz if viz['gt_type'] == 'frame'])
39
+
40
+ gt_viz.extend([viz for viz in both_viz if viz['gt_type'] == 'event'])
41
+ gt_viz.extend([viz for viz in gt_only_viz if viz['gt_type'] == 'event'])
42
+
43
+ num_gt_rows = len(gt_viz)
44
+ num_rows = max(num_model_rows, num_gt_rows)
45
+
46
+ # total_videos_needed = len(selected_models) * num_model_rows + num_gt_rows
47
+
48
+ if len(gt_viz) > 0:
49
+ selected_models.append(ground_truth)
50
+
51
+ padding = 2
52
+ font_size = 20
53
+ num_cols = len(selected_models)
54
+ crop_str = "crop=trunc(iw/2)*2:trunc(ih/2)*2"
55
+ pad_str = "pad=ceil(iw/2)*2+{}:ceil(ih/2)*2+{}:{}:{}:white".format(padding*2, padding*2, padding, padding)
56
+ num_elements = num_rows * num_cols
57
+
58
+ # remove previous temp data
59
+ files = glob.glob('temp_data/temp_*.mp4')
60
+ for f in files:
61
+ os.remove(f)
62
+
63
+ w = selected_dataset["width"]
64
+ h = selected_dataset["height"]
65
+ input_filter_parts = []
66
+ xstack_input_parts = []
67
+ layout_parts = []
68
+ video_paths = []
69
+ row_heights = [""]*num_rows
70
+ gt_viz_indices = []
71
+ if len(model_viz) > 1:
72
+ left_pad = (font_size*0.7)*max([len(viz['display_name']) for viz in model_viz[1:]]) + padding*2
73
+ else:
74
+ left_pad = 0
75
+ for row_idx in range(num_rows):
76
+ for col_idx in range(num_cols):
77
+ vid_idx = len(video_paths)
78
+ # progress_bar.progress(float(vid_idx) / total_videos_needed)
79
+ cur_model = selected_models[col_idx]
80
+ if cur_model['name'] == "gt":
81
+ if row_idx < len(gt_viz):
82
+ video_path = get_path_for_viz(base_data_dir, selected_dataset, selected_sequence, cur_model, gt_viz[row_idx])
83
+ if not os.path.isfile(video_path):
84
+ raise ValueError("Could not find video: " + video_path)
85
+ gt_viz_indices.append(vid_idx)
86
+ else:
87
+ continue
88
+ else:
89
+ if row_idx < len(model_viz):
90
+ video_path = get_path_for_viz(base_data_dir, selected_dataset, selected_sequence, cur_model, model_viz[row_idx])
91
+ if not os.path.isfile(video_path):
92
+ raise ValueError("Could not find video: " + video_path)
93
+ else:
94
+ continue
95
+
96
+ if row_heights[row_idx] == "":
97
+ row_heights[row_idx] = "h{}".format(vid_idx)
98
+ if row_idx == 0:
99
+ pad_height = font_size+padding*2
100
+ pad_txt_str = ",pad={}:{}:0:{}:white".format(w+padding*2, h+font_size+padding*4, pad_height)
101
+ text_str = get_text_str(pad_height, w, cur_model['display_name'], font_path, font_size)
102
+ pad_txt_str = pad_txt_str + "," + text_str
103
+ elif row_idx > 0 and col_idx == 0:
104
+ pad_txt_str = ",pad={}:ih:{}:0:white".format(w + left_pad + padding*2, left_pad)
105
+ text_str = get_text_str("h", left_pad, model_viz[row_idx]['display_name'], font_path, font_size)
106
+ pad_txt_str = pad_txt_str + "," + text_str
107
+ else:
108
+ pad_txt_str = ""
109
+
110
+ input_filter_part = "[{}:v]scale={}:-1,{}{}[v{}]".format(vid_idx, w, pad_str, pad_txt_str, vid_idx)
111
+ input_filter_parts.append(input_filter_part)
112
+ xstack_input_part = "[v{}]".format(vid_idx)
113
+ xstack_input_parts.append(xstack_input_part)
114
+ video_paths.append(video_path)
115
+ if row_idx == 0 or col_idx > 0:
116
+ layout_w_parts = [str(left_pad)] + ["w{}".format(i) for i in range(col_idx)]
117
+ layout_w = "+".join(layout_w_parts)
118
+ else:
119
+ layout_w = "+".join(["w{}".format(i) for i in range(col_idx)]) if col_idx > 0 else "0"
120
+ if cur_model['name'] == "gt":
121
+ layout_h = "+".join(["h{}".format(i) for i in gt_viz_indices[:-1]]) if row_idx > 0 else "0"
122
+ else:
123
+ layout_h = "+".join(row_heights[:row_idx]) if row_idx > 0 else "0"
124
+ layout_part = layout_w + "_" + layout_h
125
+ layout_parts.append(layout_part)
126
+
127
+ inputs_str = " ".join(["-i " + video_path for video_path in video_paths])
128
+ num_inputs = len(video_paths)
129
+
130
+ input_scaling_str = ";".join(input_filter_parts)
131
+ xstack_input_str = "".join(xstack_input_parts)
132
+ layout_str = "|".join(layout_parts)
133
+
134
+ # opt = "-c:v libx264 -preset veryslow -crf 18 -c:a copy"
135
+ opt = ""
136
+ # opt_fill = ":fill=black"
137
+ opt_fill = ":fill=white"
138
+ # opt_fill = ""
139
+ ffmpeg_command_str = "ffmpeg -y " + inputs_str + " -filter_complex \"" + input_scaling_str + ";" + xstack_input_str + "xstack=inputs=" + str(num_inputs) + ":layout=" + layout_str + opt_fill + "\"" + opt + " output.mp4"
140
+ print(ffmpeg_command_str)
141
+ ret = subprocess.call(ffmpeg_command_str, shell=True)
142
+
143
+ if ret != 0:
144
+ return None
145
+
146
+ video_file = open('output.mp4', 'rb')
147
+ video_bytes = video_file.read()
148
+ return video_bytes
149
+
150
  st.title("Result Analysis Tool")
151
 
152
  font_path = "font/Ubuntu-B.ttf"
 
202
  if not st.button('Get Results'):
203
  st.stop()
204
 
205
+ video_bytes = retrieve_results(selected_dataset, selected_sequence, selected_models, selected_metrics, selected_visualizations)
206
+ if video_bytes is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  st.error("Error while generating video.")
208
  st.stop()
209
 
 
 
 
210
  st.video(video_bytes)