|
1 | 1 | import contextlib |
2 | 2 | import json |
3 | | - |
| 3 | +import yaml |
4 | 4 | import cv2 |
5 | 5 | import pandas as pd |
6 | 6 | from PIL import Image |
@@ -254,13 +254,18 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91 |
254 | 254 | save_dir = make_dirs() # output directory |
255 | 255 | coco80 = coco91_to_coco80_class() |
256 | 256 |
|
| 257 | + categories_dict = {} |
| 258 | + |
257 | 259 | # Import json |
258 | 260 | for json_file in sorted(Path(json_dir).resolve().glob('*.json')): |
259 | 261 | fn = Path(save_dir) / 'labels' / json_file.stem.replace('instances_', '') # folder name |
260 | 262 | fn.mkdir() |
261 | 263 | with open(json_file) as f: |
262 | 264 | data = json.load(f) |
263 | 265 |
|
| 266 | + # Create categories_dict |
| 267 | + categories_dict = {item['id']: item['name'] for item in data['categories']} |
| 268 | + |
264 | 269 | # Create image dict |
265 | 270 | images = {'%g' % x['id']: x for x in data['images']} |
266 | 271 | # Create image-annotations dict |
@@ -308,6 +313,16 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91 |
308 | 313 | line = *(segments[i] if use_segments else bboxes[i]), # cls, box or segments |
309 | 314 | file.write(('%g ' * len(line)).rstrip() % line + '\n') |
310 | 315 |
|
| 316 | + yaml_dict = { |
| 317 | + "names": {v: k for k, v in categories_dict.items()}, |
| 318 | + # "path": "yolo_datasets", |
| 319 | + "train": "images/train", |
| 320 | + "val": "images/val" |
| 321 | + } |
| 322 | + |
| 323 | + with open((Path(save_dir) / json_file.stem).with_suffix('.yaml'), "w") as f: |
| 324 | + yaml.dump(yaml_dict, f) |
| 325 | + |
311 | 326 |
|
312 | 327 | def min_index(arr1, arr2): |
313 | 328 | """Find a pair of indexes with the shortest distance. |
|
0 commit comments