free tracker state when an object is deregistered

deregister() removed the object from tracked_objects, disappeared and track_id_map but left its entries in positions, stationary_box_history and the stationary classifier's four per-object dicts, including a 96x96 anchor crop for every object that went stationary. Every object tracked since the camera process started stayed in memory. deregister() now pops those entries and calls a new StationaryMotionClassifier.forget() to clear the classifier's state.
This commit is contained in:
Josh Hawkins
2026-11-29 11:41:16 -06:00
parent 140680215b
commit 467b906c2f
3 changed files with 79 additions and 0 deletions
@@ -0,0 +1,69 @@
"""Per-object tracker state is released when an object is deregistered."""
import unittest
from unittest.mock import MagicMock
import numpy as np
from frigate.camera import PTZMetrics
from frigate.config import FrigateConfig
from frigate.track.norfair_tracker import NorfairTracker
CAMERA = "front"
BOX = (400, 200, 500, 500)
class TestNorfairTrackerDeregister(unittest.TestCase):
def setUp(self) -> None:
config = FrigateConfig(
**{
"mqtt": {"enabled": False},
"cameras": {
CAMERA: {
"ffmpeg": {
"inputs": [
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect"],
}
]
},
"detect": {"width": 1280, "height": 720},
}
},
}
)
camera_config = config.cameras[CAMERA]
self.tracker = NorfairTracker(camera_config, PTZMetrics())
self.tracker.frame_manager = MagicMock()
self.tracker.frame_manager.get.return_value = np.zeros(
camera_config.frame_shape_yuv, dtype=np.uint8
)
self.frame_time = 1000.0
def run_frames(self, count: int, detections: list) -> None:
for _ in range(count):
self.frame_time += 0.2
self.tracker.match_and_update("frame", self.frame_time, detections)
def test_departed_object_leaves_no_state(self):
self.run_frames(100, [("car", 0.9, BOX, 30000, 0.33, (0, 0, 640, 640))])
(obj_id,) = self.tracker.tracked_objects
classifier = self.tracker.stationary_classifier
self.assertIn(obj_id, self.tracker.positions)
self.assertIn(obj_id, classifier.anchor_crops)
self.run_frames(100, [])
self.assertEqual(self.tracker.tracked_objects, {})
self.assertEqual(self.tracker.positions, {})
self.assertEqual(self.tracker.stationary_box_history, {})
self.assertEqual(classifier.anchor_crops, {})
self.assertEqual(classifier.anchor_boxes, {})
self.assertEqual(classifier.changed_counts, {})
self.assertEqual(classifier.shift_histories, {})
if __name__ == "__main__":
unittest.main()
+3
View File
@@ -348,6 +348,9 @@ class NorfairTracker(ObjectTracker):
del self.tracked_objects[id]
del self.disappeared[id]
self.positions.pop(id, None)
self.stationary_box_history.pop(id, None)
self.stationary_classifier.forget(id)
# only manually deregister objects from norfair's list if max_frames is defined
if (
+7
View File
@@ -116,6 +116,13 @@ class StationaryMotionClassifier:
self.changed_counts[id] = 0
self.shift_histories[id] = []
def forget(self, id: str) -> None:
"""Drop all state for an object that is no longer tracked."""
self.anchor_crops.pop(id, None)
self.anchor_boxes.pop(id, None)
self.changed_counts.pop(id, None)
self.shift_histories.pop(id, None)
def _extract_y_crop(
self, yuv_frame: np.ndarray, box: tuple[int, int, int, int]
) -> np.ndarray: