From 36cf6d33b110b362d003667e8626ab9ac3e5b9b1 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Sat, 18 Jul 2026 03:51:50 +0200 Subject: [PATCH] fix(reframe): robustify per-scene TRACK/GENERAL strategy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Sample 5 evenly-spread frames clamped inside the scene (old start+5/end-5 fell outside scenes shorter than ~10 frames) - Skip near-black frames so fades stop dragging single-person scenes to GENERAL - Hysteresis: a sub-2s scene flanked by agreeing neighbors adopts their strategy — each TRACK<->GENERAL flip is a full layout change on screen --- main.py | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 873b7e6..91fb468 100644 --- a/main.py +++ b/main.py @@ -426,45 +426,65 @@ def analyze_scenes_strategy(video_path, scenes): """ cap = cv2.VideoCapture(video_path) strategies = [] - + if not cap.isOpened(): return ['TRACK'] * len(scenes) - + + fps = cap.get(cv2.CAP_PROP_FPS) or 30.0 + for start, end in tqdm(scenes, desc=" Analyzing Scenes"): - # Sample 3 frames (start, middle, end) - frames_to_check = [ - start.get_frames() + 5, - int((start.get_frames() + end.get_frames()) / 2), - end.get_frames() - 5 - ] - + s_f, e_f = start.get_frames(), end.get_frames() + # Sample 5 frames spread across the scene, clamped inside it (the old + # start+5/end-5 samples landed outside scenes shorter than ~10 frames). + margin = min(2, max(0, (e_f - s_f - 1) // 2)) + frames_to_check = sorted(set( + int(round(f)) for f in np.linspace(s_f + margin, e_f - 1 - margin, 5) + )) + face_counts = [] for f_idx in frames_to_check: cap.set(cv2.CAP_PROP_POS_FRAMES, f_idx) ret, frame = cap.read() if not ret: continue - + + # Near-black frames (fades, cut-to-black) carry no faces and used + # to drag single-person scenes into GENERAL. Skip them. + if frame.mean() < 16: + continue + # Detect faces candidates = detect_face_candidates(frame) face_counts.append(len(candidates)) - + # Decision Logic if not face_counts: avg_faces = 0 else: avg_faces = sum(face_counts) / len(face_counts) - + # Strategy: # 0 faces -> GENERAL (Landscape/B-roll) # 1 face -> TRACK # > 1.2 faces -> GENERAL (Group) - + if avg_faces > 1.2 or avg_faces < 0.5: strategies.append('GENERAL') else: strategies.append('TRACK') - + cap.release() + + # Hysteresis: a short scene whose two neighbors agree on the opposite + # strategy is almost always a sampling miss (profile face, insert shot). + # Each TRACK<->GENERAL flip is a full on-screen layout change, so flapping + # is worse than an occasional wrong-but-stable choice. + max_flip_frames = int(2.0 * fps) + for i in range(1, len(strategies) - 1): + dur = scenes[i][1].get_frames() - scenes[i][0].get_frames() + if (dur < max_flip_frames + and strategies[i - 1] == strategies[i + 1] != strategies[i]): + strategies[i] = strategies[i - 1] + return strategies def detect_scenes(video_path):