diff --git a/main.py b/main.py index 3d0ddb9..d4927f4 100644 --- a/main.py +++ b/main.py @@ -82,6 +82,12 @@ model = YOLO('yolov8n.pt') mp_face_detection = mp.solutions.face_detection face_detection = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) +# Consecutive detections a large target move must survive before the camera +# follows it (see SmoothedCameraman.update_target). Env-overridable so the +# damping can be dialled back without a deploy; 1 restores the old behaviour. +JUMP_CONFIRM_FRAMES = max(int(os.environ.get("JUMP_CONFIRM_FRAMES", "3")), 1) + + class SmoothedCameraman: """ Handles smooth camera movement. @@ -111,13 +117,52 @@ class SmoothedCameraman: # As long as the target is within this zone relative to current center, DO NOT MOVE. self.safe_zone_radius = self.crop_width * 0.25 + # A target that teleports further than the safe zone in one detection 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. Committing to it immediately is what made the camera + # swing: measured on real user footage, 22% of target updates jumped + # more than the entire safe zone. So a big move has to REPEAT this many + # times before the camera follows it; a wrong reading disappears on the + # next detection and never moves the frame. + # + # The cost is latency on a genuinely fast move: at DETECT_STRIDE=4 and + # 30fps, three confirmations is ~0.4s. That reads as an operator being + # unhurried, which is the look we want, and it is far cheaper than the + # whip-panning it replaces. + # + # Measured over 262s of TRACK footage from two real user videos + # (26-jul-2026), confirm=1 -> 3: in-scene reversals 0.41/s -> 0.13/s + # (-69%), camera travel 91px/s -> 60px/s (-34%). Per scene, 54 of 84 get + # calmer and 23 are unchanged — but 7 get BUSIER, up to 59 -> 108px/s, + # because committing later can leave the camera further to travel. Net + # strongly positive, not universally so. + self.jump_confirm_frames = JUMP_CONFIRM_FRAMES + self._pending_target = None + self._pending_count = 0 + def update_target(self, face_box): - """ - Updates the target center based on detected face/person. - """ - if face_box: - x, y, w, h = face_box - self.target_center_x = x + w / 2 + """Update the target centre from a detection, ignoring lone big jumps.""" + if not face_box: + return + x, y, w, h = face_box + new_center = x + w / 2 + + if abs(new_center - self.target_center_x) > self.safe_zone_radius: + # Same big move as last time? Count it. Otherwise start counting + # afresh — two contradictory outliers must not confirm each other. + if (self._pending_target is not None + and abs(new_center - self._pending_target) <= self.safe_zone_radius): + self._pending_count += 1 + else: + self._pending_target = new_center + self._pending_count = 1 + if self._pending_count < self.jump_confirm_frames: + return # not convinced yet — hold the frame + + self._pending_target = None + self._pending_count = 0 + self.target_center_x = new_center def get_crop_box(self, force_snap=False): """ diff --git a/tests/test_speaker_tracker.py b/tests/test_speaker_tracker.py index cfcc3d1..1649bf1 100644 --- a/tests/test_speaker_tracker.py +++ b/tests/test_speaker_tracker.py @@ -76,3 +76,52 @@ class TestSwitchCooldown: 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