From 8786ad0466fb75ef625e25917d31938bf8262bb7 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 1 Apr 2026 12:55:31 -0300 Subject: [PATCH] fix: drop degenerate YOLO boxes before NMS in post_process_nms_yolo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a YOLO model predicts w=0 or h=0, the cx±w/2 conversion in __post_process_nms_yolo produces boxes where x2<=x1 or y2<=y1. These pass through NMS unchanged and reach the norfair distance function, where they trigger a divide-by-zero producing NaN. Add a vectorised validity mask immediately after the xyxy conversion to drop any box with non-positive width or height before NMS runs. Co-Authored-By: Claude Sonnet 4.6 --- frigate/util/model.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frigate/util/model.py b/frigate/util/model.py index 338303e2d7..ea73af2580 100644 --- a/frigate/util/model.py +++ b/frigate/util/model.py @@ -205,6 +205,15 @@ def __post_process_nms_yolo(predictions: np.ndarray, width, height) -> np.ndarra boxes_xyxy[:, 3] = boxes[:, 1] + boxes[:, 3] / 2 boxes = boxes_xyxy + # Drop degenerate boxes (zero or negative width/height). These arise when + # a YOLO prediction has w=0 or h=0, which after cx±w/2 conversion gives + # x2<=x1 or y2<=y1. They pass through NMS unchanged and then cause a + # divide-by-zero NaN in the norfair distance function (see norfair_tracker.py). + valid = (boxes[:, 2] > boxes[:, 0]) & (boxes[:, 3] > boxes[:, 1]) + boxes = boxes[valid] + scores = scores[valid] + class_ids = class_ids[valid] + # run NMS indices = cv2.dnn.NMSBoxes(boxes, scores, score_threshold=0.4, nms_threshold=0.4) detections = np.zeros((20, 6), np.float32)