Skip to content

Commit 77e95fc

Browse files
Fix non-rotated format to rotated format conversion logic (#8926)
Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent 7940146 commit 77e95fc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

torchvision/transforms/v2/functional/_meta.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,24 @@ def _xyxyxyxy_to_xywhr(xyxyxyxy: torch.Tensor, inplace: bool) -> torch.Tensor:
263263
return xyxyxyxy[..., :5].to(dtype)
264264

265265

266+
def is_rotated_bounding_box_format(format: BoundingBoxFormat) -> bool:
267+
return format.value in [
268+
BoundingBoxFormat.XYWHR.value,
269+
BoundingBoxFormat.CXCYWHR.value,
270+
BoundingBoxFormat.XYXYXYXY.value,
271+
]
272+
273+
266274
def _convert_bounding_box_format(
267275
bounding_boxes: torch.Tensor, old_format: BoundingBoxFormat, new_format: BoundingBoxFormat, inplace: bool = False
268276
) -> torch.Tensor:
269277

270278
if new_format == old_format:
271279
return bounding_boxes
272280

281+
if is_rotated_bounding_box_format(old_format) ^ is_rotated_bounding_box_format(new_format):
282+
raise ValueError("Cannot convert between rotated and unrotated bounding boxes.")
283+
273284
# TODO: Add _xywh_to_cxcywh and _cxcywh_to_xywh to improve performance
274285
if old_format == BoundingBoxFormat.XYWH:
275286
bounding_boxes = _xywh_to_xyxy(bounding_boxes, inplace)

torchvision/tv_tensors/_bounding_boxes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BoundingBoxFormat(Enum):
2020
* ``XYWHR``: rotated boxes represented via corner, width and height, x1, y1
2121
being top left, w, h being width and height. r is rotation angle in
2222
degrees.
23-
* ``CXCYWHR``: jrotated boxes represented via centre, width and height, cx,
23+
* ``CXCYWHR``: rotated boxes represented via centre, width and height, cx,
2424
cy being center of box, w, h being width and height. r is rotation angle
2525
in degrees.
2626
* ``XYXYXYXY``: rotated boxes represented via corners, x1, y1 being top

0 commit comments

Comments
 (0)