-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathapp.py
More file actions
342 lines (258 loc) · 10.2 KB
/
Copy pathapp.py
File metadata and controls
342 lines (258 loc) · 10.2 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os, sys, shutil
from typing import List
import tempfile
import spaces
from huggingface_hub import hf_hub_download
# Temp file bug of gradio
BASE_TMP_DIR = os.path.abspath("./.gradio_cache")
os.makedirs(BASE_TMP_DIR, exist_ok=True)
os.environ["TMPDIR"] = BASE_TMP_DIR
os.environ["TEMP"] = BASE_TMP_DIR
os.environ["TMP"] = BASE_TMP_DIR
os.environ["GRADIO_TEMP_DIR"] = BASE_TMP_DIR
tempfile.tempdir = BASE_TMP_DIR
import gradio as gr
# Import your existing project code
root_path = os.path.abspath(".")
sys.path.append(root_path)
from omnishotcut.util.visualization import visualize_concated_frames
from omnishotcut.label_correspondence import unique_intra_label_mapping, unique_inter_label_mapping
from omnishotcut.engine import single_video_inference, load_model
# -------------------------
# Global cache / constants
# -------------------------
INTRA_ID2NAME = {v: k for k, v in unique_intra_label_mapping.items()}
INTER_ID2NAME = {v: k for k, v in unique_inter_label_mapping.items()}
# Fixed demo config
DEFAULT_CHECKPOINT_PATH = "checkpoints/OmniShotCut_ckpt.pth"
DEFAULT_NUM_CONTEXT_FRAMES = 20
DEFAULT_MAX_FRAMES_PER_IMG = 132 # For the visualization
VIS_DIR = "demo_video_results"
# Public URL safe setting
MAX_GALLERY_PAGES = 20
# Prepare the checkpoint — download from HF Hub into local cache if not present
checkpoint_path = hf_hub_download(
repo_id="uva-cv-lab/OmniShotCut",
filename="OmniShotCut_ckpt.pth",
)
model, model_args = load_model(checkpoint_path)
######################## Gallery Prepare ########################
def escape_html(x):
x = "" if x is None else str(x)
return (
x.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace('"', """)
.replace("'", "'")
)
def prepare_gallery(page_paths: List[str]):
gallery_items = []
for page_idx, page_path in enumerate(page_paths):
gallery_items.append((page_path, f"Page {page_idx}"))
return gallery_items
def prepare_result_table(
pred_ranges: List[List[int]],
pred_intra_labels: List[int],
pred_inter_labels: List[int],
fps: float,
) -> str:
headers = [
"Index",
"Start Frame",
"End Frame",
"Start Time (s)",
"End Time (s)",
"Intra Label",
"Inter Label",
]
html = """
<div class="result-table-wrap">
<table class="result-table">
<thead>
<tr>
"""
for h in headers:
html += f"<th>{escape_html(h)}</th>"
html += """
</tr>
</thead>
<tbody>
"""
for idx, pred_range in enumerate(pred_ranges):
start_frame = int(pred_range[0])
end_frame = int(pred_range[1])
intra_id = int(pred_intra_labels[idx]) if idx < len(pred_intra_labels) else -1
inter_id = int(pred_inter_labels[idx]) if idx < len(pred_inter_labels) else -1
row = [
idx,
start_frame,
end_frame,
round(start_frame / fps, 3) if fps and fps > 0 else "",
round(end_frame / fps, 3) if fps and fps > 0 else "",
INTRA_ID2NAME.get(intra_id, str(intra_id)),
INTER_ID2NAME.get(inter_id, str(inter_id)),
]
html += "<tr>"
for item in row:
html += f"<td>{escape_html(item)}</td>"
html += "</tr>"
html += """
</tbody>
</table>
</div>
"""
return html
def list_sample_videos(asset_dir: str = "__assets__", max_samples: int = 8) -> List[List[str]]:
script_dir = os.path.dirname(os.path.abspath(__file__))
asset_dir = os.path.join(script_dir, asset_dir)
if not os.path.isdir(asset_dir):
return []
mp4_paths = []
for name in sorted(os.listdir(asset_dir)):
path = os.path.join(asset_dir, name)
if os.path.isfile(path) and name.lower().endswith(".mp4"):
mp4_paths.append([path])
return mp4_paths[:max_samples]
sample_videos = list_sample_videos("__assets__", max_samples = 16)
@spaces.GPU(duration=120)
def run_demo(video_file):
if video_file is None:
raise gr.Error("Please upload a video first.")
video_path = video_file if isinstance(video_file, str) else video_file.name
if not os.path.exists(video_path):
raise gr.Error(f"Video file does not exist: {video_path}")
# Read the setting
num_context_frames = DEFAULT_NUM_CONTEXT_FRAMES
max_frames_per_img = DEFAULT_MAX_FRAMES_PER_IMG
print("Start processing the video", video_path)
pred_ranges, pred_intra_labels, pred_inter_labels, video_np_full, fps = single_video_inference(
video_path = video_path,
model = model,
model_args = model_args,
overlap_window_length = int(num_context_frames),
)
print("Finish running the video")
# Prepare the folder
if os.path.exists(VIS_DIR):
shutil.rmtree(VIS_DIR)
os.makedirs(VIS_DIR)
# Visualize and store (Must Do!)
page_paths = visualize_concated_frames(
frames = video_np_full,
out_dir = VIS_DIR,
highlight_ranges_closed = pred_ranges,
max_frames_per_img = int(max_frames_per_img),
end_range_exclusive = True,
fps = fps,
start_index = 0,
)
gallery_paths = page_paths[:MAX_GALLERY_PAGES]
result_table = prepare_result_table(
pred_ranges = pred_ranges,
pred_intra_labels = pred_intra_labels,
pred_inter_labels = pred_inter_labels,
fps = fps,
)
print("Visualization pages:", len(page_paths))
print("Shown visualization pages:", len(gallery_paths))
print("Predicted shots:", len(pred_ranges))
return gr.update(value = prepare_gallery(gallery_paths)), gr.update(value = result_table)
def clear_demo_outputs():
return gr.update(value = []), gr.update(value = "")
# -------------------------
# UI Design
# -------------------------
custom_css = """
#visual_gallery img {
object-fit: contain !important;
}
#visual_gallery .thumbnail-item {
object-fit: contain !important;
}
#visual_gallery .grid-wrap {
align-items: start !important;
}
.result-table-wrap {
width: 100%;
max-height: 360px;
overflow: auto;
border: 1px solid #e5e7eb;
border-radius: 10px;
}
.result-table {
width: 100%;
border-collapse: collapse;
font-size: 14px;
}
.result-table th {
position: sticky;
top: 0;
background: #f9fafb;
border-bottom: 1px solid #e5e7eb;
padding: 8px 10px;
text-align: left;
white-space: nowrap;
}
.result-table td {
border-bottom: 1px solid #f1f5f9;
padding: 8px 10px;
white-space: nowrap;
}
.result-table tr:hover {
background: #f9fafb;
}
"""
with gr.Blocks(title="OmniShotCut Demo", css = custom_css) as demo:
# Head title
gr.Markdown(
"""
<div align="center">
# OmniShotCut: Shot-Query-based Video Transformer for Shot Boundary Detection
**A sensitive and more informative SoTA shot boundary detection model.**
<p style="white-space: nowrap;">
<a href="https://arxiv.org/abs/2505.21491"><img src="https://img.shields.io/badge/arXiv-Paper-b31b1b?logo=arxiv&logoColor=white"></a>
<a href="https://uva-computer-vision-lab.github.io/Frame-In-N-Out/"><img src="https://img.shields.io/badge/Project-Website-pink?logo=googlechrome&logoColor=white"></a>
<a href="https://huggingface.co/spaces/HikariDawn/FrameINO"><img src="https://img.shields.io/static/v1?label=%F0%9F%A4%97%20HF%20Space&message=Online+Demo&color=orange"></a>
</p>
</div>
---
Upload a video and click **Run Inference**.
"""
)
with gr.Row():
with gr.Column(scale=1):
video_input = gr.Video(label = "Input Video", height = 480)
run_button = gr.Button("Run Inference", variant="primary")
with gr.Column(scale=1):
gr.Markdown("## Visualization")
gallery = gr.Gallery(
label = None,
columns = 1,
height = 760,
preview = True,
elem_id = "visual_gallery",
object_fit = "contain",
)
gr.Markdown("## Predicted Shot Results")
result_table = gr.HTML(
value = "",
elem_id = "result_table",
)
gr.Markdown("## Sample Videos")
gr.Examples(
examples = sample_videos,
inputs = [video_input],
label = "Choose a sample video",
)
run_button.click(
fn = clear_demo_outputs,
inputs = [],
outputs = [gallery, result_table],
).then(
fn = run_demo,
inputs =[video_input],
outputs = [gallery, result_table],
)
if __name__ == "__main__":
demo.launch(share=True)