frigate/frigate/test/test_obects.py
Daniel-dev22 1c9396449e perf(track): add __slots__ to TrackedObjectAttribute
TrackedObjectAttribute is constructed per attribute (face, license plate,
logo, ...) per frame in process_frames. It holds exactly six fixed fields
and is never mutated beyond __init__, so __slots__ removes the per-instance
__dict__.

Measured in the release image: 352 B/instance (56 B object + 296 B __dict__)
-> ~104 B with __slots__, ~70% smaller, on a high-churn per-frame object.

Adds a test asserting no __dict__ and that undeclared attributes raise.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 09:57:01 -04:00

66 lines
2.4 KiB
Python

import unittest
from frigate.track.tracked_object import TrackedObjectAttribute
class TestAttributeSlots(unittest.TestCase):
def test_attribute_uses_slots(self) -> None:
attribute = TrackedObjectAttribute(
("amazon", 0.8, (847, 242, 883, 255), 468, 2.77, (702, 134, 1050, 482))
)
# __slots__ means no per-instance __dict__
self.assertFalse(hasattr(attribute, "__dict__"))
# all declared fields are still populated and readable
self.assertEqual(attribute.label, "amazon")
self.assertEqual(attribute.region, (702, 134, 1050, 482))
# setting an undeclared attribute must raise (catches accidental drift)
with self.assertRaises(AttributeError):
attribute.unexpected = 1
class TestAttribute(unittest.TestCase):
def test_overlapping_object_selection(self) -> None:
attribute = TrackedObjectAttribute(
(
"amazon",
0.80078125,
(847, 242, 883, 255),
468,
2.769230769230769,
(702, 134, 1050, 482),
)
)
objects = [
{
"label": "car",
"score": 0.98828125,
"box": (728, 223, 1266, 719),
"area": 266848,
"ratio": 1.0846774193548387,
"region": (349, 0, 1397, 1048),
"frame_time": 1727785394.498972,
"centroid": (997, 471),
"id": "1727785349.150633-408hal",
"start_time": 1727785349.150633,
"motionless_count": 362,
"position_changes": 0,
"score_history": [0.98828125, 0.95703125, 0.98828125, 0.98828125],
},
{
"label": "person",
"score": 0.76953125,
"box": (826, 172, 939, 417),
"area": 27685,
"ratio": 0.46122448979591835,
"region": (702, 134, 1050, 482),
"frame_time": 1727785394.498972,
"centroid": (882, 294),
"id": "1727785390.499768-9fbhem",
"start_time": 1727785390.499768,
"motionless_count": 2,
"position_changes": 1,
"score_history": [0.8828125, 0.83984375, 0.91796875, 0.94140625],
},
]
assert attribute.find_best_object(objects) == "1727785390.499768-9fbhem"