clipwright/tests/test_speaker_tracker.py
juancarlos.cavero bdd9e5d53d fix(reframe): ignore lone target jumps so the camera stops swinging
The camera swung between subjects because a single detection could teleport the
target anywhere and the frame followed immediately. On real user footage pulled
from prod, 22% of target updates moved further than the entire safe zone.

A move that large is far more often a detector error — a second face, a false
positive, a box snapping to a different body part — than a person who actually
moved that far. It now has to repeat JUMP_CONFIRM_FRAMES (3) consecutive
detections before the camera commits; a wrong reading disappears on the next
detection and never moves the frame. Contradictory outliers cannot confirm each
other, so a detector flapping between two wrong places holds the frame still.

Measured over 262s of TRACK footage across 12 chunks of two real user videos,
counting only reversals INSIDE a scene (a cut is supposed to re-frame, so
scoring scene-change snaps would measure the feature, not the bug):

  confirm=1   0.41 rev/s   91 px/s      —
  confirm=2   0.20 rev/s   76 px/s   -51%
  confirm=3   0.13 rev/s   60 px/s   -69%
  confirm=4   0.10 rev/s   48 px/s   -76%

3 is the knee: 4 buys another 7 points of reversals for a third more latency.

Honest about the downside — per scene, travel gets calmer in 54 of 84 and is
unchanged in 23, but 7 get BUSIER (worst 59 -> 108px/s, and one dead-still scene
starts drifting at 31px/s), because committing later can leave the camera
further to travel. Net strongly positive, not universally so.

Tried and rejected: a 3s switch cooldown, which bought only -19% on its own and
is precisely what would make a two-person interview stop following the
conversation. JUMP_CONFIRM_FRAMES=1 restores the old behaviour without a deploy.

Methodology note: variants are compared by recording the detections once and
replaying them through the cameraman, so every variant sees identical input
rather than a fresh run of a non-deterministic detector.
2026-07-26 20:59:04 +02:00

127 lines
4.9 KiB
Python

"""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
class TestJumpConfirmation:
"""A lone huge target jump is a detector error, not a person moving."""
def _cam(self, crop=600, video_w=1920):
cam = main.SmoothedCameraman(crop, 1080, video_w, 1080,
aspect_ratio=9 / 16)
cam.target_center_x = 500.0
return cam
def test_small_moves_apply_immediately(self):
cam = self._cam()
cam.update_target([540, 0, 20, 20]) # centre 550, well inside
assert cam.target_center_x == 550
def test_a_single_big_jump_is_ignored(self):
cam = self._cam()
before = cam.target_center_x
cam.update_target([1400, 0, 40, 40]) # centre 1420, far away
assert cam.target_center_x == before, "one outlier must not move the frame"
def test_a_sustained_big_jump_is_followed(self):
cam = self._cam()
for _ in range(cam.jump_confirm_frames):
cam.update_target([1400, 0, 40, 40])
assert cam.target_center_x == 1420
def test_contradictory_outliers_do_not_confirm_each_other(self):
# Detector flapping between two wrong places must not add up to a move.
cam = self._cam()
before = cam.target_center_x
for _ in range(cam.jump_confirm_frames * 2):
cam.update_target([1400, 0, 40, 40]) # far right
cam.update_target([20, 0, 40, 40]) # far left
assert cam.target_center_x == before
def test_a_confirmed_move_resets_the_counter(self):
cam = self._cam()
for _ in range(cam.jump_confirm_frames):
cam.update_target([1400, 0, 40, 40])
assert cam.target_center_x == 1420
cam.update_target([1410, 0, 20, 20]) # small follow-up
assert cam.target_center_x == 1420
def test_no_detection_leaves_the_target_alone(self):
cam = self._cam()
cam.update_target(None)
assert cam.target_center_x == 500