Effortless COCO annotation to YOLO segmentation
COCO instance segmentation labels to YOLO segmentation annotation.
TL;DR
Github link: http://github.com/z00bean/coco2yolo-seg
Requires only two essential Python libraries — just import json
and import os
— no need for additional dependencies.
1. Add COCO JSON path: Specify the COCO annotation JSON file path in the file COCO2YOLO-seg.py
(line 64) and specify the desired output folder name in the Python file.
2. Run the conversion script: python COCO2YOLO-seg.py
YOLO Segmentation Data Format
The YOLO segmentation data format is designed to streamline the training of YOLO segmentation models; however, many ML and deep learning practitioners have faced difficulty in converting existing COCO annotations to YOLO segmentation format [1][2]. In this article, I provide a detailed explanation of the YOLO segmentation data format, and offer a solution to a common hurdle faced by researchers and developers.
Here’s an in-depth explanation of the format:
- One text file per image: Each image in the dataset has a corresponding text file. The text file shares the same name as the image file and has a “.txt” extension.
- One row per object: Every row in the text file corresponds to one object instance present in the image.
- Object information per row: Each row contains critical information about the object instance:
- Object class index: An integer representing the class of the object (e.g., 0 for a person, 1 for a car, etc.).
- Object bounding coordinates: The bounding coordinates around the mask area, normalized to be between 0 and 1.
The format for a single row in the segmentation dataset file is as follows:
<class-index> <x1> <y1> <x2> <y2> ... <xn> <yn>
Here, <class-index> is the object class index, and <x1> <y1> <x2> <y2> … <xn> <yn> are the normalized coordinates of the mask area. (More info here.)
Key Steps:
- Load COCO JSON Data: The script begins by loading the COCO JSON data, which includes information about images and annotations.
- Create Output Folder: An output folder (named “labels” by default), is created to store YOLO segmentation annotations.
- Extract Annotations: Annotations are extracted from the COCO JSON data, including image_id, category_id, segmentation, and bbox.
- Calculate Normalized Coordinates: The script calculates the normalized center coordinates (
x_center, y_center
) and normalized width/height (bbox_width, bbox_height
).# Calculate the normalized center coordinates and width/height x_center = (bbox[0] + bbox[2] / 2) / image_width
y_center = (bbox[1] + bbox[3] / 2) / image_height
bbox_width = bbox[2] / image_width
bbox_height = bbox[3] / image_height - Convert to YOLO Segmentation Format: COCO segmentation coordinates are converted to YOLO segmentation format using a specific calculation and formatting.
# Convert COCO segmentation to YOLO segmentation format
yolo_segmentation = [f”{(x) / image_width:.5f} {(y) / image_height:.5f}” for x, y in zip(segmentation[0][::2], segmentation[0][1::2])]
yolo_segmentation = ‘ ‘.join(yolo_segmentation) - Generate YOLO Annotation Line: The YOLO annotation line is created, combining the category_id and YOLO segmentation coordinates.
- Save YOLO Annotation: The YOLO annotation line is saved in a text file named after the corresponding image in the “labels” folder.
In summary, this script provides a convenient way to convert COCO segmentation annotations into the YOLO segmentation format, simplifying the preparation of datasets for YOLO segmentation model training.