Skip to content

Commit

Permalink
Detection Offset - Use bbox for percent padding
Browse files Browse the repository at this point in the history
  • Loading branch information
sberan committed Jan 22, 2025
1 parent b566c2e commit abdb3cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -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"

0 comments on commit abdb3cf

Please sign in to comment.