Candle commited on
Commit ·
aab93cc
1
Parent(s): 57d6cb5
loop detection
Browse files- detect_loops.py +19 -12
detect_loops.py
CHANGED
|
@@ -17,9 +17,16 @@ def extract_frames(webp_path):
|
|
| 17 |
return frames
|
| 18 |
|
| 19 |
def compute_sim(f1, f2):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def detect_loops(frames, min_len=6, max_len=40, top_k=3):
|
| 25 |
import time
|
|
@@ -48,11 +55,11 @@ def detect_loops(frames, min_len=6, max_len=40, top_k=3):
|
|
| 48 |
# Compare composite frames directly
|
| 49 |
start_comp = composite_frames[i].astype(np.float32)
|
| 50 |
end_comp = composite_frames[j].astype(np.float32)
|
| 51 |
-
|
| 52 |
t1 = time.time()
|
| 53 |
-
score =
|
| 54 |
-
print(f"Loop ({i},{j}):
|
| 55 |
-
candidates.append((score, i, j,
|
| 56 |
# Sort by score descending
|
| 57 |
candidates.sort(reverse=True)
|
| 58 |
return candidates[:top_k]
|
|
@@ -99,26 +106,26 @@ if __name__ == "__main__":
|
|
| 99 |
# Save all candidates and their scores to JSON
|
| 100 |
import json
|
| 101 |
loop_json = []
|
| 102 |
-
for score, i, j,
|
| 103 |
loop_json.append({
|
| 104 |
"start": int(i),
|
| 105 |
"end": int(j),
|
| 106 |
"score": float(score),
|
| 107 |
-
"
|
| 108 |
})
|
| 109 |
json_name = f"{webp_path.stem}.loop.json"
|
| 110 |
json_path = output_dir / json_name
|
| 111 |
with open(json_path, "w") as f:
|
| 112 |
json.dump(loop_json, f, indent=2)
|
| 113 |
print(f"Saved loop candidates: {json_path}")
|
| 114 |
-
for idx, (score, i, j,
|
| 115 |
-
print(f"Loop candidate: start={i}, end={j}, score={score:.
|
| 116 |
# Extract loop frames (seamless looping: frames[i:j])
|
| 117 |
loop_frames = frames[i:j]
|
| 118 |
# Convert BGR (OpenCV) to RGB for PIL
|
| 119 |
pil_frames = [Image.fromarray(cv2.cvtColor(f, cv2.COLOR_BGR2RGB)) for f in loop_frames]
|
| 120 |
# Save as animated webp
|
| 121 |
-
out_name = f"{webp_path.stem}.loop-{
|
| 122 |
out_path = output_dir / out_name
|
| 123 |
pil_frames[0].save(
|
| 124 |
out_path,
|
|
|
|
| 17 |
return frames
|
| 18 |
|
| 19 |
def compute_sim(f1, f2):
|
| 20 |
+
assert f1.shape == f2.shape, f"Shape mismatch: {f1.shape} vs {f2.shape}"
|
| 21 |
+
assert f1.shape[2] == 3, f"Expected 3 channels, got {f1.shape[2]}"
|
| 22 |
+
assert f1.dtype == np.float32, f"Expected float32, got {f1.dtype}"
|
| 23 |
+
assert f2.dtype == np.float32, f"Expected float32, got {f2.dtype}"
|
| 24 |
+
# Flatten to 1D vectors
|
| 25 |
+
v1 = f1.flatten()
|
| 26 |
+
v2 = f2.flatten()
|
| 27 |
+
eps = 1e-8
|
| 28 |
+
cos_sim = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2) + eps)
|
| 29 |
+
return cos_sim
|
| 30 |
|
| 31 |
def detect_loops(frames, min_len=6, max_len=40, top_k=3):
|
| 32 |
import time
|
|
|
|
| 55 |
# Compare composite frames directly
|
| 56 |
start_comp = composite_frames[i].astype(np.float32)
|
| 57 |
end_comp = composite_frames[j].astype(np.float32)
|
| 58 |
+
cos_sim = compute_sim(start_comp, end_comp)
|
| 59 |
t1 = time.time()
|
| 60 |
+
score = cos_sim # Higher cosine similarity is better
|
| 61 |
+
print(f"Loop ({i},{j}): Cosine similarity={cos_sim:.4f} (t={t1-t0:.3f}s)")
|
| 62 |
+
candidates.append((score, i, j, cos_sim))
|
| 63 |
# Sort by score descending
|
| 64 |
candidates.sort(reverse=True)
|
| 65 |
return candidates[:top_k]
|
|
|
|
| 106 |
# Save all candidates and their scores to JSON
|
| 107 |
import json
|
| 108 |
loop_json = []
|
| 109 |
+
for score, i, j, cos_sim in loops:
|
| 110 |
loop_json.append({
|
| 111 |
"start": int(i),
|
| 112 |
"end": int(j),
|
| 113 |
"score": float(score),
|
| 114 |
+
"cos_sim": float(cos_sim)
|
| 115 |
})
|
| 116 |
json_name = f"{webp_path.stem}.loop.json"
|
| 117 |
json_path = output_dir / json_name
|
| 118 |
with open(json_path, "w") as f:
|
| 119 |
json.dump(loop_json, f, indent=2)
|
| 120 |
print(f"Saved loop candidates: {json_path}")
|
| 121 |
+
for idx, (score, i, j, cos_sim) in enumerate(loops):
|
| 122 |
+
print(f"Loop candidate: start={i}, end={j}, score={score:.4f}, COS_SIM={cos_sim:.4f}")
|
| 123 |
# Extract loop frames (seamless looping: frames[i:j])
|
| 124 |
loop_frames = frames[i:j]
|
| 125 |
# Convert BGR (OpenCV) to RGB for PIL
|
| 126 |
pil_frames = [Image.fromarray(cv2.cvtColor(f, cv2.COLOR_BGR2RGB)) for f in loop_frames]
|
| 127 |
# Save as animated webp
|
| 128 |
+
out_name = f"{webp_path.stem}.loop-{idx}.webp"
|
| 129 |
out_path = output_dir / out_name
|
| 130 |
pil_frames[0].save(
|
| 131 |
out_path,
|