-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_voc_to_coco.py
309 lines (249 loc) · 8.36 KB
/
convert_voc_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import json
import glob
import argparse
from tqdm import tqdm
import xml.etree.ElementTree as element_tree
def get_args():
parser = argparse.ArgumentParser(
description="Convert Pascal VOC annotation to COCO format.")
parser.add_argument(
"xml_dir",
help="Directory path to xml files.",
type=str,
default='Annotations',
)
parser.add_argument(
"json_file",
help="Output COCO format json file.",
type=str,
default='output.json',
)
parser.add_argument(
"--start_image_id",
type=int,
default=None,
)
parser.add_argument(
"--start_bbox_id",
help="Bounding Box start ID.",
type=int,
default=1,
)
parser.add_argument(
"--category",
help="Specify a category list.",
type=str,
default=None,
)
parser.add_argument(
"--indent",
help="COCO format json indent.",
type=int,
default=None,
)
parser.add_argument(
"--bbox_offset",
help="Bounding Box offset.",
type=int,
default=-1,
)
parser.add_argument(
"--reserve_category_num",
type=int,
default=None,
)
args = parser.parse_args()
return args
def main():
# 引数取得
args = get_args()
xml_dir = args.xml_dir
json_file = args.json_file
start_image_id = args.start_image_id
start_bbox_id = args.start_bbox_id
category_txt_path = args.category
indent = args.indent
bbox_offset = args.bbox_offset
reserve_category_num = args.reserve_category_num
# Pascal VOC XMLファイルリスト取得
xml_files = glob.glob(os.path.join(xml_dir, "*.xml"))
# 事前定義のカテゴリーリストを取得
predefine_categories = None
if category_txt_path is not None:
with open(category_txt_path, 'r') as f:
category_list = f.readlines()
predefine_categories = {
name.rstrip('\n'): i
for i, name in enumerate(category_list)
}
# カテゴリーリスト生成
if predefine_categories is not None:
categories = predefine_categories
else:
categories = get_categories(xml_files)
# Pascal VOC → COCO 変換
print("Number of xml files: {}".format(len(xml_files)))
json_dict = convert_xml_to_json(
xml_files,
categories,
start_image_id,
start_bbox_id,
bbox_offset,
reserve_category_num,
)
# JSON保存
if os.path.dirname(json_file):
os.makedirs(os.path.dirname(json_file), exist_ok=True)
with open(json_file, "w") as fp:
json_text = json.dumps(json_dict, indent=indent)
fp.write(json_text)
print("Success: {}".format(json_file))
def get_categories(xml_files):
classes_names = []
# 全XMLのobjectからnameを取得
for xml_file in xml_files:
tree = element_tree.parse(xml_file)
root = tree.getroot()
for member in root.findall("object"):
classes_names.append(member[0].text)
# 重複を削除してソート
classes_names = list(set(classes_names))
classes_names.sort()
# Dict形式に変換
categories = {name: i + 1 for i, name in enumerate(classes_names)}
return categories
def get_element(root, name, length=None):
# 指定タグの値を取得
vars = root.findall(name)
# 長さチェック
if length is not None:
if len(vars) == 0:
raise ValueError("Can not find %s in %s." % (name, root.tag))
if length > 0 and len(vars) != length:
raise ValueError(
"The size of %s is supposed to be %d, but is %d." %
(name, length, len(vars)))
if length == 1:
vars = vars[0]
return vars
def get_basename_without_ext(filename):
# 拡張子を含まないファイル名を取得
basename_without_ext = filename.replace("\\", "/")
basename_without_ext = os.path.splitext(os.path.basename(filename))[0]
return str(basename_without_ext)
def get_basename_with_ext(filename):
# 拡張子を含むファイル名を取得
basename_with_ext = filename.replace("\\", "/")
basename_with_ext = os.path.basename(basename_with_ext)
return str(basename_with_ext)
def convert_xml_to_json(
xml_files,
categories=None,
start_image_id=None,
start_bbox_id=1,
bbox_offset=-1,
reserve_category_num=None,
):
json_dict = {
"images": [],
"type": "instances",
"annotations": [],
"categories": []
}
count_dict = {}
bbox_id = start_bbox_id
if start_image_id is not None:
image_id_count = start_image_id
for xml_file in tqdm(xml_files, "Convert XML to JSON"):
# ルート要素取得
tree = element_tree.parse(xml_file)
root = tree.getroot()
# 画像ファイル名取得
path = get_element(root, "path")
if len(path) == 1:
filename = get_basename_with_ext(path[0].text)
elif len(path) == 0:
filename = get_element(root, "filename", 1).text
else:
raise ValueError("%d paths found in %s" % (len(path), xml_file))
# 画像情報取得
size = get_element(root, "size", 1)
width = int(get_element(size, "width", 1).text)
height = int(get_element(size, "height", 1).text)
if start_image_id is None:
image_id = get_basename_without_ext(filename)
else:
image_id = image_id_count
# JSON Dict追加
image_info = {
"file_name": filename,
"height": height,
"width": width,
"id": image_id,
}
json_dict["images"].append(image_info)
# object情報取得
for obj in get_element(root, "object"):
# カテゴリー名取得
category = get_element(obj, "name", 1).text
# 初出のカテゴリー名の場合、リストに追加
if category not in categories:
new_id = len(categories)
categories[category] = new_id
# カテゴリー数カウント
if category not in count_dict:
count_dict[category] = 0
else:
count_dict[category] += 1
# カテゴリーID取得
category_id = categories[category]
# バウンディングボックス情報取得
bbox = get_element(obj, "bndbox", 1)
xmin = int(float(get_element(bbox, "xmin", 1).text)) + bbox_offset
ymin = int(float(get_element(bbox, "ymin", 1).text)) + bbox_offset
xmax = int(float(get_element(bbox, "xmax", 1).text))
ymax = int(float(get_element(bbox, "ymax", 1).text))
assert xmax > xmin
assert ymax > ymin
bbox_width = abs(xmax - xmin)
bbox_height = abs(ymax - ymin)
# JSON Dict追加
annotation_info = {
"area": bbox_width * bbox_height,
"iscrowd": 0,
"image_id": image_id,
"bbox": [xmin, ymin, bbox_width, bbox_height],
"category_id": category_id,
"id": bbox_id,
"ignore": 0,
"segmentation": [],
}
json_dict["annotations"].append(annotation_info)
bbox_id = bbox_id + 1
if start_image_id is not None:
image_id_count = image_id_count + 1
# カテゴリー情報
max_category_id = 0
for category_name, category_id in categories.items():
category_info = {
"supercategory": "none",
"id": category_id,
"name": category_name
}
json_dict["categories"].append(category_info)
if max_category_id <= category_id:
max_category_id = category_id + 1
if reserve_category_num is not None:
for index in range(len(json_dict["categories"]), reserve_category_num):
category_info = {
"supercategory": "none",
"id": max_category_id,
"name": 'reserve_' + str(index).zfill(4)
}
json_dict["categories"].append(category_info)
max_category_id += 1
print(count_dict)
return json_dict
if __name__ == "__main__":
main()