-
Notifications
You must be signed in to change notification settings - Fork 2
/
voc2coco.py
68 lines (59 loc) · 3.16 KB
/
voc2coco.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
import argparse
import os
def parse_args():
"""Parse in command line arguments"""
parser = argparse.ArgumentParser(description='Convert annotations from VOC format to COCO format')
parser.add_argument('--annot_type', type=str, required=True,
help='The file format of the source annotation file: json or xml.')
if parser.parse_known_args()[0].annot_type.lower() == 'json':
option_parser = argparse.ArgumentParser()
option_parser.add_argument('--json_input_file', type=str, required=True,
help='.json annotation file path in VOC format in path/to/file.json form.')
option_parser.add_argument('--output_dir', required=True,
help='.json annotation file path in VOC format in path/to/file.json form.')
option_parser.add_argument('--val_percent', type=int, required=False, default=0,
help='Enter the percentage of validation partition of the dataset. Between 0-100.')
elif parser.parse_known_args()[0].annot_type.lower() == 'xml':
option_parser = argparse.ArgumentParser()
option_parser.add_argument('--xml_input_dir', type=str, required=True,
help='directory path for .xml annotation files in VOC format ')
option_parser.add_argument('--output_dir', required=True,
help='.json annotation file path in VOC format in path/to/file.json form.')
option_parser.add_argument('--val_percent', type=int, required=False, default=0,
help='Enter the percentage of validation partition of the dataset. Between 0-100.')
else:
raise Exception('--annot_type argument was given wrong. Either json or xml, it is supposed to be.')
return option_parser.parse_known_args()
if __name__ == "__main__":
opt = parse_args()
dest = opt[0].output_dir
val_part = opt[0].val_percent
if not os.path.exists(dest):
os.makedirs(os.path.join(dest))
if val_part:
os.makedirs(os.path.join(dest, 'val'))
print('-'*80)
if val_part > 0:
if not val_part % 10 == 0 or val_part > 90:
raise Exception('--val_percent must be multiples of 10, and should be smaller than 100.')
else:
print('[INFO] {}% of the data will be used as validation set.'.format(val_part))
else:
print('[INFO] Converted data will not contain a validation set.')
print('[START] Format conversion starts.')
if opt[1][1] == 'json':
from STANDARD2COCO import S2CC
src = opt[0].json_input_file
coco_annotations = S2CC(src, dest, val_part)
coco_annotations.convert_2_coco()
# if opt[1][1] == 'json':
# from JSON2COCO import JSON2COCO
# src = opt[0].json_input_file
# coco_annotations = JSON2COCO(src, dest, val_part)
# coco_annotations.convert_2_coco()
else:
from VOC2COCO import V2CC
src = opt[0].xml_input_dir
coco_annotations = V2CC(os.path.join(src, 'images'), dest)
file_name = coco_annotations.convert_2_coco()
# print(opt[0].xml_input_dir)