From c3f06d627c5893464ecf599b3e8f106a4ed5b81c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 1 Apr 2026 12:54:02 -0300 Subject: [PATCH] fix: guard against zero-dimension boxes in norfair distance() When the Kalman filter produces a degenerate estimate where width or height is zero (or the model outputs a zero-area detection), the distance() function divides by zero, producing NaN. NaN propagates into norfair's internal tracking state and causes a hard crash: RuntimeWarning: divide by zero encountered in double_scalars ValueError: Received nan values from distance function Guard both estimate_dim and detection_dim before any division, returning float("inf") so norfair treats the pairing as unmatched rather than crashing. Fixes #9742. Co-Authored-By: Claude Sonnet 4.6 --- frigate/track/norfair_tracker.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 84a0f390a3..6aae9d1eed 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -45,6 +45,15 @@ def distance(detection: np.ndarray, estimate: np.ndarray) -> float: estimate_dim = np.diff(estimate, axis=0).flatten() detection_dim = np.diff(detection, axis=0).flatten() + # Guard against zero-dimension estimates or detections (e.g. degenerate boxes + # produced by the Kalman filter on the first few frames). Dividing by zero + # yields NaN which propagates into norfair and causes a hard crash with + # "Received nan values from distance function". + if estimate_dim[0] <= 0 or estimate_dim[1] <= 0: + return float("inf") + if detection_dim[0] <= 0 or detection_dim[1] <= 0: + return float("inf") + # get bottom center positions detection_position = np.array( [np.average(detection[:, 0]), np.max(detection[:, 1])]