|
1 | 1 |
|
2 | | -__version__ = '0.1' |
| 2 | +__version__ = '0.2' |
| 3 | + |
| 4 | +import numpy as np |
3 | 5 |
|
4 | 6 | from image_dataset_viz.dataset_exporter import DatasetExporter, render_datapoint |
| 7 | + |
| 8 | + |
| 9 | +def bbox_to_points(bbox_xyxy): |
| 10 | + """Helper method to convert bounding box as list/tuple of [x1, y1, x2, y2] into points `ndarray` of shape (N, 2) |
| 11 | +
|
| 12 | + Args: |
| 13 | + bbox_xyxy (list or tuple): bounding box as list/tuple of [x1, y1, x2, y2] |
| 14 | +
|
| 15 | + Returns: |
| 16 | + ndarray of points |
| 17 | +
|
| 18 | + """ |
| 19 | + assert isinstance(bbox_xyxy, (list, tuple)) and len(bbox_xyxy) == 4, \ |
| 20 | + "Argument bbox_xyxy should be a list/tuple of [x1, y1, x2, y2]" |
| 21 | + |
| 22 | + return np.array([ |
| 23 | + [bbox_xyxy[0], bbox_xyxy[1]], |
| 24 | + [bbox_xyxy[2], bbox_xyxy[1]], |
| 25 | + [bbox_xyxy[2], bbox_xyxy[3]], |
| 26 | + [bbox_xyxy[0], bbox_xyxy[3]], |
| 27 | + ]) |
| 28 | + |
| 29 | + |
| 30 | +def xywh_to_xyxy(xywh): |
| 31 | + """Helper method to transform bounding box of type [x1, y1, width, height] into [x1, y1, x2, y2] |
| 32 | +
|
| 33 | + Args: |
| 34 | + xywh (list or tuple): bounding box of type [x1, y1, width, height] |
| 35 | +
|
| 36 | + Returns: |
| 37 | + list [x1, y1, x2, y2] |
| 38 | +
|
| 39 | + """ |
| 40 | + assert isinstance(xywh, (list, tuple)) and len(xywh) == 4, \ |
| 41 | + "Argument xywh should be a list/tuple of [x1, y1, width, height]" |
| 42 | + x1, y1 = xywh[0], xywh[1] |
| 43 | + x2 = x1 + max(0, xywh[2] - 1) |
| 44 | + y2 = y1 + max(0, xywh[3] - 1) |
| 45 | + return [x1, y1, x2, y2] |
| 46 | + |
| 47 | + |
| 48 | +def xyxy_to_xywh(xyxy): |
| 49 | + """Helper method to transform bounding box of type [x1, y1, x2, y2] into [x1, y1, width, height] |
| 50 | +
|
| 51 | + Args: |
| 52 | + xyxy (list or tuple): bounding box of type [x1, y1, x2, y2] |
| 53 | +
|
| 54 | + Returns: |
| 55 | + list [x1, y1, width, height] |
| 56 | +
|
| 57 | + """ |
| 58 | + assert isinstance(xyxy, (list, tuple)) and len(xyxy) == 4, \ |
| 59 | + "Argument xyxy should be a list/tuple of [x1, y1, x2, y2]" |
| 60 | + x1, y1 = xyxy[0], xyxy[1] |
| 61 | + w = xyxy[2] - x1 + 1 |
| 62 | + h = xyxy[3] - y1 + 1 |
| 63 | + return [x1, y1, w, h] |
0 commit comments