tlc.export.exporters.coco¶

Exporter for the COCO format.

Module Contents¶

Classes¶

Class

Description

CocoExporter

Exporter for the COCO format.

API¶

class CocoExporter¶

Bases: tlc.export.exporter.SerializingExporter

Exporter for the COCO format.

Tables which are originally instances of the TableFromCoco class will be compatible with this exporter.

can_export(
table: Table,
output_url: Url,
) bool¶

Check if the table can be exported to the COCO format.

Can not be 100% accurate, as we don’t know if the user has supplied additional arguments, such as annotation_column_name, or image_column_name.

priority = 3¶
serialize(
table: Table,
output_url: Url,
weight_threshold: float = 0.0,
image_folder: Url | str = '',
absolute_image_paths: bool = False,
annotation_column_name: str | None = None,
image_column_name: str | None = None,
per_image_extras: Sequence[str] | None = None,
indent: int = 4,
segmentation_format: Literal[polygon, rle, coco] = 'polygon',
**kwargs: Any,
) str¶

Serialize a table to the COCO format.

Default behavior is to write a COCO file with image paths relative to the (output) annotations file. Written paths can be further configured with the absolute_image_paths and image_folder argument.

Note that for a coco file to be valid, the image paths should be absolute or relative w.r.t. the annotations file itself.

Parameters:
  • table – The table to serialize

  • output_url – The output URL

  • weight_threshold – The weight threshold

  • image_folder – Make image paths relative to a specific folder. Note that this may produce an annotations file that needs special handling. This option is mutually exclusive with absolute_image_paths.

  • absolute_image_paths – Make image paths absolute. If this is set to True, the image_folder cannot be set.

  • annotation_column_name – Optional column name to use for annotations instead of the defaults (bbs, segmentations, or keypoints_2d). The content of the column determines the annotation mode (bounding boxes, segmentations, or keypoints) based on its structure.

  • image_column_name – Optional column name to use for image URLs. Defaults to image.

  • per_image_extras – Image-level column names to include in the COCO image dicts. These should correspond to top-level table columns (e.g. columns originally imported via the importer’s per_image_extras parameter). When None, no extra image fields are written.

  • indent – The number of spaces to use for indentation in the output.

  • segmentation_format – How to write instance segmentation masks. "polygon" (default) traces each mask into a COCO multi-polygon segmentation (one polygon per connected component); this is the most widely-supported COCO instance-segmentation representation, but it is lossy (rasterized boundary) and cannot represent holes natively. "rle" writes the stored run-length encoding directly — lossless, preserving holes and disconnected components exactly; prefer it for masks with holes or disconnected parts, at the cost of narrower support in downstream tooling. "coco" follows the COCO convention: polygons for regular instances and RLE for crowd regions (iscrowd=1), which matches how standard COCO datasets store segmentations. Ignored for non-segmentation exports.

  • **kwargs – Any additional arguments

Returns:

The serialized table

supported_format = coco¶