Skip to content

NVR with realtime local object detection for IP cameras

perf(util): cut redundant work in per-frame detection consolidation (#23522)

Daniela2b46f5d84fc
diff --git a/frigate/test/test_video.py b/frigate/test/test_video.py
index 8612990e2e..d09a60559f 100644
--- a/frigate/test/test_video.py
+++ b/frigate/test/test_video.py
@@ -82,6 +82,24 @@ class TestRegion(unittest.TestCase):
assert len(cluster_candidates) == 2
+ def test_cluster_candidates_partition_boxes(self):
+ # every box index must appear in exactly one cluster (no box used twice,
+ # none dropped) - the invariant the used-box tracking enforces
+ boxes = [
+ (100, 100, 200, 200),
+ (202, 150, 252, 200),
+ (210, 160, 260, 210),
+ (900, 900, 950, 950),
+ (905, 905, 955, 955),
+ ]
+
+ cluster_candidates = get_cluster_candidates(
+ self.frame_shape, self.min_region_size, boxes
+ )
+
+ assigned = [idx for cluster in cluster_candidates for idx in cluster]
+ self.assertEqual(sorted(assigned), list(range(len(boxes))))
+
def test_transliterate_to_latin(self):
self.assertEqual(transliterate_to_latin("frégate"), "fregate")
self.assertEqual(transliterate_to_latin("utilité"), "utilite")
diff --git a/frigate/util/object.py b/frigate/util/object.py
index 7c7edc10c5..311fccf477 100644
--- a/frigate/util/object.py
+++ b/frigate/util/object.py
@@ -401,13 +401,13 @@ def get_cluster_candidates(frame_shape, min_region, boxes):
# determined by the max_region size minus half the box + 20%
# TODO: see if we can do this with numpy
cluster_candidates = []
- used_boxes = []
+ used_boxes = set()
# loop over each box
for current_index, b in enumerate(boxes):
if current_index in used_boxes:
continue
cluster = [current_index]
- used_boxes.append(current_index)
+ used_boxes.add(current_index)
cluster_boundary = get_cluster_boundary(b, min_region)
# find all other boxes that fit inside the boundary
for compare_index, compare_box in enumerate(boxes):
@@ -436,7 +436,7 @@ def get_cluster_candidates(frame_shape, min_region, boxes):
if should_cluster:
cluster.append(compare_index)
- used_boxes.append(compare_index)
+ used_boxes.add(compare_index)
cluster_candidates.append(cluster)
# return the unique clusters only
@@ -558,6 +558,7 @@ def reduce_detections(
current_detection = sorted_by_area[current_detection_idx]
current_label = current_detection[0]
current_box = current_detection[2]
+ current_area = area(current_box)
overlap = 0
for to_check_idx in range(
min(current_detection_idx + 1, len(sorted_by_area)),
@@ -568,14 +569,14 @@ def reduce_detections(
# if area of current detection / area of check < 5% they should not be compared
# this covers cases where a large car parked in a driveway doesn't block detections
# of cars in the street behind it
- if area(current_box) / area(to_check) < 0.05:
+ if current_area / area(to_check) < 0.05:
continue
intersect_box = intersection(current_box, to_check)
# if % of smaller detection is inside of another detection, consolidate
- if intersect_box is not None and area(intersect_box) / area(
- current_box
- ) > LABEL_CONSOLIDATION_MAP.get(
+ if intersect_box is not None and area(
+ intersect_box
+ ) / current_area > LABEL_CONSOLIDATION_MAP.get(
current_label, LABEL_CONSOLIDATION_DEFAULT
):
overlap = 1