-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_doh_to_coco.py
129 lines (106 loc) · 4.49 KB
/
change_doh_to_coco.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
import collections as cl
import os
def get_info():
tmp = cl.OrderedDict()
tmp["description"] = "100doh dataset of coco format"
tmp["url"] = ""
tmp["version"] = "1"
tmp["year"] = 2024
tmp["contributor"] = ""
tmp["data_created"] = "2024/01/01"
return tmp
def get_licenses():
tmp = cl.OrderedDict()
tmp["id"] = 0
tmp["url"] = ""
tmp["name"] = ""
return tmp
def get_image_data(id, annotation, image_file):
tmp = cl.OrderedDict()
tmp["license"] = 0
tmp["file_name"] = image_file
tmp["coco_url"] = ""
tmp["height"] = annotation["height"]
tmp["width"] = annotation["width"]
tmp["date_captured"] = ""
tmp["flickr_url"] = ""
tmp["id"] = id
return tmp
def get_categories():
tmps = []
sup = ["N/A", "hand", "object"]
categories = ["N/A", "hand", "object"]
for i in range(len(categories)):
tmp = cl.OrderedDict()
tmp["id"] = i
tmp["supercategory"] = sup[i]
tmp["name"] = categories[i]
tmps.append(tmp)
return tmps
def main(annotation_dir, json_path):
with open(annotation_dir) as f:
annotations_origin = json.load(f)
image_list = annotations_origin.keys()
print(type(image_list))
print(len(image_list))
info = get_info()
licenses = get_licenses()
categories = get_categories()
images = []
annotations = []
# get image data and annotations
count_ano_id = 0
for i, image_file in enumerate(image_list):
annotation_origin = annotations_origin[image_file]
image_data = get_image_data(i, annotation_origin[0], image_file)
# get annotation per image
old_obj_bbox = None # 同一画像内の1つ前のobj_bboxを保存しておく
for side_annotation in annotation_origin:
side_annotation_dict = cl.OrderedDict()
new_hand_bbox = [side_annotation["x1"] * side_annotation["width"],
side_annotation["y1"] * side_annotation["height"],
side_annotation["x2"] * side_annotation["width"] - side_annotation["x1"] * side_annotation["width"],
side_annotation["y2"] * side_annotation["height"] - side_annotation["y1"] * side_annotation["height"]]
side_annotation_dict["segmentation"] = []
side_annotation_dict["id"] = count_ano_id
count_ano_id += 1
side_annotation_dict["image_id"] = i
side_annotation_dict["category_id"] = 1
side_annotation_dict["area"] = new_hand_bbox[2] * new_hand_bbox[3]
side_annotation_dict["iscrowd"] = 0
side_annotation_dict["bbox"] = new_hand_bbox
annotations.append(side_annotation_dict)
if side_annotation["obj_bbox"] is not None:
side_obj_annotation_dict = cl.OrderedDict()
new_obj_bbox = [side_annotation["obj_bbox"]["x1"] * side_annotation["width"],
side_annotation["obj_bbox"]["y1"] * side_annotation["height"],
side_annotation["obj_bbox"]["x2"] * side_annotation["width"] - side_annotation["obj_bbox"]["x1"] * side_annotation["width"],
side_annotation["obj_bbox"]["y2"] * side_annotation["height"] - side_annotation["obj_bbox"]["y1"] * side_annotation["height"]]
side_obj_annotation_dict["segmentation"] = []
side_obj_annotation_dict["id"] = count_ano_id
count_ano_id += 1
side_obj_annotation_dict["image_id"] = i
side_obj_annotation_dict["category_id"] = 2
side_obj_annotation_dict["area"] = new_obj_bbox[2] * new_obj_bbox[3]
side_obj_annotation_dict["iscrowd"] = 0
side_obj_annotation_dict["bbox"] = new_obj_bbox
if new_obj_bbox != old_obj_bbox:
annotations.append(side_obj_annotation_dict)
old_obj_bbox = new_obj_bbox
images.append(image_data)
print("Made file")
json_data = {
'info': info,
'images': images,
'licenses': licenses,
'annotations': annotations,
'categories': categories,
}
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=2)
annotation_dir_list = ["trainval.json", "test.json"]
json_path_list = ["trainval_cocof.json", "test_cocof.json"]
for a, j in zip(annotation_dir_list, json_path_list):
print(a, j)
main(a, j)