Skip to content

Annotations

plakakia.annotations

Readers and writers for bounding-box annotation formats.

Supported inputs: YOLO (.txt), Pascal VOC (.xml) and COCO (.json). Supported outputs: YOLO and Pascal VOC.

All readers return :class:Annotations, a pair of NumPy arrays holding integer pixel boxes (x1, y1, x2, y2) and integer class ids.

Annotations dataclass

Annotations(boxes: NDArray[int64], labels: NDArray[int64])

Bounding boxes of one image.

Attributes:

Name Type Description
boxes NDArray[int64]

(N, 4) int64 array of (x1, y1, x2, y2) pixel coordinates.

labels NDArray[int64]

(N,) int64 array of class ids.

empty classmethod

empty() -> Annotations

Annotations with no boxes.

shifted

shifted(dx: int, dy: int) -> Annotations

Return a copy with all boxes translated by (dx, dy).

read_yolo

read_yolo(
    path: PathLike, image_size: tuple[int, int]
) -> Annotations

Read a YOLO .txt label file.

Each line is class cx cy w h with coordinates normalised to [0, 1]. Lines with more values are treated as YOLO segmentation polygons (class x1 y1 x2 y2 ...) and converted to their bounding box.

Parameters:

Name Type Description Default
path PathLike

Label file.

required
image_size tuple[int, int]

(height, width) of the image the labels refer to.

required

read_pascal_voc

read_pascal_voc(
    path: PathLike,
    label_map: Mapping[str, int] | None = None,
) -> Annotations

Read a Pascal VOC .xml file.

Parameters:

Name Type Description Default
path PathLike

XML file.

required
label_map Mapping[str, int] | None

Mapping from class names to integer ids. Names that are already integers do not need an entry.

None

read_coco

read_coco(path: PathLike) -> dict[str, Annotations]

Read a COCO .json file.

Returns:

Type Description
dict[str, Annotations]

Mapping from image file name (Path(file_name).name) to its annotations.

dict[str, Annotations]

Images without annotations map to empty :class:Annotations.

write_yolo

write_yolo(
    path: PathLike,
    annotations: Annotations,
    image_size: tuple[int, int],
) -> None

Write boxes (in pixel coordinates of an image of (height, width)) as YOLO.

write_pascal_voc

write_pascal_voc(
    path: PathLike,
    annotations: Annotations,
    image_shape: tuple[int, ...],
    image_filename: str,
    label_names: Mapping[int, str] | None = None,
) -> None

Write boxes as a Pascal VOC .xml file.

Parameters:

Name Type Description Default
path PathLike

Output XML file.

required
annotations Annotations

Boxes in pixel coordinates of the image.

required
image_shape tuple[int, ...]

Shape of the image ((H, W) or (H, W, C)).

required
image_filename str

Value for the <filename> tag.

required
label_names Mapping[int, str] | None

Optional mapping from class ids back to names.

None