forked from Solafune-Inc/OC-cost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coco2Annotations.py
68 lines (54 loc) · 1.88 KB
/
coco2Annotations.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
from collections import defaultdict
import argparse
def coco2Annotations(coco_dict: dict):
annotations_dict = {
"images": list()
}
images_annotations = defaultdict(list)
image_pair = dict()
category_pair = dict()
for image in coco_dict["images"]:
image_pair[(image["id"])] = image["file_name"]
for category in coco_dict["categories"]:
category_pair[(category["id"])] = category["name"]
for bbox in coco_dict["annotations"]:
image_name = image_pair[bbox["image_id"]]
category_name = category_pair[bbox["category_id"]]
lefttop_x = bbox["bbox"][0]
lefttop_y = bbox["bbox"][1]
width = bbox["bbox"][2]
height = bbox["bbox"][3]
images_annotations[image_name].append(
{
"class": category_name,
"lefttop_x": lefttop_x,
"lefttop_y": lefttop_y,
"rightbottom_x": lefttop_x + width,
"rightbottom_y": lefttop_y + height
}
)
for key in images_annotations.keys():
annotations_dict["images"].append(
{
"name": key,
"annotation": images_annotations[key]
}
)
return annotations_dict
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='translate coco format to Annotations format')
parser.add_argument(
'-i', '--input', help='input json')
parser.add_argument(
'-o', '--output', help='output json')
parser.add_argument('--prediction', help='is json file is prediction file',
action='store_false')
args = parser.parse_args()
load_dict = dict()
with open(args.input) as f:
load_dict = json.load(f)
trans_dict = coco2Annotations(load_dict)
with open(args.output, 'w') as fp:
json.dump(trans_dict, fp)