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>
This commit is contained in:
pgregg88 2026-06-13 13:02:08 -05:00
parent d7ad3ba699
commit 1fdee5f2e7

View File

@ -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(