Search code examples
imagedeep-learningobject-detectiondata-annotationsannotation-processing

How to convert yolo annotations to coco format. Json?


I want to convert my labels in yolo format to coco format I have tried https://github.com/Taeyoung96/Yolo-to-COCO-format-converter And Pylabel They all have a bugs.

I want to train on detectron 2 but it fails to load the dataset because of the wrong json file.


Solution

  • You can use the globox package (disclaimer: I'm the author). This should ressemble something like:

    from pathlib import Path
    
    import globox
    
    
    def main() -> None:
      path = Path("/path/to/annotations/")  # Where the .txt files are
      save_file = Path("coco.json")
    
      annotations = globox.AnnotationSet.from_yolo(gts_path)
      annotations.save_coco(save_file)
    
    
    if __name__ == "__main__":
        main()
    

    If you need to map the labels using a .name file:

    names_file = Path("/path/to/classes.names")
    names = AnnotationSet.parse_names_file(names_file)
    annotations.map_labels(names)
    

    you can check the documentation for other formats (Yolo Darknet, Yolo v8, etc.).