From 1fdee5f2e7c4bad54295817cb10c12f0c867be70 Mon Sep 17 00:00:00 2001 From: pgregg88 <4943027+pgregg88@users.noreply.github.com> Date: Sat, 13 Jun 2026 13:02:08 -0500 Subject: [PATCH] Fix NaN crash in norfair distance() by guarding zero-area boxes distance() divides change-in-position and width/height ratios by the box dimensions. When a Kalman estimate collapses or a detection box is clamped to zero width/height (observed intermittently on an autotracking PTZ camera during motion), the divisions produce NaN, norfair raises "Received nan values from distance function", and the camera process exits and is not respawned. Clamp the box dimensions to >= 1px (pixel coordinates, so < 1 is meaningless), which protects all four division sites with no behavior change for valid boxes. Discussion: https://github.com/blakeblackshear/frigate/discussions/23471 Signed-off-by: pgregg88 <4943027+pgregg88@users.noreply.github.com> --- frigate/track/norfair_tracker.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 84a0f390a3..f79f23b7f6 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -42,8 +42,13 @@ def distance(detection: np.ndarray, estimate: np.ndarray) -> float: # ultimately, this should try and estimate distance in 3-dimensional space # consider change in location, width, and height - estimate_dim = np.diff(estimate, axis=0).flatten() - detection_dim = np.diff(detection, axis=0).flatten() + # Guard against degenerate (zero-area) boxes: a collapsed Kalman estimate or + # a zero-dimension detection makes the divisions below produce NaN, which + # norfair rejects by raising ValueError ("Received nan values from distance + # function") and crashes the camera process. Clamp dims to >= 1px (these are + # pixel coordinates, so a dimension < 1 is meaningless). + estimate_dim = np.maximum(np.abs(np.diff(estimate, axis=0).flatten()), 1.0) + detection_dim = np.maximum(np.abs(np.diff(detection, axis=0).flatten()), 1.0) # get bottom center positions detection_position = np.array(