A Python script that converts CVAT polygons / segmentation data / annotations in COCO JSON format to YOLOv8 compatible format for object detection and segmentation tasks.
- Converts COCO JSON format to YOLOv8 annotation format
- Handles polygon segmentation data
- Normalizes coordinates according to YOLOv8 requirements
- Preserves category IDs and mappings
- Processes multiple images and annotations
- Python 3.6 or higher
- Access to CVAT-exported annotations in COCO JSON format
Clone this repository:
git clone https://github.com/SakibAhmedShuva/cvat-yolov8-converter.git
cd cvat-yolov8-converter
- Update the
coco_file
path in the script to point to your COCO JSON file:
coco_file = '/path/to/your/instances_default.json'
- Run the script:
python cvat_to_yolov8.py
The script will:
- Create a directory named
yolov8_annotations
(if it doesn't exist) - Process each annotation in the COCO JSON file
- Generate corresponding YOLOv8 format text files for each image
For each image in the COCO dataset, the script creates a text file with the same name as the image (but with .txt extension) containing the annotations in YOLOv8 format:
<category_id> <x1> <y1> <x2> <y2> ... <xn> <yn>
Where:
category_id
is the class IDx1, y1, x2, y2, ..., xn, yn
are normalized polygon coordinates
Input (COCO format):
{
"images": [{"id": 1, "width": 800, "height": 600, "file_name": "image1.jpg"}],
"annotations": [
{
"image_id": 1,
"category_id": 0,
"segmentation": [[100, 100, 200, 100, 200, 200, 100, 200]]
}
]
}
Output (YOLOv8 format in image1.txt):
0 0.125 0.166667 0.25 0.166667 0.25 0.333333 0.125 0.333333
- Currently only supports polygon segmentation
- Assumes one segmentation per annotation
- Does not handle complex, nested polygons
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
https://github.com/SakibAhmedShuva
Project Link: https://github.com/SakibAhmedShuva/CVAT-Segmentation-to-YOLOv8-Converter