How to convert yolo annotations to coco format. Json?
Asked Answered
L

1

6

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.

Lotti answered 7/4, 2022 at 21:54 Comment(1)
Could you elaborate on the error you get? Also, provide file examples.Connive
C
5

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.).

Connive answered 8/4, 2022 at 20:2 Comment(10)
Hello thanks for your help but i get this error File "C:\Users\Nima\Desktop\ObjectDetectionEval-master\ObjectDetectionEval\boundingbox.py", line 186, in <genexpr> coords = (float(c) for c in coords) ValueError: could not convert string to float: ''Lotti
An annotation file may be malformed, could you investigate which one is the culprit and post it here please?Connive
I tracked down the bug my labels in the first line have an space in end and that makes your code not to work example:0 0.5375 0.8875 0.10625 0.212** this * characters are spaces . is there any way to strip end of the lines in your codeLotti
I will send an example of the label file your GitHub repository issuesLotti
Yes, line 175 of boundingbox.py you can add a .strip() after the .split(), this should solve the issue. I'll fix the bug in the repo.Connive
before*, not afterConnive
There is no bug in your repo it was bug in my dataset . i fixed my dataset with script and your code worked like charmLotti
Sure, but fixing it makes my repo more robust to these kind of dataset bugs. You can accept my answer if it solved your issue, thanks!Connive
There is little problem: there is no method named Annotation.parse_names_file(names_file) is my repo outdated? and I want to map categories from 1 instead of zero is there anyway for thisLotti
The function name is parse_names_file(). You can use the map_labels() method to map to different labels.Connive

© 2022 - 2025 — McMap. All rights reserved.