Skip to content

box

data.dataclass.box

boxes_datas module-attribute

boxes_datas = {'xyxy': [[1, 2, 4, 6], [22, 28, 36, 62], [13, 39, 14, 78], [42, 24, 81, 46]], 'xywh': [[1, 2, 3, 4], [22, 28, 14, 34], [13, 39, 1, 39], [42, 24, 39, 22]], 'ccwh': [[2.5, 4, 3, 4], [29, 45, 14, 34], [13.5, 58.5, 1, 39], [61.5, 35, 39, 22]]}

boxes module-attribute

boxes = test_init_box(box_data, coordinate)

NumpyBoxes

Bases: ndarray

convert_coordinate

convert_coordinate(boxes: Union[ndarray, Tensor], source_coordinate: str, target_coordinate: str) -> Union[ndarray, Tensor]
Source code in SaigeToolkit/data/dataclass/box.py
def convert_coordinate(
    boxes: Union[np.ndarray, torch.Tensor],  # shape: (num_bboxes, 4), dtype: float
    source_coordinate: str,  # coordinate type of input boxes ["xyxy", "xywh", "ccwh"]
    target_coordinate: str,  # coordinate type want to convert ["xyxy", "xywh", "ccwh"]
) -> Union[np.ndarray, torch.Tensor]:
    def _to_xywh(boxes: NumpyBoxes, coordinate: str) -> NumpyBoxes:
        """some coordinate -> (left, top, width, height)"""
        if coordinate == "xywh":
            pass
        elif coordinate == "xyxy":
            boxes[:, [2, 3]] = boxes[:, [2, 3]] - boxes[:, [0, 1]]
        elif coordinate == "ccwh":
            boxes[:, [0, 1]] = boxes[:, [0, 1]] - (boxes[:, [2, 3]] / 2)
        else:
            raise NotImplementedError

        return boxes

    def _from_xywh(boxes: NumpyBoxes, coordinate: str) -> NumpyBoxes:
        """(left, top, width, height) -> some coordinate"""
        if coordinate == "xywh":
            pass
        elif coordinate == "xyxy":
            boxes[:, [2, 3]] = boxes[:, [2, 3]] + boxes[:, [0, 1]]
        elif coordinate == "ccwh":
            boxes[:, [0, 1]] = boxes[:, [0, 1]] + (boxes[:, [2, 3]] / 2)
        else:
            raise NotImplementedError

        return boxes

    if isinstance(boxes, np.ndarray):
        new_boxes = boxes.copy()
    elif isinstance(boxes, torch.Tensor):
        new_boxes = boxes.clone()

    if source_coordinate != target_coordinate:  # if you want to change the coordinate
        new_boxes = _to_xywh(new_boxes, source_coordinate)  # source coordinate -> xywh
        new_boxes = _from_xywh(new_boxes, target_coordinate)  # xywh -> target coordinate

    return new_boxes

test_init_box

test_init_box(box_data, coordinate) -> NumpyBoxes
Source code in SaigeToolkit/data/dataclass/box.py
def test_init_box(box_data, coordinate) -> NumpyBoxes:
    boxes = NumpyBoxes(box_data, coordinate)

    assert boxes.coordinate == coordinate
    assert boxes.dtype == np.float32
    assert boxes.tolist() == box_data

    return boxes

test_base_function

test_base_function(boxes: NumpyBoxes)
Source code in SaigeToolkit/data/dataclass/box.py
def test_base_function(boxes: NumpyBoxes):
    boxes = boxes + 3
    boxes = boxes * 3
    boxes = boxes - 3
    boxes = boxes / 3
    boxes = np.concatenate([boxes] * 4, axis=0)
    boxes = np.split(boxes, 4, axis=0)[0]
    boxes = boxes.astype(np.int32)

    assert boxes.dtype == np.int32

test_convert_coordinate

test_convert_coordinate(boxes: NumpyBoxes, coordinate, answer)
Source code in SaigeToolkit/data/dataclass/box.py
def test_convert_coordinate(boxes: NumpyBoxes, coordinate, answer):
    boxes = boxes.convert_coordinate(coordinate)

    assert boxes.coordinate == coordinate
    assert boxes.dtype == np.float32
    assert boxes.tolist() == answer

test_to_other_data

test_to_other_data(boxes: NumpyBoxes, answer)
Source code in SaigeToolkit/data/dataclass/box.py
def test_to_other_data(boxes: NumpyBoxes, answer):
    new_boxes = boxes.to_tensor()

    assert new_boxes.tolist() == answer