fix(reframe): stop the switch cooldown from failing open

The cooldown that keeps the camera from swinging between subjects only applied
when the active speaker happened to be detected in the current frame. If they
were missing — a blink, a head turn, one motion-blurred frame — the guard fell
through and switched immediately, so it only ever held when it wasn't needed.
Measured on a 12s clip: 3 of 7 target switches jumped the cooldown this way.

Now the cooldown holds regardless: it returns the active speaker's box when
visible and None otherwise, and the caller only calls update_target() on a
truthy box, so the camera keeps its target and finishes whatever move it was
making. The hold stays bounded by the cooldown, so a subject who really did
leave the shot is still released once the window expires.

Measured on the same clip: cooldown bypasses 3 -> 0, target switches 7 -> 5.

This does NOT by itself calm the camera down — reversals stayed at 13 over 12s.
The dominant driver is target jitter (p90 of 496px per update, 15 of 67 updates
alone exceeding the 101px safe zone) plus faces losing their identity on
re-detection, both of which need tuning against representative footage rather
than a screen recording.
This commit is contained in:
juancarlos.cavero 2026-07-26 11:44:27 +02:00
parent fd7a2b2cd9
commit f0ecb0d078
2 changed files with 95 additions and 4 deletions

19
main.py
View file

@ -271,11 +271,24 @@ class SpeakerTracker:
self.locked_counter += 1
return best_candidate['box']
# New person
# New person. The cooldown must hold whether or not the current
# speaker happens to be detected in THIS frame.
#
# It used to fall through and switch when the active speaker was
# missing from the candidate list — a blink, a head turn or one
# motion-blurred frame was enough. That is precisely when the
# cooldown is needed, so it only ever fired when it wasn't: 3 of 7
# target switches measured on a 12s clip (25-jul-2026) jumped the
# cooldown this way, and every jump drags the camera across frame.
#
# Returning None holds instead: the caller only calls
# update_target() on a truthy box, so the camera keeps its current
# target and finishes whatever move it was making. The hold is
# bounded by the cooldown itself — once it expires, a speaker who
# really did leave the shot is switched away from normally.
if frame_number - self.last_switch_frame < self.switch_cooldown:
old_cand = next((c for c in current_candidates if c['id'] == self.active_speaker_id), None)
if old_cand:
return old_cand['box']
return old_cand['box'] if old_cand else None
self.active_speaker_id = target_id
self.last_switch_frame = frame_number

View file

@ -0,0 +1,78 @@
"""Speaker switching hysteresis.
Regression cover for the camera swinging between subjects: the switch cooldown
used to fall through and switch anyway whenever the active speaker happened to
be missing from the current frame's candidates — a blink or one motion-blurred
frame was enough, which is exactly when the cooldown is needed. Measured on a
12s clip (25-jul-2026), 3 of 7 target switches jumped the cooldown this way.
"""
import pytest
main = pytest.importorskip("main") # needs cv2/mediapipe, absent in minimal CI
WIDTH = 1280
COOLDOWN = 30
def _face(center_x, size=120, score=None):
"""A candidate box centred on ``center_x``."""
return {"box": [center_x - size / 2, 100, size, size],
"score": score if score is not None else size * size}
def _tracker():
return main.SpeakerTracker(cooldown_frames=COOLDOWN)
def _lock_onto(tracker, x, frame=0):
"""Drive the tracker until it is locked on the subject at ``x``."""
for f in range(frame, frame + 5):
tracker.get_target([_face(x)], f, WIDTH)
return tracker.active_speaker_id
class TestSwitchCooldown:
def test_locks_onto_a_lone_speaker(self):
t = _tracker()
assert _lock_onto(t, 300) is not None
def test_holds_when_the_active_speaker_blinks_out(self):
# THE BUG: speaker A vanishes for one frame while B is on screen.
# Inside the cooldown the tracker must hold, not hand the camera to B.
t = _tracker()
a = _lock_onto(t, 300)
box = t.get_target([_face(1000)], 6, WIDTH) # only B visible
assert box is None, "must hold instead of switching mid-cooldown"
assert t.active_speaker_id == a, "the active speaker must not change"
def test_holds_on_the_active_speaker_when_both_are_visible(self):
t = _tracker()
a = _lock_onto(t, 300)
box = t.get_target([_face(300), _face(1000)], 6, WIDTH)
assert box is not None
assert t.active_speaker_id == a
def test_switches_once_the_cooldown_expires(self):
# The hold is bounded: someone who really left the shot is released.
t = _tracker()
a = _lock_onto(t, 300)
t.get_target([_face(1000)], 6, WIDTH) # held
assert t.active_speaker_id == a
for f in range(COOLDOWN + 10, COOLDOWN + 20): # past the window
t.get_target([_face(1000)], f, WIDTH)
assert t.active_speaker_id != a, "cooldown must not hold forever"
def test_no_candidates_holds_rather_than_recentres(self):
t = _tracker()
_lock_onto(t, 300)
assert t.get_target([], 6, WIDTH) is None
def test_repeated_dropouts_do_not_accumulate_switches(self):
# A flickering detection used to switch on every dropout. The lock is
# taken at frame 0, so the cooldown window runs up to frame COOLDOWN.
t = _tracker()
a = _lock_onto(t, 300)
for f in range(6, COOLDOWN):
t.get_target([_face(1000)] if f % 2 else [_face(300), _face(1000)],
f, WIDTH)
assert t.active_speaker_id == a