LeapImageWithBBox

code_loader.contract.visualizer_classes.LeapImageWithBBox

Used to visualize an image overlayed with bounding boxes

import numpy.typing as npt
from code_loader.contract.enums import LeapDataType

@dataclass
class BoundingBox:
    x: float
    y: float
    width: float
    height: float
    confidence: float
    label: str
    rotation: float = 0.0
    metadata: Optional[Dict[str, Union[str, int, float]]] = None

@dataclass
class LeapImageWithBBox:
    data: Union[npt.NDArray[np.float32], npt.NDArray[np.uint8]]
    bounding_boxes: List[BoundingBox]
    type: LeapDataType = LeapDataType.ImageWithBBox
Args

data

an np.ndarray uint8/float32 representation of the image. The expected image format is [H,W,1] OR [H,W,3] and is expected to be in [0,255].

bounding_boxes

A list of bounding box. Each bounding_box has the following attributes:

  • x - x center of the bounding box. Expected to be between [0,1]

  • y - y center of the bounding box. Expected to be between [0,1]

  • width - width of the bounding box. Expected to be between [0,1]

  • height - height of the bounding box. Expected to be between [0,1]

  • confidence - the confidence of the prediction. Expected to be between [0,1]. Ground Truth should be set to 1.

  • label - a str label of the prediction

  • rotation - a float between [0,360] that represents the degree of roation. Defaults to 0.

  • metadata (optional) - a metadata on the bounding box. A dictionary of property names (str) to their values (str/int/float)

Examples

Basic Usage

from code_loader.contract.visualizer_classes import LeapImageWithBBox
import numpy as np
from code_loader.contract.enums import LeapDataType
...
@tensorleap_custom_visualizer("bb_gt_decoder", LeapDataType.ImageWithBBox)
def gt_bb_decoder(image: np.ndarray, bb_gt: np.ndarray) -> LeapImageWithBBox:
    bbox = [BoundingBox(x=bbx[0], y=bbx[1], width=bbx[2], height=bbx[3], confidence=1, label=all_clss.get(int(bbx[4]) if not np.isnan(bbx[4]) else -1, 'Unknown Class')) for bbx in bb_gt.squeeze(0)]
    image = image.squeeze(0)
    return LeapImageWithBBox(data=image, bounding_boxes=bbox)

Last updated

Was this helpful?