From 55872d73d6f06dd59f53e66e9e2ae84a9165d6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 30 Nov 2026 14:29:14 +0100 Subject: [PATCH] Read back zoom level after returning to preset (#25118) * Read back zoom level after returning to preset The camera is idle when the track ends, so motor_stopped is still set after GotoPreset and the status loop meant to update the zoom level never runs. The next track then starts from the previous track's zoom level, and in absolute zooming mode its first move zooms back to it. Clear motor_stopped first, as calibration does after its preset moves. * Don't block maintenance on the preset status wait If the camera stops responding when the track ends, get_camera_status never sets motor_stopped and the wait after the preset move loops forever. run() waits on camera_maintenance, so every other camera stalls with it. Drop the wait and let the status check at the top of maintenance read the zoom level back, once per pass. * Don't send the stored zoom level on the initial absolute move A track that starts before maintenance has read the zoom back from the return preset still sees the previous track's level, and the initial move sent it as an AbsoluteMove after pan/tilt. Zoom 0 skips the zoom on the initial move, which is what it was meant to do, and the next move works from the level read after pan/tilt. * Drop the motor_stopped clear after returning to preset The zoom = 0 change on the initial move fixes the stale zoom on its own. Clearing motor_stopped there made a track that starts mid-return have its first move refused and recorded with a start time of 0. The removed wait never polled, as motor_stopped is still set from the wait before the preset move. --- frigate/ptz/autotrack.py | 8 +++----- frigate/test/test_ptz_autotrack.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/frigate/ptz/autotrack.py b/frigate/ptz/autotrack.py index 94f3860bb7..88afb207dd 100644 --- a/frigate/ptz/autotrack.py +++ b/frigate/ptz/autotrack.py @@ -1191,9 +1191,10 @@ class PtzAutoTracker(threading.Thread): # absolute zooming separately from pan/tilt if camera_config.onvif.autotracking.zooming == ZoomingModeEnum.absolute: - # don't zoom on initial move + # don't zoom on initial move. the stored zoom level can still be + # the previous track's if this one starts right after a preset return if "target_box" not in tom: - zoom = current_zoom_level + zoom = 0 else: if ( result := self._should_zoom_in( @@ -1421,9 +1422,6 @@ class PtzAutoTracker(threading.Thread): autotracker_config.return_preset.lower(), ) - # update stored zoom level from preset - await self._wait_until_stopped(camera) - self.dispatcher.publish( f"{camera}/ptz_autotracker/active", "OFF", retain=False ) diff --git a/frigate/test/test_ptz_autotrack.py b/frigate/test/test_ptz_autotrack.py index a20e7c2f64..bc16a7bb20 100644 --- a/frigate/test/test_ptz_autotrack.py +++ b/frigate/test/test_ptz_autotrack.py @@ -267,6 +267,22 @@ class TestAutotrackerMoveQueue(unittest.IsolatedAsyncioTestCase): ) +class TestAutotrackerInitialZoom(unittest.TestCase): + def test_initial_absolute_move_does_not_zoom(self) -> None: + tracker = _make_tracker() + tracker.config.cameras[ + CAMERA + ].onvif.autotracking.zooming = ZoomingModeEnum.absolute + # stale level from the previous track, not yet read back from the preset + tracker.ptz_metrics[CAMERA].zoom_level.value = 0.55 + tracker.tracked_object_metrics = {CAMERA: {}} + box = (900, 400, 1000, 600) + obj = MagicMock() + obj.obj_data = {"box": box} + + self.assertEqual(tracker._get_zoom_amount(CAMERA, obj, box, 0), 0) + + class TestMaxTargetBox(unittest.TestCase): def test_follows_zoom_factor(self) -> None: self.assertAlmostEqual(calculate_max_target_box(0.5), 0.6**2)