update denormalize_boxes docstring and examples; update from_google_gemini_2_0, from_google_gemini_2_5 and from_moondream to use latest version of denormalize_boxes

This commit is contained in:
SkalskiP committed 2025-11-14 13:12:39 +01:00
1 parent f563c5a634
commit dfd2a60855
3 files changed
+25 -31

No files matched your search

+6 -8
View File
@@ -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.]
# ])
+18 -22
View File
@@ -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),
)
+1 -1
View File
@@ -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)),