def horizontal_flip( input: features.BoundingBox) -> features.BoundingBox: x, y, w, h = input.convert("xywh").to_parts() x = input.image_size[1] - (x + w) return features.BoundingBox.from_parts(x, y, w, h, like=input, format="xywh")
def bounding_box(input: BoundingBox, *, size: Tuple[int, int], **_: Any) -> BoundingBox: old_height, old_width = input.image_size new_height, new_width = size height_scale = new_height / old_height width_scale = new_width / old_width old_x1, old_y1, old_x2, old_y2 = input.convert("xyxy").to_parts() new_x1 = old_x1 * width_scale new_y1 = old_y1 * height_scale new_x2 = old_x2 * width_scale new_y2 = old_y2 * height_scale return BoundingBox.from_parts( new_x1, new_y1, new_x2, new_y2, like=input, format="xyxy", image_size=size ).convert(input.format)
def resize(input: features.BoundingBox, size: torch.Tensor) -> features.BoundingBox: old_height, old_width = input.image_size new_height, new_width = size height_scale = new_height / old_height width_scale = new_width / old_width old_x1, old_y1, old_x2, old_y2 = input.convert("xyxy").to_parts() new_x1 = old_x1 * width_scale new_y1 = old_y1 * height_scale new_x2 = old_x2 * width_scale new_y2 = old_y2 * height_scale return features.BoundingBox.from_parts(new_x1, new_y1, new_x2, new_y2, like=input, format="xyxy", image_size=tuple( size.tolist()))
def image(input: Image, *, crop_box: BoundingBox) -> Image: # FIXME: pad input in case it is smaller than crop_box x1, y1, x2, y2 = crop_box.convert("xyxy").to_parts() return Image(input[..., y1 : y2 + 1, x1 : x2 + 1], like=input) # type: ignore[misc]
def __init__(self, crop_box: BoundingBox) -> None: super().__init__() self.crop_box = crop_box.convert("xyxy")
def bounding_box(input: BoundingBox) -> BoundingBox: x, y, w, h = input.convert("xywh").to_parts() x = input.image_size[1] - (x + w) return BoundingBox.from_parts(x, y, w, h, like=input, format="xywh").convert(input.format)