From ec3fb00494d3fa17231068002b56def82cf95530 Mon Sep 17 00:00:00 2001 From: Daniel <47092714+Daniel-dev22@users.noreply.github.com> Date: Tue, 23 Jun 2026 17:47:04 -0400 Subject: [PATCH] perf(track): use sum()/len() instead of np.mean in average_boxes (#23521) * perf(track): avoid numpy reductions on tiny box lists in position smoothing update_position runs per tracked object per frame. While a position has fewer than 10 samples it calls np.percentile four times, and average_boxes (per stationary object per frame) calls np.mean four times - all on lists of at most 10 ints, where numpy's per-call dispatch/validation overhead dominates the actual work. Replace them with pure-Python equivalents: - average_boxes: sum()/len() instead of np.mean (bit-identical output) - interpolated_percentile(): linear-interpolated percentile matching numpy.percentile (including its lerp branch at frac>=0.5) for the small lists used here, in place of np.percentile Measured in the release image (numpy 1.26.4) on a 10-element list: np.percentile 18735 ns -> 191 ns/call (98x); np.mean-based average_boxes 7480 ns -> 591 ns (12.7x); ~74 us saved per object-frame in update_position. A live py-spy --gil profile of a camera process_frames worker showed np.percentile (update_position) and np.mean (average_boxes) among the top Frigate-owned on-CPU frames. Output is unchanged: added tests assert both helpers are bit-identical to numpy over randomized small inputs. Co-Authored-By: Claude Opus 4.8 (1M context) * Drop interpolated_percentile, keep only average_boxes Per review: reimplementing np.percentile hurts readability and risks divergence from numpy (e.g. numpy 2.x). Revert update_position to np.percentile and remove the helper; keep only the average_boxes change (sum()/len() instead of np.mean), which stays bit-identical. Co-Authored-By: Claude Opus 4.8 (1M context) --------- Co-authored-by: Claude Opus 4.8 (1M context) --- frigate/test/test_obects.py | 16 ++++++++++++++++ frigate/util/object.py | 19 +++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/frigate/test/test_obects.py b/frigate/test/test_obects.py index 8fe831980e..ee0162ec01 100644 --- a/frigate/test/test_obects.py +++ b/frigate/test/test_obects.py @@ -1,6 +1,22 @@ +import random import unittest +import numpy as np + from frigate.track.tracked_object import TrackedObjectAttribute +from frigate.util.object import average_boxes + + +class TestBoxStatistics(unittest.TestCase): + def test_average_boxes_matches_numpy(self) -> None: + rng = random.Random(0) + for _ in range(5000): + boxes = [ + [rng.randint(0, 4000) for _ in range(4)] + for _ in range(rng.randint(1, 10)) + ] + expected = [float(np.mean([b[i] for b in boxes])) for i in range(4)] + self.assertEqual(average_boxes(boxes), expected) class TestAttribute(unittest.TestCase): diff --git a/frigate/util/object.py b/frigate/util/object.py index 311fccf477..b4ee72dc07 100644 --- a/frigate/util/object.py +++ b/frigate/util/object.py @@ -339,18 +339,13 @@ def reduce_boxes(boxes, iou_threshold=0.0): def average_boxes(boxes: list[list[int, int, int, int]]) -> list[int, int, int, int]: """Return a box that is the average of a list of boxes.""" - x_mins = [] - y_mins = [] - x_max = [] - y_max = [] - - for box in boxes: - x_mins.append(box[0]) - y_mins.append(box[1]) - x_max.append(box[2]) - y_max.append(box[3]) - - return [np.mean(x_mins), np.mean(y_mins), np.mean(x_max), np.mean(y_max)] + n = len(boxes) + return [ + sum(box[0] for box in boxes) / n, + sum(box[1] for box in boxes) / n, + sum(box[2] for box in boxes) / n, + sum(box[3] for box in boxes) / n, + ] def median_of_boxes(boxes: list[list[int, int, int, int]]) -> list[int, int, int, int]: