Update app.py
Browse files
app.py
CHANGED
|
@@ -414,7 +414,7 @@ class VideoTagger:
|
|
| 414 |
|
| 415 |
if progress is not None:
|
| 416 |
if is_first_load:
|
| 417 |
-
progress(0.0, desc="Loading model...")
|
| 418 |
else:
|
| 419 |
progress(0.0, desc="Opening video...")
|
| 420 |
|
|
@@ -435,13 +435,14 @@ class VideoTagger:
|
|
| 435 |
if total_frames <= 0:
|
| 436 |
total_frames = 1
|
| 437 |
|
|
|
|
| 438 |
frames_to_process = max(1, (total_frames + frame_interval - 1) // frame_interval)
|
| 439 |
|
| 440 |
aggregated_general: Dict[str, float] = {}
|
| 441 |
aggregated_character: Dict[str, float] = {}
|
| 442 |
|
| 443 |
-
frame_idx = 0
|
| 444 |
-
processed_frames = 0
|
| 445 |
batch_tensors: List[np.ndarray] = []
|
| 446 |
|
| 447 |
try:
|
|
@@ -450,25 +451,44 @@ class VideoTagger:
|
|
| 450 |
if not ret:
|
| 451 |
break
|
| 452 |
|
| 453 |
-
# Only process every N-th frame
|
| 454 |
if frame_idx % frame_interval == 0:
|
| 455 |
-
#
|
|
|
|
| 456 |
arr = self._prepare_frame_bgr(frame) # (H, W, 3) float32
|
| 457 |
batch_tensors.append(arr)
|
| 458 |
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
|
|
|
|
|
|
|
|
|
| 462 |
progress(
|
| 463 |
-
|
| 464 |
desc=(
|
| 465 |
-
f"
|
| 466 |
-
f"({
|
| 467 |
),
|
| 468 |
)
|
| 469 |
|
| 470 |
-
# If batch is full, run inference
|
| 471 |
if len(batch_tensors) >= self.batch_size:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
num_done = self._run_batch_and_aggregate(
|
| 473 |
batch_tensors,
|
| 474 |
general_thresh=general_thresh,
|
|
@@ -484,8 +504,8 @@ class VideoTagger:
|
|
| 484 |
progress(
|
| 485 |
ratio,
|
| 486 |
desc=(
|
| 487 |
-
f"
|
| 488 |
-
f"{frames_to_process}..."
|
| 489 |
),
|
| 490 |
)
|
| 491 |
|
|
@@ -495,6 +515,21 @@ class VideoTagger:
|
|
| 495 |
|
| 496 |
# Process any leftover frames in the last partial batch
|
| 497 |
if batch_tensors:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
num_done = self._run_batch_and_aggregate(
|
| 499 |
batch_tensors,
|
| 500 |
general_thresh=general_thresh,
|
|
@@ -509,8 +544,8 @@ class VideoTagger:
|
|
| 509 |
progress(
|
| 510 |
ratio,
|
| 511 |
desc=(
|
| 512 |
-
f"
|
| 513 |
-
f"{frames_to_process}
|
| 514 |
),
|
| 515 |
)
|
| 516 |
|
|
@@ -822,7 +857,7 @@ with gr.Blocks(title=TITLE) as demo:
|
|
| 822 |
custom_theme = gr.themes.Default(
|
| 823 |
primary_hue=gr.themes.colors.blue,
|
| 824 |
secondary_hue=gr.themes.colors.slate,
|
| 825 |
-
radius_size=gr.themes.sizes.
|
| 826 |
font=[gr.themes.GoogleFont("Raleway")],
|
| 827 |
)
|
| 828 |
|
|
|
|
| 414 |
|
| 415 |
if progress is not None:
|
| 416 |
if is_first_load:
|
| 417 |
+
progress(0.0, desc="Loading model (first run may take a while)...")
|
| 418 |
else:
|
| 419 |
progress(0.0, desc="Opening video...")
|
| 420 |
|
|
|
|
| 435 |
if total_frames <= 0:
|
| 436 |
total_frames = 1
|
| 437 |
|
| 438 |
+
# Number of frames we will actually process (sampled every N frames)
|
| 439 |
frames_to_process = max(1, (total_frames + frame_interval - 1) // frame_interval)
|
| 440 |
|
| 441 |
aggregated_general: Dict[str, float] = {}
|
| 442 |
aggregated_character: Dict[str, float] = {}
|
| 443 |
|
| 444 |
+
frame_idx = 0 # index over all video frames
|
| 445 |
+
processed_frames = 0 # count of sampled frames fully processed by the model
|
| 446 |
batch_tensors: List[np.ndarray] = []
|
| 447 |
|
| 448 |
try:
|
|
|
|
| 451 |
if not ret:
|
| 452 |
break
|
| 453 |
|
|
|
|
| 454 |
if frame_idx % frame_interval == 0:
|
| 455 |
+
# This is a sampled frame
|
| 456 |
+
sampled_index = processed_frames + len(batch_tensors) + 1 # 1-based index among sampled frames
|
| 457 |
arr = self._prepare_frame_bgr(frame) # (H, W, 3) float32
|
| 458 |
batch_tensors.append(arr)
|
| 459 |
|
| 460 |
+
if progress is not None:
|
| 461 |
+
# Show which sampled frame we're preparing, and which raw video frame it is.
|
| 462 |
+
ratio = min(
|
| 463 |
+
(processed_frames + len(batch_tensors)) / frames_to_process,
|
| 464 |
+
0.99,
|
| 465 |
+
)
|
| 466 |
progress(
|
| 467 |
+
ratio,
|
| 468 |
desc=(
|
| 469 |
+
f"Preparing sampled frame {sampled_index}/{frames_to_process} "
|
| 470 |
+
f"(video frame {frame_idx + 1}/{total_frames})..."
|
| 471 |
),
|
| 472 |
)
|
| 473 |
|
| 474 |
+
# If batch is full, run inference on it
|
| 475 |
if len(batch_tensors) >= self.batch_size:
|
| 476 |
+
# Inform the user we're now running the model on this batch
|
| 477 |
+
if progress is not None:
|
| 478 |
+
start_sample = processed_frames + 1
|
| 479 |
+
end_sample = processed_frames + len(batch_tensors)
|
| 480 |
+
ratio = min(
|
| 481 |
+
(processed_frames + len(batch_tensors)) / frames_to_process,
|
| 482 |
+
0.99,
|
| 483 |
+
)
|
| 484 |
+
progress(
|
| 485 |
+
ratio,
|
| 486 |
+
desc=(
|
| 487 |
+
f"Running model on batch: sampled frames "
|
| 488 |
+
f"{start_sample}-{end_sample}/{frames_to_process}..."
|
| 489 |
+
),
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
num_done = self._run_batch_and_aggregate(
|
| 493 |
batch_tensors,
|
| 494 |
general_thresh=general_thresh,
|
|
|
|
| 504 |
progress(
|
| 505 |
ratio,
|
| 506 |
desc=(
|
| 507 |
+
f"Finished processing sampled frames "
|
| 508 |
+
f"{processed_frames}/{frames_to_process}..."
|
| 509 |
),
|
| 510 |
)
|
| 511 |
|
|
|
|
| 515 |
|
| 516 |
# Process any leftover frames in the last partial batch
|
| 517 |
if batch_tensors:
|
| 518 |
+
if progress is not None:
|
| 519 |
+
start_sample = processed_frames + 1
|
| 520 |
+
end_sample = processed_frames + len(batch_tensors)
|
| 521 |
+
ratio = min(
|
| 522 |
+
(processed_frames + len(batch_tensors)) / frames_to_process,
|
| 523 |
+
0.99,
|
| 524 |
+
)
|
| 525 |
+
progress(
|
| 526 |
+
ratio,
|
| 527 |
+
desc=(
|
| 528 |
+
f"Running model on final batch: sampled frames "
|
| 529 |
+
f"{start_sample}-{end_sample}/{frames_to_process}..."
|
| 530 |
+
),
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
num_done = self._run_batch_and_aggregate(
|
| 534 |
batch_tensors,
|
| 535 |
general_thresh=general_thresh,
|
|
|
|
| 544 |
progress(
|
| 545 |
ratio,
|
| 546 |
desc=(
|
| 547 |
+
f"Finished processing all sampled frames "
|
| 548 |
+
f"{processed_frames}/{frames_to_process}..."
|
| 549 |
),
|
| 550 |
)
|
| 551 |
|
|
|
|
| 857 |
custom_theme = gr.themes.Default(
|
| 858 |
primary_hue=gr.themes.colors.blue,
|
| 859 |
secondary_hue=gr.themes.colors.slate,
|
| 860 |
+
radius_size=gr.themes.sizes.radius_xl,
|
| 861 |
font=[gr.themes.GoogleFont("Raleway")],
|
| 862 |
)
|
| 863 |
|