Flip bboxes horizontally or vertically. Args: bboxes (Tensor): Shape (..., 4*k) img_shape (tuple): Image shape. direction (str): Flip direction, options are "horizontal", "vertical", "diagonal". Default: "horizontal" Returns: Tensor: Flipped bbox
(bboxes, img_shape, direction='horizontal')
| 20 | |
| 21 | |
| 22 | def bbox_flip(bboxes, img_shape, direction='horizontal'): |
| 23 | """Flip bboxes horizontally or vertically. |
| 24 | |
| 25 | Args: |
| 26 | bboxes (Tensor): Shape (..., 4*k) |
| 27 | img_shape (tuple): Image shape. |
| 28 | direction (str): Flip direction, options are "horizontal", "vertical", |
| 29 | "diagonal". Default: "horizontal" |
| 30 | |
| 31 | Returns: |
| 32 | Tensor: Flipped bboxes. |
| 33 | """ |
| 34 | assert bboxes.shape[-1] % 4 == 0 |
| 35 | assert direction in ['horizontal', 'vertical', 'diagonal'] |
| 36 | flipped = bboxes.clone() |
| 37 | if direction == 'horizontal': |
| 38 | flipped[..., 0::4] = img_shape[1] - bboxes[..., 2::4] |
| 39 | flipped[..., 2::4] = img_shape[1] - bboxes[..., 0::4] |
| 40 | elif direction == 'vertical': |
| 41 | flipped[..., 1::4] = img_shape[0] - bboxes[..., 3::4] |
| 42 | flipped[..., 3::4] = img_shape[0] - bboxes[..., 1::4] |
| 43 | else: |
| 44 | flipped[..., 0::4] = img_shape[1] - bboxes[..., 2::4] |
| 45 | flipped[..., 1::4] = img_shape[0] - bboxes[..., 3::4] |
| 46 | flipped[..., 2::4] = img_shape[1] - bboxes[..., 0::4] |
| 47 | flipped[..., 3::4] = img_shape[0] - bboxes[..., 1::4] |
| 48 | return flipped |
| 49 | |
| 50 | |
| 51 | def bbox_mapping(bboxes, |
no test coverage detected