Skip to content

Commit

Permalink
Merge pull request #621 from roboflow/nick/bug-fix/detection-offset-n…
Browse files Browse the repository at this point in the history
…ull-images

Fix Detection Offset Bug
  • Loading branch information
PawelPeczek-Roboflow authored Aug 30, 2024
2 parents 1da5849 + c985496 commit c43af93
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ def offset_detections(
if len(detections) == 0:
return detections
_detections = deepcopy(detections)
image_dimensions = detections.data["image_dimensions"]
_detections.xyxy = np.array(
[
(
x1 - offset_width // 2,
y1 - offset_height // 2,
x2 + offset_width // 2,
y2 + offset_height // 2,
max(0, x1 - offset_width // 2),
max(0, y1 - offset_height // 2),
min(image_dimensions[i][1], x2 + offset_width // 2),
min(image_dimensions[i][0], y2 + offset_height // 2),
)
for (x1, y1, x2, y2) in _detections.xyxy
for i, (x1, y1, x2, y2) in enumerate(_detections.xyxy)
]
)
_detections[parent_id_key] = detections[detection_id_key].copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_offset_detection() -> None:
"detection_id": np.array(["two"]),
"class_name": np.array(["car"]),
"parent_id": np.array(["p2"]),
"image_dimensions": np.array([[640, 640]]),
},
)

Expand All @@ -108,6 +109,42 @@ def test_offset_detection() -> None:
detections["parent_id"][0]
), "New detection id (random) must be assigned"

def test_offset_detection_when_larger_than_image() -> None:
# given
detections = sv.Detections(
xyxy=np.array([[90, 190, 110, 210]], dtype=np.float64),
class_id=np.array([1]),
confidence=np.array([0.5], dtype=np.float64),
data={
"detection_id": np.array(["two"]),
"class_name": np.array(["car"]),
"parent_id": np.array(["p2"]),
"image_dimensions": np.array([[640, 640]]),
},
)

# when
result = offset_detections(
detections=detections,
offset_width=2000,
offset_height=2000,
)

# then
x1, y1, x2, y2 = result.xyxy[0]
assert x1 == 0, "Left corner should be at the image border"
assert y1 == 0, "Top corner should be at the image border"
assert x2 == 640, "Right corner should be at the image border"
assert y2 == 640, "Bottom corner should be at the image border"
assert result["parent_id"] == str(
detections["detection_id"][0]
), "Parent id should be set to origin detection id"
assert result["detection_id"] != str(
detections["parent_id"][0]
), "New detection id (random) must be assigned"




def test_offset_detection_when_nothing_predicted() -> None:
# given
Expand Down

0 comments on commit c43af93

Please sign in to comment.