From e0fb73f18a94043cb56c28e8e019dfb320c1d083 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 10 Jul 2024 12:13:07 +0200 Subject: [PATCH 1/2] `xywh_to_xyxy` and `xcycwh_to_xyxy` box utils --- docs/detection/utils.md | 12 ++++++ supervision/__init__.py | 2 + supervision/detection/core.py | 2 +- supervision/detection/utils.py | 76 +++++++++++++++++++++++++++++++-- test/detection/test_utils.py | 78 +++++++++++++++++++++++++++++++++- 5 files changed, 164 insertions(+), 6 deletions(-) diff --git a/docs/detection/utils.md b/docs/detection/utils.md index ea98c868..85fcacda 100644 --- a/docs/detection/utils.md +++ b/docs/detection/utils.md @@ -80,6 +80,18 @@ comments: true

contains_holes

+:::supervision.detection.utils.xywh_to_xyxy + +
+

xywh_to_xyxy

+
+ +:::supervision.detection.utils.xcycwh_to_xyxy + +
+

xcycwh_to_xyxy

+
+ :::supervision.detection.utils.contains_holes
diff --git a/supervision/__init__.py b/supervision/__init__.py index 4f28d49f..8fb015c9 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -67,6 +67,8 @@ from supervision.detection.utils import ( polygon_to_mask, polygon_to_xyxy, scale_boxes, + xywh_to_xyxy, + xcycwh_to_xyxy, ) from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import ( diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 61a08b7e..48711bce 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -686,7 +686,7 @@ class Detections: if np.asarray(xywh).shape[0] == 0: return cls.empty() - xyxy = xywh_to_xyxy(boxes_xywh=xywh) + xyxy = xywh_to_xyxy(xywh=xywh) return cls(xyxy=xyxy, mask=mask) @classmethod diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index 5b92aedd..e7090be3 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -228,10 +228,78 @@ def pad_boxes(xyxy: np.ndarray, px: int, py: Optional[int] = None) -> np.ndarray return result -def xywh_to_xyxy(boxes_xywh: np.ndarray) -> np.ndarray: - xyxy = boxes_xywh.copy() - xyxy[:, 2] = boxes_xywh[:, 0] + boxes_xywh[:, 2] - xyxy[:, 3] = boxes_xywh[:, 1] + boxes_xywh[:, 3] +def xywh_to_xyxy(xywh: np.ndarray) -> np.ndarray: + """ + Converts bounding box coordinates from `(x, y, width, height)` + format to `(x_min, y_min, x_max, y_max)` format. + + Args: + xywh (np.ndarray): A numpy array of shape `(N, 4)` where each row + corresponds to a bounding box in the format `(x, y, width, height)`. + + Returns: + np.ndarray: A numpy array of shape `(N, 4)` where each row corresponds + to a bounding box in the format `(x_min, y_min, x_max, y_max)`. + + Examples: + ```python + import numpy as np + import supervision as sv + + xywh = np.array([ + [10, 20, 30, 40], + [15, 25, 35, 45] + ]) + + sv.xywh_to_xyxy(xywh=xywh) + # array([ + # [10, 20, 40, 60], + # [15, 25, 50, 70] + # ]) + ``` + """ + xyxy = xywh.copy() + xyxy[:, 2] = xywh[:, 0] + xywh[:, 2] + xyxy[:, 3] = xywh[:, 1] + xywh[:, 3] + return xyxy + + +def xcycwh_to_xyxy(xcycwh: np.ndarray) -> np.ndarray: + """ + Converts bounding box coordinates from `(center_x, center_y, width, height)` + format to `(x_min, y_min, x_max, y_max)` format. + + Args: + xcycwh (np.ndarray): A numpy array of shape `(N, 4)` where each row + corresponds to a bounding box in the format `(center_x, center_y, width, + height)`. + + Returns: + np.ndarray: A numpy array of shape `(N, 4)` where each row corresponds + to a bounding box in the format `(x_min, y_min, x_max, y_max)`. + + Examples: + ```python + import numpy as np + import supervision as sv + + xcycwh = np.array([ + [50, 50, 20, 30], + [30, 40, 10, 15] + ]) + + sv.xcycwh_to_xyxy(xcycwh=xcycwh) + # array([ + # [40, 35, 60, 65], + # [25, 32.5, 35, 47.5] + # ]) + ``` + """ + xyxy = xcycwh.copy() + xyxy[:, 0] = xcycwh[:, 0] - xcycwh[:, 2] / 2 + xyxy[:, 1] = xcycwh[:, 1] - xcycwh[:, 3] / 2 + xyxy[:, 2] = xcycwh[:, 0] + xcycwh[:, 2] / 2 + xyxy[:, 3] = xcycwh[:, 1] + xcycwh[:, 3] / 2 return xyxy diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index 20c818e6..a19a518a 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -16,7 +16,7 @@ from supervision.detection.utils import ( merge_data, move_boxes, process_roboflow_result, - scale_boxes, + scale_boxes, xcycwh_to_xyxy, xywh_to_xyxy, ) TEST_MASK = np.zeros((1, 1000, 1000), dtype=bool) @@ -1090,3 +1090,79 @@ def test_contains_multiple_segments( with exception: result = contains_multiple_segments(mask=mask, connectivity=connectivity) assert result == expected_result + + +@pytest.mark.parametrize( + "xywh, expected_result", + [ + ( + np.array([[10, 20, 30, 40]]), + np.array([[10, 20, 40, 60]]) + ), # standard case + ( + np.array([[0, 0, 0, 0]]), + np.array([[0, 0, 0, 0]]) + ), # zero size bounding box + ( + np.array([[50, 50, 100, 100]]), + np.array([[50, 50, 150, 150]]) + ), # large bounding box + ( + np.array([[-10, -20, 30, 40]]), + np.array([[-10, -20, 20, 20]]) + ), # negative coordinates + ( + np.array([[50, 50, 0, 30]]), + np.array([[50, 50, 50, 80]]) + ), # zero width + ( + np.array([[50, 50, 20, 0]]), + np.array([[50, 50, 70, 50]]) + ), # zero height + ( + np.array([]).reshape(0, 4), + np.array([]).reshape(0, 4) + ), # empty array + ] +) +def test_xywh_to_xyxy(xywh: np.ndarray, expected_result: np.ndarray) -> None: + result = xywh_to_xyxy(xywh) + np.testing.assert_array_equal(result, expected_result) + + +@pytest.mark.parametrize( + "xcycwh, expected_result", + [ + ( + np.array([[50, 50, 20, 30]]), + np.array([[40, 35, 60, 65]]) + ), # standard case + ( + np.array([[0, 0, 0, 0]]), + np.array([[0, 0, 0, 0]]) + ), # zero size bounding box + ( + np.array([[50, 50, 100, 100]]), + np.array([[0, 0, 100, 100]]) + ), # large bounding box centered at (50, 50) + ( + np.array([[-10, -10, 20, 30]]), + np.array([[-20, -25, 0, 5]]) + ), # negative coordinates + ( + np.array([[50, 50, 0, 30]]), + np.array([[50, 35, 50, 65]]) + ), # zero width + ( + np.array([[50, 50, 20, 0]]), + np.array([[40, 50, 60, 50]]) + ), # zero height + ( + np.array([]).reshape(0, 4), + np.array([]).reshape(0, 4) + ), # empty array + ] +) +def test_xcycwh_to_xyxy(xcycwh: np.ndarray, expected_result: np.ndarray) -> None: + result = xcycwh_to_xyxy(xcycwh) + np.testing.assert_array_equal(result, expected_result) \ No newline at end of file From 2ed2a793cdb93a9fa2ff1aaf91492431b3cec50d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:14:58 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/__init__.py | 2 +- test/detection/test_utils.py | 68 +++++++++++------------------------- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/supervision/__init__.py b/supervision/__init__.py index 8fb015c9..a4126f31 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -67,8 +67,8 @@ from supervision.detection.utils import ( polygon_to_mask, polygon_to_xyxy, scale_boxes, - xywh_to_xyxy, xcycwh_to_xyxy, + xywh_to_xyxy, ) from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import ( diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index a19a518a..9224c676 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -16,7 +16,9 @@ from supervision.detection.utils import ( merge_data, move_boxes, process_roboflow_result, - scale_boxes, xcycwh_to_xyxy, xywh_to_xyxy, + scale_boxes, + xcycwh_to_xyxy, + xywh_to_xyxy, ) TEST_MASK = np.zeros((1, 1000, 1000), dtype=bool) @@ -1095,35 +1097,20 @@ def test_contains_multiple_segments( @pytest.mark.parametrize( "xywh, expected_result", [ - ( - np.array([[10, 20, 30, 40]]), - np.array([[10, 20, 40, 60]]) - ), # standard case - ( - np.array([[0, 0, 0, 0]]), - np.array([[0, 0, 0, 0]]) - ), # zero size bounding box + (np.array([[10, 20, 30, 40]]), np.array([[10, 20, 40, 60]])), # standard case + (np.array([[0, 0, 0, 0]]), np.array([[0, 0, 0, 0]])), # zero size bounding box ( np.array([[50, 50, 100, 100]]), - np.array([[50, 50, 150, 150]]) + np.array([[50, 50, 150, 150]]), ), # large bounding box ( np.array([[-10, -20, 30, 40]]), - np.array([[-10, -20, 20, 20]]) + np.array([[-10, -20, 20, 20]]), ), # negative coordinates - ( - np.array([[50, 50, 0, 30]]), - np.array([[50, 50, 50, 80]]) - ), # zero width - ( - np.array([[50, 50, 20, 0]]), - np.array([[50, 50, 70, 50]]) - ), # zero height - ( - np.array([]).reshape(0, 4), - np.array([]).reshape(0, 4) - ), # empty array - ] + (np.array([[50, 50, 0, 30]]), np.array([[50, 50, 50, 80]])), # zero width + (np.array([[50, 50, 20, 0]]), np.array([[50, 50, 70, 50]])), # zero height + (np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array + ], ) def test_xywh_to_xyxy(xywh: np.ndarray, expected_result: np.ndarray) -> None: result = xywh_to_xyxy(xywh) @@ -1133,36 +1120,21 @@ def test_xywh_to_xyxy(xywh: np.ndarray, expected_result: np.ndarray) -> None: @pytest.mark.parametrize( "xcycwh, expected_result", [ - ( - np.array([[50, 50, 20, 30]]), - np.array([[40, 35, 60, 65]]) - ), # standard case - ( - np.array([[0, 0, 0, 0]]), - np.array([[0, 0, 0, 0]]) - ), # zero size bounding box + (np.array([[50, 50, 20, 30]]), np.array([[40, 35, 60, 65]])), # standard case + (np.array([[0, 0, 0, 0]]), np.array([[0, 0, 0, 0]])), # zero size bounding box ( np.array([[50, 50, 100, 100]]), - np.array([[0, 0, 100, 100]]) + np.array([[0, 0, 100, 100]]), ), # large bounding box centered at (50, 50) ( np.array([[-10, -10, 20, 30]]), - np.array([[-20, -25, 0, 5]]) + np.array([[-20, -25, 0, 5]]), ), # negative coordinates - ( - np.array([[50, 50, 0, 30]]), - np.array([[50, 35, 50, 65]]) - ), # zero width - ( - np.array([[50, 50, 20, 0]]), - np.array([[40, 50, 60, 50]]) - ), # zero height - ( - np.array([]).reshape(0, 4), - np.array([]).reshape(0, 4) - ), # empty array - ] + (np.array([[50, 50, 0, 30]]), np.array([[50, 35, 50, 65]])), # zero width + (np.array([[50, 50, 20, 0]]), np.array([[40, 50, 60, 50]])), # zero height + (np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array + ], ) def test_xcycwh_to_xyxy(xcycwh: np.ndarray, expected_result: np.ndarray) -> None: result = xcycwh_to_xyxy(xcycwh) - np.testing.assert_array_equal(result, expected_result) \ No newline at end of file + np.testing.assert_array_equal(result, expected_result)