fix: drop degenerate YOLO boxes before NMS in post_process_nms_yolo

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

View File

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