Overview

Export annotations to COCO format

Unofficial preview. Docsbook assembled this page from the public wkentaro/labelme README.md and examples/ directory. It is not affiliated with, endorsed by, or maintained by the Labelme project. The official documentation lives at labelme.io/docs.

Labelme exports to COCO instance-segmentation format through labelme2coco.py, shipped in examples/instance_segmentation. One command turns a directory of per-image JSON files into the single annotations.json that COCO loaders expect.

Run the export#

./labelme2coco.py data_annotated data_dataset_coco --labels labels.txt

Three arguments, in this order: the directory of annotated images, the output directory to create, and the label list that fixes the category set.

What the export produces#

Path What it holds
data_dataset_coco/JPEGImages The source images, copied
data_dataset_coco/annotations.json One COCO file covering every image and every instance

This is the shape difference from Pascal VOC, which spreads its output across several directories of per-image files. COCO puts everything in one document, so a loader is pointed at one path and an image root.

Prepare the annotations first#

COCO instance format carries object identity, so the input needs to carry it too. Annotate with a fixed label list and validation before exporting:

labelme data_annotated --labels labels.txt --validate-label exact

Two things to settle before the export rather than after:

  • Category names come from labels.txt. A label that reached the JSON but is missing from the list has no category to map onto. --validate-label exact prevents that at annotation time.
  • Objects are separated by group_id. Two polygons over one partly-hidden object must share a group_id to export as one instance instead of two. The core concepts page explains the field.

Updated

Was this page helpful?