-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_coco2ytvis.py
53 lines (45 loc) · 1.89 KB
/
convert_coco2ytvis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import json
from vita.data.datasets.ytvis import (
COCO_TO_YTVIS_2019,
COCO_TO_YTVIS_2021,
)
from vita.data.datasets.ovis import (
COCO_TO_OVIS,
)
_root = os.getenv("DETECTRON2_DATASETS", "datasets")
convert_list = [
(COCO_TO_YTVIS_2019,
os.path.join(_root, "coco/annotations/instances_train2017.json"),
os.path.join(_root, "coco/annotations/coco2ytvis2019_train.json"), "COCO to YTVIS 2019:"),
(COCO_TO_YTVIS_2019,
os.path.join(_root, "coco/annotations/instances_val2017.json"),
os.path.join(_root, "coco/annotations/coco2ytvis2019_val.json"), "COCO val to YTVIS 2019:"),
(COCO_TO_YTVIS_2021,
os.path.join(_root, "coco/annotations/instances_train2017.json"),
os.path.join(_root, "coco/annotations/coco2ytvis2021_train.json"), "COCO to YTVIS 2021:"),
(COCO_TO_YTVIS_2021,
os.path.join(_root, "coco/annotations/instances_val2017.json"),
os.path.join(_root, "coco/annotations/coco2ytvis2021_val.json"), "COCO val to YTVIS 2021:"),
(COCO_TO_OVIS,
os.path.join(_root, "coco/annotations/instances_train2017.json"),
os.path.join(_root, "coco/annotations/coco2ovis_train.json"), "COCO to OVIS:"),
]
for convert_dict, src_path, out_path, msg in convert_list:
src_f = open(src_path, "r")
out_f = open(out_path, "w")
src_json = json.load(src_f)
# print(src_json.keys()) dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
out_json = {}
for k, v in src_json.items():
if k != 'annotations':
out_json[k] = v
converted_item_num = 0
out_json['annotations'] = []
for anno in src_json['annotations']:
if anno["category_id"] not in convert_dict:
continue
out_json['annotations'].append(anno)
converted_item_num += 1
json.dump(out_json, out_f)
print(msg, converted_item_num, "items converted.")