From abdb3cfebc08ca1092c5a5e4eb0ec9e2c863d941 Mon Sep 17 00:00:00 2001 From: Sam Beran Date: Wed, 22 Jan 2025 16:19:59 -0600 Subject: [PATCH] Detection Offset - Use bbox for percent padding --- .../core_steps/transformations/detection_offset/v1.py | 11 ++++++----- .../transformations/test_detection_offset.py | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/inference/core/workflows/core_steps/transformations/detection_offset/v1.py b/inference/core/workflows/core_steps/transformations/detection_offset/v1.py index fe1a844649..bb1b4c4e09 100644 --- a/inference/core/workflows/core_steps/transformations/detection_offset/v1.py +++ b/inference/core/workflows/core_steps/transformations/detection_offset/v1.py @@ -118,7 +118,7 @@ def run( offset_height: int, units: str = "Pixels", ) -> BlockResult: - use_percentage = units == "Percent (%)" + use_percentage = units == "Percent (%) - of bounding box width / height" return [ { "predictions": offset_detections( @@ -148,18 +148,19 @@ def offset_detections( _detections.xyxy = np.array( [ ( - max(0, x1 - int(image_dimensions[i][1] * offset_width / 200)), - max(0, y1 - int(image_dimensions[i][0] * offset_height / 200)), + max(0, x1 - int(box_width * offset_width / 200)), + max(0, y1 - int(box_height * offset_height / 200)), min( image_dimensions[i][1], - x2 + int(image_dimensions[i][1] * offset_width / 200), + x2 + int(box_width * offset_width / 200), ), min( image_dimensions[i][0], - y2 + int(image_dimensions[i][0] * offset_height / 200), + y2 + int(box_height * offset_height / 200), ), ) for i, (x1, y1, x2, y2) in enumerate(_detections.xyxy) + for box_width, box_height in [(x2 - x1, y2 - y1)] ] ) else: diff --git a/tests/workflows/unit_tests/core_steps/transformations/test_detection_offset.py b/tests/workflows/unit_tests/core_steps/transformations/test_detection_offset.py index 3ad3517ad1..c91f79c657 100644 --- a/tests/workflows/unit_tests/core_steps/transformations/test_detection_offset.py +++ b/tests/workflows/unit_tests/core_steps/transformations/test_detection_offset.py @@ -163,7 +163,7 @@ def test_offset_detection_when_nothing_predicted() -> None: def test_offset_detection_with_percentage() -> None: # given detections = sv.Detections( - xyxy=np.array([[100, 200, 300, 400]], dtype=np.float64), + xyxy=np.array([[100, 200, 400, 600]], dtype=np.float64), class_id=np.array([1]), confidence=np.array([0.5], dtype=np.float64), data={ @@ -184,7 +184,7 @@ def test_offset_detection_with_percentage() -> None: # then x1, y1, x2, y2 = result.xyxy[0] - assert x1 == 68, "Left corner should be moved by 10% of image width to the left" - assert y1 == 168, "Top corner should be moved by 10% of image height to the top" - assert x2 == 332, "Right corner should be moved by 10% of image width to the right" - assert y2 == 432, "Bottom corner should be moved by 10% of image height to the bottom" + assert x1 == 85, "Left corner should be moved by 5% of detection width to the left" + assert y1 == 180, "Top corner should be moved by 5% of detection height to the top" + assert x2 == 415, "Right corner should be moved by 5% of detection width to the right" + assert y2 == 620, "Bottom corner should be moved by 5% of detection height to the bottom"