fix(reframe): robustify per-scene TRACK/GENERAL strategy
- 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
This commit is contained in:
parent
101a467c34
commit
36cf6d33b1
1 changed files with 34 additions and 14 deletions
48
main.py
48
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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue