Home > Enterprise >  How to convert yolo annotations to coco format. Json?
How to convert yolo annotations to coco format. Json?

Time:04-14

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.

Thanks everybody

CodePudding user response:

Could you try with this tool (disclaimer: I'm the author)? It is not (yet) a Python package so you need to downloads the repo first. This should ressemble something like:

from ObjectDetectionEval import *
from pathlib import Path


def main() -> None:
  path = Path("/path/to/annotations/")  # Where the .txt files are
  names_file = Path("/path/to/classes.names")
  save_file = Path("coco.json")

  annotations = AnnotationSet.from_yolo(gts_path).map_labels(names)
  
  # If you need to change the labels
  # names = Annotation.parse_names_file(names_file)
  # annotations.map_labels(names)

  annotations.save_coco(save_file)


if __name__ == "__main__":
    main()

If you need more control (coordinate format, images location and extension, etc.) you should use the more generic AnnotationSet.from_txt(). If it does not suit your needs you can easily implement your own parser using AnnotationSet.from_folder().

  • Related