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 <noreply@anthropic.com>
This commit is contained in:
root 2026-04-01 12:54:02 -03:00
parent adc8c2a6e8
commit c3f06d627c

View File

@ -45,6 +45,15 @@ def distance(detection: np.ndarray, estimate: np.ndarray) -> float:
estimate_dim = np.diff(estimate, axis=0).flatten() estimate_dim = np.diff(estimate, axis=0).flatten()
detection_dim = np.diff(detection, 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 # get bottom center positions
detection_position = np.array( detection_position = np.array(
[np.average(detection[:, 0]), np.max(detection[:, 1])] [np.average(detection[:, 0]), np.max(detection[:, 1])]