Skip to content

Commit

Permalink
Add CLI tools for converting yolo to mscoco
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqwang committed Mar 26, 2022
1 parent f9f12ac commit 79f5b58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
29 changes: 29 additions & 0 deletions tools/convert_yolo_to_coco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2022, yolort team. All rights reserved.

import argparse

from yolort.utils.yolo2coco import YOLO2COCO


def get_parser():
parser = argparse.ArgumentParser("Datasets converter from yolo to coco", add_help=True)

parser.add_argument("--data_source", default="./coco128", help="Dataset root path")
parser.add_argument("--class_names", default="./coco.name", help="Path of the label names")
parser.add_argument("--split", default="train", choices=["train", "val"], help="Dataset split part")
parser.add_argument("--year", default=2017, type=int, help="Year of the dataset")

return parser


def cli_main():
parser = get_parser()
args = parser.parse_args()
print(f"Command Line Args: {args}")

converter = YOLO2COCO(args.data_source, args.class_names, split=args.split, year=args.year)
converter.generate()


if __name__ == "__main__":
cli_main()
13 changes: 4 additions & 9 deletions yolort/utils/yolo2coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class YOLO2COCO:
root (string): Root directory of the Sintel Dataset
metalabels (string | List[string]): Concrete label names of different classes
split (string, optional): The dataset split, either "train" (default) or "test"
year (int, optional): The year of the dataset. Default: 2021
year (int, optional): The year of the dataset. Default: 2017
"""

def __init__(
self,
root: str,
metalabels: Union[str, List[str]],
split: str = "train",
year: int = 2021,
year: int = 2017,
) -> None:

self._year = year
self.type = "instances"
self.split = split
self.split = f"{split}{year}"
self.root_path = Path(root)
self.label_path = self.root_path / "labels"
self.annotation_root = self.root_path / "annotations"
Expand All @@ -50,12 +50,7 @@ def __init__(
}
]
self.categories = [
{
"id": coco_category["id"],
"name": coco_category["name"],
"supercategory": coco_category["supercategory"],
}
for coco_category in self.metadata
{"id": coco_category["id"], "name": coco_category["name"]} for coco_category in self.metadata
]

@staticmethod
Expand Down

0 comments on commit 79f5b58

Please sign in to comment.