Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate yaml including class id and name #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions general_json2yolo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import json
import shutil

import yaml
import cv2
import pandas as pd
from PIL import Image
Expand Down Expand Up @@ -258,13 +260,18 @@ def convert_coco_json(json_dir="../coco/annotations/", use_segments=False, cls91
save_dir = make_dirs() # output directory
coco80 = coco91_to_coco80_class()

categories_dict = {}

# Import json
for json_file in sorted(Path(json_dir).resolve().glob("*.json")):
fn = Path(save_dir) / "labels" / json_file.stem.replace("instances_", "") # folder name
for json_file in sorted(Path(json_dir).resolve().glob("*coco*.json")):
fn = Path(save_dir) / "labels" / json_file.stem.replace("instances_", "").replace("_coco", "") # folder name
fn.mkdir()
with open(json_file) as f:
data = json.load(f)

# Create categories_dict
categories_dict = {item["id"] - 1: item["name"] for item in data["categories"]}

# Create image dict
images = {"%g" % x["id"]: x for x in data["images"]}
# Create image-annotations dict
Expand Down Expand Up @@ -312,6 +319,19 @@ def convert_coco_json(json_dir="../coco/annotations/", use_segments=False, cls91
line = (*(segments[i] if use_segments else bboxes[i]),) # cls, box or segments
file.write(("%g " * len(line)).rstrip() % line + "\n")

yaml_dict = {
"names": {k: v for k, v in categories_dict.items()},
# "path": "yolo_datasets",
"train": "images/train",
"val": "images/val",
}

with open((Path(save_dir) / json_file.stem).with_suffix(".yaml"), "w") as f:
yaml.dump(yaml_dict, f)
shutil.copy(
(Path(save_dir) / json_file.stem).with_suffix(".yaml"), (Path(save_dir) / "classes").with_suffix(".yaml")
)


def min_index(arr1, arr2):
"""
Expand Down