File size: 10,959 Bytes
efbb653 29736b1 efbb653 1b7edb8 3927f54 1b7edb8 29736b1 1b7edb8 29736b1 efbb653 1b7edb8 efbb653 8b689fc 29736b1 3830a3f 29736b1 efbb653 29736b1 8b689fc 29736b1 8b689fc 29736b1 07ee382 3830a3f 07ee382 3830a3f 07ee382 29736b1 efbb653 07ee382 efbb653 07ee382 efbb653 1b7edb8 efbb653 1b7edb8 efbb653 1b7edb8 07ee382 29736b1 1b7edb8 29736b1 07ee382 1b7edb8 3927f54 1b7edb8 3927f54 1b7edb8 3927f54 07ee382 29736b1 07ee382 1b7edb8 3927f54 1b7edb8 3927f54 07ee382 29736b1 1b7edb8 29736b1 8b689fc 29736b1 3830a3f 29736b1 efbb653 | 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 | import torch
import numpy as np
from pathlib import Path
from transnetv2_pytorch import TransNetV2
import json
import re
# SCENE_CUT_THRESHOLD = 0.09
K = 3 # Number of cuts to detect
MIN_DURATION_FRAMES = 2
MIN_CONFIDENCE = 0.02
data_dir = Path("data/animations")
files = sorted(data_dir.glob("sample-*.webp"))
print(f"Found {len(files)} files to process.")
def get_best_device():
if torch.cuda.is_available():
return torch.device("cuda")
elif torch.backends.mps.is_available():
return torch.device("mps")
# return torch.device("cpu")
else:
return torch.device("cpu")
def load_original_frames(filepath):
"""Load original frames from an animated webp file as PIL Images."""
from PIL import Image
im = Image.open(filepath)
frames = []
try:
while True:
frames.append(im.convert("RGB"))
im.seek(im.tell() + 1)
except EOFError:
pass
return frames
def save_prediction_plot(single_frame_pred, original_frames, filename, interval=5, title=None):
"""
Save a plot of single frame predictions with thumbnails annotated at regular intervals.
"""
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np
plt.figure(figsize=(12, 4))
if title:
plt.title(title)
plt.plot(single_frame_pred)
ax = plt.gca()
# Add thumbnails at regular intervals
for idx in range(0, len(original_frames), interval):
thumb = original_frames[idx].resize((64, 64))
imagebox = OffsetImage(np.array(thumb), zoom=0.5)
ab = AnnotationBbox(imagebox, (idx, single_frame_pred[idx]), frameon=False, box_alignment=(0.5, -0.1))
ax.add_artist(ab)
plt.xlabel("Frame")
plt.ylabel("Prediction")
plt.tight_layout()
plt.savefig(filename)
plt.close()
def save_timeline_jpg(frames, scene_change_indices, filename, interval=5, roi_radius=2, title=None, single_frame_pred=None):
"""
Save a timeline JPG with thumbnails every `interval` frames and every frame near scene changes.
Scene change regions are highlighted. Each thumbnail is annotated with its frame index.
"""
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import matplotlib.patches as mpatches
import numpy as np
# Determine frames to render
frames_to_render = set(range(0, len(frames), interval))
for idx in scene_change_indices:
for offset in range(-roi_radius, roi_radius+1):
fidx = idx + offset
if 0 <= fidx < len(frames):
frames_to_render.add(fidx)
frames_to_render = sorted(frames_to_render)
# Map frames to evenly spaced positions
n = len(frames_to_render)
x_positions = list(range(n))
fig, ax = plt.subplots(figsize=(max(8, n*0.5), 3))
ax.set_xlim(-1, n)
ax.set_ylim(0, 1)
ax.axis('off')
# Highlight scene change regions
for idx in scene_change_indices:
region = [i for i, fidx in enumerate(frames_to_render) if abs(fidx-idx) <= roi_radius]
if region:
start, end = region[0], region[-1]
rect = mpatches.Rectangle((start-0.5, 0.05), end-start+1, 0.9, color='yellow', alpha=0.2)
ax.add_patch(rect)
# Prepare sets for quick lookup
last_frames = set(scene_change_indices)
first_frames = set(idx + 1 for idx in scene_change_indices if idx + 1 < len(frames))
# Draw thumbnails and annotate
for i, fidx in enumerate(frames_to_render):
thumb = frames[fidx].resize((32, 32))
imagebox = OffsetImage(np.array(thumb), zoom=0.7)
# Determine border color
if fidx in last_frames:
bboxprops = dict(edgecolor='red', linewidth=2)
elif fidx in first_frames:
bboxprops = dict(edgecolor='green', linewidth=2)
else:
bboxprops = None
ab = AnnotationBbox(
imagebox,
(x_positions[i], 0.6),
frameon=True,
box_alignment=(0.5, 0.5),
bboxprops=bboxprops
)
ax.add_artist(ab)
# Draw frame index
ax.text(x_positions[i], 0.32, str(fidx), ha='center', va='center', fontsize=9, color='black', bbox=dict(facecolor='white', edgecolor='none', alpha=0.8, boxstyle='round,pad=0.2'))
# Draw prediction value below frame index
if single_frame_pred is not None:
pred_val = single_frame_pred[fidx]
# Ensure pred_val is a scalar float for formatting
if isinstance(pred_val, np.ndarray):
pred_val = float(pred_val.squeeze())
ax.text(x_positions[i], 0.18, f"{pred_val:.2f}", ha='center', va='center', fontsize=8, color='blue', bbox=dict(facecolor='white', edgecolor='none', alpha=0.7, boxstyle='round,pad=0.2'))
if title:
ax.text(0, 0.95, title, fontsize=12, ha='left', va='top', color='navy')
plt.tight_layout()
plt.savefig(filename, dpi=150)
plt.close(fig)
def frames_to_video_tensor(frames):
"""Convert a list of PIL frames to a torch tensor of shape (num_frames, 27, 48, 3) and dtype uint8."""
import numpy as np
from PIL import Image
processed = []
for frame in frames:
arr = np.array(frame.resize((48, 27), resample=Image.Resampling.BILINEAR), dtype=np.uint8)
processed.append(torch.from_numpy(arr))
return torch.stack(processed)
def detect_scene_changes(frames):
video_tensor = frames_to_video_tensor(frames)
video_tensor = video_tensor.unsqueeze(0).to(device) # shape: 1 x num_frames x H x W x 3
with torch.no_grad():
single_frame_logits, all_frame_logits = model(video_tensor)
# Squeeze last dimension so output is flat (num_frames,)
single_frame_logits_np = single_frame_logits.cpu().numpy().squeeze() # shape: (num_frames,)
all_frame_logits_np = all_frame_logits["many_hot"].cpu().numpy().squeeze() # shape: (num_frames,)
single_frame_pred = torch.sigmoid(single_frame_logits).cpu().numpy().squeeze() # shape: (num_frames,)
all_frame_pred_np = torch.sigmoid(all_frame_logits["many_hot"]).cpu().numpy().squeeze() # shape: (num_frames,)
return {
"single_frame_pred": single_frame_pred,
"all_frame_pred": all_frame_pred_np,
"single_frame_logits": single_frame_logits_np,
"all_frame_logits": all_frame_logits_np,
}
def cached_detect_scene_changes(file, original_frames):
"""Detect scene changes with caching to avoid redundant computation."""
match = re.search(r"sample-(\d+)", file.name)
sample_num = match.group(1) if match else "unknown"
transnetv2_json = file.parent / f"sample-{sample_num}.transnetv2.json"
if transnetv2_json.exists():
with open(transnetv2_json, "r") as f:
result = json.load(f)
result["single_frame_pred"] = np.array(result["single_frame_pred"])
result["all_frame_pred"] = np.array(result["all_frame_pred"])
result["single_frame_logits"] = np.array(result["single_frame_logits"])
result["all_frame_logits"] = np.array(result["all_frame_logits"])
else:
result = detect_scene_changes(original_frames)
# Save model output to cache file
with open(transnetv2_json, "w") as f:
json.dump({
"single_frame_pred": result["single_frame_pred"].tolist(),
"all_frame_pred": result["all_frame_pred"].tolist(),
"single_frame_logits": result["single_frame_logits"].tolist(),
"all_frame_logits": result["all_frame_logits"].tolist()
}, f, indent=2)
return result
if __name__ == "__main__":
device = get_best_device()
print(f"Using device: {device}")
model = TransNetV2()
state_dict = torch.load("transnetv2-pytorch-weights.pth")
model.load_state_dict(state_dict)
model.eval().to(device)
for file in files:
match = re.search(r"sample-(\d+)", file.name)
sample_num = match.group(1) if match else "unknown"
original_frames = load_original_frames(file)
result = cached_detect_scene_changes(file, original_frames)
# scene_change_indices = [i for i, p in enumerate(result["single_frame_pred"]) if p >= SCENE_CUT_THRESHOLD]
# Detect top-K-1 scene changes
single_frame_pred = result["single_frame_pred"]
# Ignore first and last frame when selecting scene changes, and enforce MIN_DURATION_FRAMES between cuts
valid_indices = np.arange(1, len(single_frame_pred) - 1)
valid_preds = single_frame_pred[1:-1]
# Sort indices by prediction score (descending)
sorted_indices = valid_indices[np.argsort(valid_preds)[::-1]]
scene_change_indices = []
scene_cut_confidences = []
for idx in sorted_indices:
if all(abs(idx - prev) >= MIN_DURATION_FRAMES for prev in scene_change_indices):
scene_change_indices.append(int(idx))
scene_cut_confidences.append(float(single_frame_pred[idx]))
if len(scene_change_indices) >= (K - 1):
break
# Check if any confidence is below MIN_CONFIDENCE
failed = any(conf < MIN_CONFIDENCE for conf in scene_cut_confidences)
print(f"File: {file.name}, Frames: {len(original_frames)}, Scene Changes: {len(scene_change_indices)}, Success: {not failed}")
# Save results to JSON (include threshold and predictions)
json_filename = file.parent / f"sample-{sample_num}.json"
with open(json_filename, "w") as f:
json.dump({
"num_frames": len(original_frames),
"scene_change_indices": scene_change_indices,
"scene_cut_confidences": scene_cut_confidences,
# "threshold": SCENE_CUT_THRESHOLD
"params": {
"k": K,
"min_duration_frames": MIN_DURATION_FRAMES,
"min_confidence": MIN_CONFIDENCE,
},
"success": not failed
}, f, indent=2)
# Save timeline JPG
timeline_filename = file.parent / f"sample-{sample_num}.timeline.jpg"
plot_filename = file.parent / f"sample-{sample_num}.plot.jpg"
save_timeline_jpg(
frames=original_frames,
scene_change_indices=scene_change_indices,
filename=timeline_filename,
interval=10,
roi_radius=2,
title=f"Timeline: {file.name}",
single_frame_pred=result["single_frame_pred"]
)
# Save prediction plot with thumbnails
save_prediction_plot(
single_frame_pred=result["single_frame_pred"],
original_frames=original_frames,
filename=plot_filename,
interval=5,
title=f"Single Frame Predictions: {file.name}"
)
|