diff --git a/supervision/detection/utils/boxes.py b/supervision/detection/utils/boxes.py index 44b78cba..01fcdc76 100644 --- a/supervision/detection/utils/boxes.py +++ b/supervision/detection/utils/boxes.py @@ -123,12 +123,13 @@ def denormalize_boxes( import numpy as np import supervision as sv - normalized_xyxy = np.array([ + xyxy = np.array([ [0.1, 0.2, 0.5, 0.6], [0.3, 0.4, 0.7, 0.8], [0.2, 0.1, 0.6, 0.5] ]) - sv.denormalize_boxes(normalized_xyxy, (1280, 720)) + + sv.denormalize_boxes(xyxy, (1280, 720)) # array([ # [128., 144., 640., 432.], # [384., 288., 896., 576.], @@ -140,14 +141,11 @@ def denormalize_boxes( import numpy as np import supervision as sv - normalized_xyxy = np.array([ + xyxy = np.array([ [256., 128., 768., 640.] ]) - result = sv.denormalize_boxes( - normalized_xyxy, - (1280, 720), - normalization_factor=1024.0 - ) + + sv.denormalize_boxes(xyxy, (1280, 720), normalization_factor=1024.0) # array([ # [320., 90., 960., 450.] # ]) diff --git a/supervision/detection/vlm.py b/supervision/detection/vlm.py index 71207554..2f9b60dd 100644 --- a/supervision/detection/vlm.py +++ b/supervision/detection/vlm.py @@ -538,7 +538,7 @@ def from_google_gemini_2_0( return np.empty((0, 4)), None, np.empty((0,), dtype=str) labels = [] - boxes_list = [] + xyxy = [] for item in data: if "box_2d" not in item or "label" not in item: @@ -546,18 +546,16 @@ def from_google_gemini_2_0( labels.append(item["label"]) box = item["box_2d"] # Gemini bbox order is [y_min, x_min, y_max, x_max] - boxes_list.append( - denormalize_boxes( - np.array([box[1], box[0], box[3], box[2]]).astype(np.float64), - resolution_wh=(w, h), - normalization_factor=1000, - ) - ) + xyxy.append([box[1], box[0], box[3], box[2]]) - if not boxes_list: + if len(xyxy) == 0: return np.empty((0, 4)), None, np.empty((0,), dtype=str) - xyxy = np.array(boxes_list) + xyxy = denormalize_boxes( + np.array(xyxy, dtype=np.float64), + resolution_wh=(w, h), + normalization_factor=1000, + ) class_name = np.array(labels) class_id = None @@ -649,10 +647,10 @@ def from_google_gemini_2_5( box = item["box_2d"] # Gemini bbox order is [y_min, x_min, y_max, x_max] absolute_bbox = denormalize_boxes( - np.array([box[1], box[0], box[3], box[2]]).astype(np.float64), + np.array([[box[1], box[0], box[3], box[2]]]).astype(np.float64), resolution_wh=(w, h), normalization_factor=1000, - ) + )[0] boxes_list.append(absolute_bbox) if "mask" in item: @@ -735,7 +733,7 @@ def from_google_gemini_2_5( def from_moondream( result: dict, resolution_wh: tuple[int, int], -) -> tuple[np.ndarray]: +) -> np.ndarray: """ Parse and scale bounding boxes from moondream JSON output. @@ -773,7 +771,7 @@ def from_moondream( if "objects" not in result or not isinstance(result["objects"], list): return np.empty((0, 4), dtype=float) - denormalize_xyxy = [] + xyxy = [] for item in result["objects"]: if not all(k in item for k in ["x_min", "y_min", "x_max", "y_max"]): @@ -784,14 +782,12 @@ def from_moondream( x_max = item["x_max"] y_max = item["y_max"] - denormalize_xyxy.append( - denormalize_boxes( - np.array([x_min, y_min, x_max, y_max]).astype(np.float64), - resolution_wh=(w, h), - ) - ) + xyxy.append([x_min, y_min, x_max, y_max]) - if not denormalize_xyxy: + if len(xyxy) == 0: return np.empty((0, 4)) - return np.array(denormalize_xyxy, dtype=float) + return denormalize_boxes( + np.array(xyxy).astype(np.float64), + resolution_wh=(w, h), + ) diff --git a/test/detection/utils/test_boxes.py b/test/detection/utils/test_boxes.py index 787c970f..66d0d999 100644 --- a/test/detection/utils/test_boxes.py +++ b/test/detection/utils/test_boxes.py @@ -150,7 +150,7 @@ def test_scale_boxes( @pytest.mark.parametrize( - "normalized_xyxy, resolution_wh, normalization_factor, expected_result, exception", + "xyxy, resolution_wh, normalization_factor, expected_result, exception", [ ( np.empty(shape=(0, 4)),