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
32
main.py
32
main.py
|
|
@ -430,13 +430,16 @@ def analyze_scenes_strategy(video_path, scenes):
|
||||||
if not cap.isOpened():
|
if not cap.isOpened():
|
||||||
return ['TRACK'] * len(scenes)
|
return ['TRACK'] * len(scenes)
|
||||||
|
|
||||||
|
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
||||||
|
|
||||||
for start, end in tqdm(scenes, desc=" Analyzing Scenes"):
|
for start, end in tqdm(scenes, desc=" Analyzing Scenes"):
|
||||||
# Sample 3 frames (start, middle, end)
|
s_f, e_f = start.get_frames(), end.get_frames()
|
||||||
frames_to_check = [
|
# Sample 5 frames spread across the scene, clamped inside it (the old
|
||||||
start.get_frames() + 5,
|
# start+5/end-5 samples landed outside scenes shorter than ~10 frames).
|
||||||
int((start.get_frames() + end.get_frames()) / 2),
|
margin = min(2, max(0, (e_f - s_f - 1) // 2))
|
||||||
end.get_frames() - 5
|
frames_to_check = sorted(set(
|
||||||
]
|
int(round(f)) for f in np.linspace(s_f + margin, e_f - 1 - margin, 5)
|
||||||
|
))
|
||||||
|
|
||||||
face_counts = []
|
face_counts = []
|
||||||
for f_idx in frames_to_check:
|
for f_idx in frames_to_check:
|
||||||
|
|
@ -444,6 +447,11 @@ def analyze_scenes_strategy(video_path, scenes):
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
if not ret: continue
|
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
|
# Detect faces
|
||||||
candidates = detect_face_candidates(frame)
|
candidates = detect_face_candidates(frame)
|
||||||
face_counts.append(len(candidates))
|
face_counts.append(len(candidates))
|
||||||
|
|
@ -465,6 +473,18 @@ def analyze_scenes_strategy(video_path, scenes):
|
||||||
strategies.append('TRACK')
|
strategies.append('TRACK')
|
||||||
|
|
||||||
cap.release()
|
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
|
return strategies
|
||||||
|
|
||||||
def detect_scenes(video_path):
|
def detect_scenes(video_path):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue