Skip to content

Commit 6e0f046

Browse files
Fix RotatedBbox.wrap() by assigning correct attributes (#1993)
<!-- Contributing guide: https://github.com/open-edge-platform/datumaro/blob/develop/contributing.md --> <!-- Please add a summary of changes. You may use Copilot to auto-generate the PR description but please consider including any other relevant facts which Copilot may be unaware of (such as design choices and testing procedure). Add references to the relevant issues and pull requests if any like so: Resolves #111 and #222. Depends on #1000 (for series of dependent commits). --> The `RotatedBbox.wrap()` function tries to access wrong attributes. This causes errors in dataset transforms that include rotated bounding boxes. See the example: ```python """Minimal reproducible example demonstrating the bug in RotatedBbox.wrap method. The wrap method tries to access item.x and item.y, but RotatedBbox only has cx and cy properties (center coordinates), not x and y. """ from datumaro.components.annotation import RotatedBbox bbox = RotatedBbox(cx=100.0, cy=200.0, w=50.0, h=30.0, r=45.0, label=0) wrapped_bbox = bbox.wrap(label=1) ``` ### Checklist <!-- Put an 'x' in all the boxes that apply --> - [ ] I have added tests to cover my changes or documented any manual tests. - [ ] I have updated the [documentation](https://github.com/open-edge-platform/datumaro/tree/develop/docs) accordingly --------- Signed-off-by: williamcorsel <[email protected]>
1 parent 532d9e5 commit 6e0f046

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/datumaro/components/annotation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ def wrap(item, **kwargs):
12801280
Returns:
12811281
RotatedBbox: A new RotatedBbox instance with updated attributes.
12821282
"""
1283-
d = {"x": item.x, "y": item.y, "w": item.w, "h": item.h, "r": item.r}
1283+
d = {"cx": item.cx, "cy": item.cy, "w": item.w, "h": item.h, "r": item.r}
12841284
d.update(kwargs)
12851285
return attr.evolve(item, **d)
12861286

tests/unit/test_annotation.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,44 @@ def test_get_points(self, fxt_ellipses: List[Ellipse]):
4646

4747
class RotatedBboxTest:
4848
@pytest.fixture
49-
def fxt_rot_bbox(self):
49+
def fxt_rot_bbox(self) -> RotatedBbox:
5050
coords = np.random.randint(0, 180, size=(5,), dtype=np.uint8)
5151
return RotatedBbox(coords[0], coords[1], coords[2], coords[3], coords[4])
5252

53-
def test_create_polygon(self, fxt_rot_bbox):
53+
def test_create_polygon(self, fxt_rot_bbox: RotatedBbox) -> None:
5454
polygon = fxt_rot_bbox.as_polygon()
5555

5656
expected = RotatedBbox.from_rectangle(polygon)
5757
assert fxt_rot_bbox == expected
5858

59+
class RotatedBboxWrapTest:
60+
@pytest.fixture
61+
def fxt_rot_bbox(self) -> RotatedBbox:
62+
return RotatedBbox(
63+
cx=50.0, cy=60.0, w=40.0, h=20.0, r=30.0, label=1, id=5, group=3, attributes={"score": 0.95}
64+
)
65+
66+
def test_wrap_creates_new_instance(self, fxt_rot_bbox: RotatedBbox) -> None:
67+
wrapped = RotatedBbox.wrap(fxt_rot_bbox)
68+
assert isinstance(wrapped, RotatedBbox)
69+
assert wrapped is not fxt_rot_bbox
70+
71+
def test_wrap_updates_attributes(self, fxt_rot_bbox: RotatedBbox) -> None:
72+
wrapped = RotatedBbox.wrap(fxt_rot_bbox, cx=100.0, label=10, group=20)
73+
assert wrapped.cx == 100.0
74+
assert (wrapped.cy, wrapped.w, wrapped.h, wrapped.r) == (
75+
fxt_rot_bbox.cy,
76+
fxt_rot_bbox.w,
77+
fxt_rot_bbox.h,
78+
fxt_rot_bbox.r,
79+
)
80+
assert (wrapped.label, wrapped.group) == (10, 20)
81+
82+
def test_wrap_preserves_other_attributes(self, fxt_rot_bbox: RotatedBbox) -> None:
83+
wrapped = RotatedBbox.wrap(fxt_rot_bbox, cx=100.0)
84+
assert (wrapped.label, wrapped.id, wrapped.group) == (1, 5, 3)
85+
assert wrapped.attributes == {"score": 0.95}
86+
5987

6088
@pytest.fixture
6189
def fxt_index_mask():

0 commit comments

Comments
 (0)