module documentation

Undocumented

Class ColorMap A mapping that assigns distinct RGB colors to hashable labels.
Function add_augmentation_footer Append the applied augmentations as a footer below the image.
Function append_text_block Append a multi-line text block below the image.
Function concat_images Concatenate images into a single labeled image.
Function create_text_image Create an image with centered text.
Function distinct_color_generator Generate distinct RGB colors using the golden ratio.
Function draw_bbox_label Draw the class name label at the top-left corner of the bounding box.
Function draw_cross Draw a cross on the image.
Function draw_dashed_rectangle Draw a dashed rectangle on the image.
Function draw_keypoint_label Draw a text label next to a keypoint on the image.
Function get_contrast_color Return a contrasting color for the given RGB color.
Function hsv_to_rgb Convert an HSV color to RGB.
Function resolve_color Resolve a color to RGB.
Function rgb_to_hsv Convert an RGB color to HSV.
Function str_to_rgb Convert a string to its deterministic RGB color.
Function visualize Visualize labels on the image.
Function wrap_text Wrap text into lines that fit within the given width.
def add_augmentation_footer(image: np.ndarray, augmentations: list[str]) -> np.ndarray:

Append the applied augmentations as a footer below the image.

def append_text_block(image: np.ndarray, lines: list[str], font_scale: float, bg_color: Color = (245, 245, 245), text_color: Color = (32, 32, 32)) -> np.ndarray:

Append a multi-line text block below the image.

def concat_images(image_dict: dict[str, np.ndarray], padding: int = 10, label_height: int = 30) -> np.ndarray:

Concatenate images into a single labeled image.

It will attempt to create a square grid of images.

Parameters
image_dict:dict[str, np.ndarray]Mapping of image names to images.
padding:intPadding between images.
label_height:intHeight of each label.
Returns
np.ndarrayThe concatenated image.
def create_text_image(text: str, width: int, height: int, font_size: float = 0.7, bg_color: Color = 255, text_color: Color = 0) -> np.ndarray:

Create an image with centered text.

Parameters
text:strText to display.
width:intImage width.
height:intImage height.
font_size:floatFont size for the text.
bg_color:ColorBackground color.
text_color:ColorText color.
Returns
np.ndarrayThe generated image.
def distinct_color_generator(stop: int = -1) -> Generator[RGB, None, None]:

Generate distinct RGB colors using the golden ratio.

This generator produces a sequence of distinct colors in RGB format. The colors are generated by incrementing the hue by the golden ratio and keeping saturation and value fixed. This ensures a wide distribution of visually distinct colors.

Examples

>>> colors = list(distinct_color_generator(stop=2))
>>> len(colors)
2
>>> all(len(color) == 3 for color in colors)
True
Parameters
stop:intoptional maximum number of colors to generate. If set to  − 1, the generator continues indefinitely.
Returns
Generator[RGB, None, None]Undocumented
Yields
The next RGB color with each component in the range [0, 255].
def draw_bbox_label(image: np.ndarray, class_name: str, box: np.ndarray, color: tuple[int, int, int], font_scale: float):

Draw the class name label at the top-left corner of the bounding box.

Parameters
image:np.ndarrayImage to draw on.
class_name:strName of the class.
box:np.ndarrayBounding box coordinates in [class_id, x1, y1, x2, y2] format, where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.
color:tuple[int, int, int]Label color.
font_scale:floatFont scale.
def draw_cross(img: np.ndarray, center: tuple[int, int], size: int = 5, color: Color = 0, thickness: int = 1):

Draw a cross on the image.

Parameters
img:np.ndarrayImage to draw on.
center:tuple[int, int]Center of the cross.
size:intSize of the cross.
color:ColorCross color.
thickness:intLine thickness.
def draw_dashed_rectangle(image: np.ndarray, pt1: tuple[int, int], pt2: tuple[int, int], color: Color, thickness: int = 1, dash_length: int = 10):

Draw a dashed rectangle on the image.

Parameters
image:np.ndarrayImage to draw on.
pt1:tuple[int, int]Top-left corner of the rectangle.
pt2:tuple[int, int]Bottom-right corner of the rectangle.
color:ColorRectangle color.
thickness:intLine thickness.
dash_length:intLength of each dash.
def draw_keypoint_label(image: np.ndarray, text: str, point: tuple[int, int], size: int, color: tuple[int, int, int], font_scale: float):

Draw a text label next to a keypoint on the image.

Parameters
image:np.ndarrayImage to draw on.
text:strText to draw.
point:tuple[int, int]Keypoint coordinates.
size:intKeypoint size.
color:tuple[int, int, int]Text color.
font_scale:floatFont scale.
def get_contrast_color(color: Color) -> RGB:

Return a contrasting color for the given RGB color.

Examples

>>> get_contrast_color((255, 0, 0))
(0, 255, 255)
>>> get_contrast_color(0)
(0, 0, 0)
Parameters
color:ColorColor to contrast.
Returns
RGBThe contrasting color.
def hsv_to_rgb(color: HSV) -> RGB:

Convert an HSV color to RGB.

Examples

>>> hsv_to_rgb((0, 1, 1))
(255, 0, 0)
>>> hsv_to_rgb((120, 1, 1))
(0, 255, 0)
Parameters
color:HSVHSV color to convert.
Returns
RGBThe converted RGB color.
def resolve_color(color: Color) -> RGB:

Resolve a color to RGB.

Examples

>>> resolve_color(12)
(12, 12, 12)
>>> resolve_color((1, 2, 3))
(1, 2, 3)
>>> resolve_color(300)
Traceback (most recent call last):
...
ValueError: Color value 300 is out of range [0, 255]
Parameters
color:ColorColor to resolve.
Returns
RGBThe resolved RGB color.
Raises
ValueErrorIf an integer channel value is outside [0, 255].
def rgb_to_hsv(color: Color) -> HSV:

Convert an RGB color to HSV.

Examples

>>> tuple(round(v, 3) for v in rgb_to_hsv((255, 0, 0)))
(0.0, 1.0, 1.0)
>>> tuple(round(v, 3) for v in rgb_to_hsv(128))
(0.0, 0.0, 0.502)
Parameters
color:ColorColor to convert.
Returns
HSVThe converted HSV color.
def str_to_rgb(string: str) -> RGB:

Convert a string to its deterministic RGB color.

Examples

>>> str_to_rgb("car") == str_to_rgb("car")
True
>>> len(str_to_rgb("car"))
3
Parameters
string:strString to convert.
Returns
RGBDeterministic RGB color.
def visualize(image: np.ndarray, source_name: str, labels: Labels, classes: dict[str, dict[str, int]], blend_all: bool = False, categorical_encodings: dict[str, dict[str, int]] | None = None) -> np.ndarray:

Visualize labels on the image.

Parameters
image:np.ndarrayImage to visualize.
source_name:strName of the image source.
labels:LabelsLabels to visualize.
classes:dict[str, dict[str, int]]Mapping from task names to class ID mappings.
blend_all:boolWhether to blend all labels into a single image. This mixes labels that belong to different tasks.
categorical_encodings:dict[str, dict[str, int]] | Noneoptional mapping for categorical metadata tasks. Keys are full task identifiers such as "task_name/metadata/key" and values map string labels to encoded integers.
Returns
np.ndarrayThe visualized image.
def wrap_text(text: str, max_width: int, font_scale: float, thickness: int = 1) -> list[str]:

Wrap text into lines that fit within the given width.