A class for handling bounding boxes. The class supports various bounding box formats like 'xyxy', 'xywh', and 'ltwh'. Bounding box data should be provided in numpy arrays. Attributes: bboxes (numpy.ndarray): The bounding boxes stored in a 2D numpy array. format (st
| 32 | |
| 33 | |
| 34 | class Bboxes: |
| 35 | """ |
| 36 | A class for handling bounding boxes. |
| 37 | |
| 38 | The class supports various bounding box formats like 'xyxy', 'xywh', and 'ltwh'. |
| 39 | Bounding box data should be provided in numpy arrays. |
| 40 | |
| 41 | Attributes: |
| 42 | bboxes (numpy.ndarray): The bounding boxes stored in a 2D numpy array. |
| 43 | format (str): The format of the bounding boxes ('xyxy', 'xywh', or 'ltwh'). |
| 44 | |
| 45 | Note: |
| 46 | This class does not handle normalization or denormalization of bounding boxes. |
| 47 | """ |
| 48 | |
| 49 | def __init__(self, bboxes, format='xyxy') -> None: |
| 50 | """Initializes the Bboxes class with bounding box data in a specified format.""" |
| 51 | assert format in _formats, f'Invalid bounding box format: {format}, format must be one of {_formats}' |
| 52 | bboxes = bboxes[None, :] if bboxes.ndim == 1 else bboxes |
| 53 | assert bboxes.ndim == 2 |
| 54 | assert bboxes.shape[1] == 4 |
| 55 | self.bboxes = bboxes |
| 56 | self.format = format |
| 57 | # self.normalized = normalized |
| 58 | |
| 59 | def convert(self, format): |
| 60 | """Converts bounding box format from one type to another.""" |
| 61 | assert format in _formats, f'Invalid bounding box format: {format}, format must be one of {_formats}' |
| 62 | if self.format == format: |
| 63 | return |
| 64 | elif self.format == 'xyxy': |
| 65 | func = xyxy2xywh if format == 'xywh' else xyxy2ltwh |
| 66 | elif self.format == 'xywh': |
| 67 | func = xywh2xyxy if format == 'xyxy' else xywh2ltwh |
| 68 | else: |
| 69 | func = ltwh2xyxy if format == 'xyxy' else ltwh2xywh |
| 70 | self.bboxes = func(self.bboxes) |
| 71 | self.format = format |
| 72 | |
| 73 | def areas(self): |
| 74 | """Return box areas.""" |
| 75 | self.convert('xyxy') |
| 76 | return (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1]) |
| 77 | |
| 78 | # def denormalize(self, w, h): |
| 79 | # if not self.normalized: |
| 80 | # return |
| 81 | # assert (self.bboxes <= 1.0).all() |
| 82 | # self.bboxes[:, 0::2] *= w |
| 83 | # self.bboxes[:, 1::2] *= h |
| 84 | # self.normalized = False |
| 85 | # |
| 86 | # def normalize(self, w, h): |
| 87 | # if self.normalized: |
| 88 | # return |
| 89 | # assert (self.bboxes > 1.0).any() |
| 90 | # self.bboxes[:, 0::2] /= w |
| 91 | # self.bboxes[:, 1::2] /= h |
no outgoing calls
no test coverage detected