Package: labelformat
Labelformat - Label Conversion, SimplifiedAn open-source tool to seamlessly convert between popular computer vision label formats.Why LabelformatPopular label formats are sparsely documented and store different
information. Understanding them and dealing with the differences is tedious
and time-consuming. Labelformat aims to solve this pain.Supported Tasks and Formatsobject-detectionCOCOKITTILightlyPascalVOCYOLOv5YOLOv6YOLOv7YOLOv8Labelbox(input only)instance-segmentationCOCOYOLOv8FeaturesSupport for common dataset label formats (more coming soon)Support for common tool formats (more coming soon)Minimal dependencies, targets python 3.7 or higherMemory concious - datasets are processed file-by-file instead of loading everything
in memory (when possible)TypedTested with round trip tests to ensure consistencyMIT licenseSupported PlatformsThis package is compatible with the following platforms:WindowsmacOSLinuxNoteLabelformat is a young project, contributions and bug reports are welcome. Please seeContributingsection below.InstallationpipinstalllabelformatUsageCLIExamplesConvert instance segmentation labels from COCO to YOLOv8:labelformatconvert\--taskinstance-segmentation\--input-formatcoco\--input-filecoco-labels/train.json\--output-formatyolov8\--output-fileyolo-labels/data.yaml\--output-splittrainConvert object detection labels from KITTI to PascalVOC:labelformatconvert\--taskobject-detection\--input-formatkitti\--input-folderkitti-labels/labels\--category-namescat,dog,fish\--images-rel-path../images\--output-formatpascalvoc\--output-folderpascalvoc-labelsConvert object detection labels from Labelbox to Lightly:labelformatconvert\--taskobject-detection\--input-formatlabelbox\--input-filelabelbox-labels/export-result.ndjson\--category-namescat,dog,fish\--output-formatlightly\--output-folderlightly-labels/annotation-taskCommand ArgumentsList the available tasks with:$labelformatconvert--helpusage: labelformat convert [-h] --task{instance-segmentation,object-detection}Convert labels from one format to another.optional arguments:-h, --help--task {instance-segmentation,object-detection}List the available formats for a given task with:$labelformatconvert--taskobject-detection--helpusage: labelformat convert [-h] --task{instance-segmentation,object-detection}--input-format{coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}--output-format{coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}Convert labels from one format to another.optional arguments:-h, --help--task {instance-segmentation,object-detection}--input-format {coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}Input format--output-format {coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}Output formatSpecify the input and output format to get required options for specific formats:$labelformatconvert\--taskobject-detection\--input-formatcoco\--output-formatyolov8\--helpusage: labelformat convert [-h] --task{instance-segmentation,object-detection}--input-format{coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}--output-format{coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}--input-file INPUT_FILE --output-file OUTPUT_FILE[--output-split OUTPUT_SPLIT]Convert labels from one format to another.optional arguments:-h, --help--task {instance-segmentation,object-detection}--input-format {coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}Input format--output-format {coco,kitti,labelbox,lightly,pascalvoc,yolov5,yolov6,yolov7,yolov8}Output format'coco' input arguments:--input-file INPUT_FILEPath to input COCO JSON file'yolov8' output arguments:--output-file OUTPUT_FILEOutput data.yaml file--output-split OUTPUT_SPLITSplit to useCodePlease refer to the code for a full list of available classes.frompathlibimportPathfromlabelformat.formatsimportCOCOObjectDetectionInput,YOLOv8ObjectDetectionOutput# Load the input labelslabel_input=COCOObjectDetectionInput(input_file=Path("coco-labels/train.json"))# Convert to output format and saveYOLOv8ObjectDetectionOutput(output_file=Path("yolo-labels/data.yaml"),output_split="train",).save(label_input=label_input)TutorialWe will walk through in detail how to convert object detection labels from COCO format
to YOLOv8 format and the other way around.Convert Object Detections from COCO to YOLOv8Let's assume we havecoco.jsonin thecoco-labelsdirectory with following contents:{"info":{"description":"COCO 2017 Dataset","url":"http://cocodataset.org","version":"1.0","year":2017,"contributor":"COCO Consortium","date_created":"2017/09/01"},"licenses":[{"url":"http://creativecommons.org/licenses/by/2.0/","id":4,"name":"Attribution License"}],"images":[{"file_name":"image1.jpg","height":416,"width":640,"id":0,"date_captured":"2013-11-18 02:53:27"},{"file_name":"image2.jpg","height":428,"width":640,"id":1,"date_captured":"2016-01-23 13:56:27"}],"annotations":[{"area":421,"iscrowd":0,"image_id":0,"bbox":[540,295,23,18],"category_id":2,"id":1},{"area":695.1853359360001,"iscrowd":0,"image_id":0,"bbox":[513,271,21,33],"category_id":0,"id":2},{"area":27826,"iscrowd":0,"image_id":1,"bbox":[268,63,94,295],"category_id":2,"id":16}],"categories":[{"supercategory":"animal","id":0,"name":"cat"},{"supercategory":"animal","id":1,"name":"dog"},{"supercategory":"animal","id":2,"name":"fish"}]}Convert it to YOLOv8 format with the following command:labelformat convert \--task object-detection \--input-format coco \--input-file coco-labels/coco.json \--output-format yolov8 \--output-file yolo-from-coco-labels/data.yaml \--output-split trainThis creates the following data structure with YOLOv8 labels:yolo-from-coco-labels/
├── data.yaml
└── labels/
    ├── image1.txt
    └── image2.txtThe contents of the created files will be:# data.yaml
names:
  0: cat
  1: dog
  2: fish
nc: 3
path: .
train: images

# image1.txt
2 0.86171875 0.7307692307692307 0.0359375 0.04326923076923077
0 0.81796875 0.6911057692307693 0.0328125 0.07932692307692307

# image2.txt
2 0.4921875 0.49182242990654207 0.146875 0.6892523364485982Convert Object Detections from YOLOv8 to COCOUnlike COCO format, YOLO uses relative image coordinates. To convert from YOLO to COCO
we therefore have to provide also input images. We prepare the following folder structure:yolo-labels/
├── data.yaml
├── images/
|   ├── image1.jpg
|   └── image2.jpg
└── labels/
    ├── image1.txt
    └── image2.txtThe file contents will be as above. The location of the image folder
is defined indata.yamlwith thepath(root path) andtrainfield.
Note that YOLO format allows specifying different data folders fortrain,valandtestdata splits, we chose to usetrainfor our example.To convert to COCO run the command below. Note that we specify--input-split train:labelformat convert \--task object-detection \--input-format yolov8 \--input-file yolo-labels/data.yaml \--input-split train \--output-format coco \--output-file coco-from-yolo-labels/coco.jsonThe command will producecoco-from-yolo-labels/coco.jsonwith the following contents:{"images":[{"id":0,"file_name":"image1.jpg","width":640,"height":416},{"id":1,"file_name":"image2.jpg","width":640,"height":428}],"categories":[{"id":0,"name":"cat"},{"id":1,"name":"dog"},{"id":2,"name":"fish"}],"annotations":[{"image_id":0,"category_id":2,"bbox":[540.0,295.0,23.0,18.0]},{"image_id":0,"category_id":0,"bbox":[513.0,271.0,21.0,33.0]},{"image_id":1,"category_id":2,"bbox":[268.0,63.0,94.0,295.0]}]}Note that converting from COCO to YOLO and back loses some information since
the intermediate format does not store all the fields.ContributingIf you encounter a bug or have a feature suggestion we will be happy if you file a GitHub issue.We also welcome contributions, please submit a PR.DevelopmentThe library targets python 3.7 and higher. We use poetry to manage the development environment.Here is an example development workflow:# Create a virtual environment with development dependenciespoetryenvusepython3.7
poetryinstall# Make changes...# Autoformat the codepoetryrunmakeformat# Run testspoetryrunmakeall-checksMaintained ByLightlyis a spin-off from ETH Zurich that helps companies
build efficient active learning pipelines to select the most relevant data for their models.You can find out more about the company and it's services by following the links below:HomepageWeb-AppLightly Solution Documentation (Lightly Worker & API)