Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detection Offset - Use bbox for percent padding #970

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading