-
Notifications
You must be signed in to change notification settings - Fork 1
/
VOC2Json.py
44 lines (33 loc) · 1.13 KB
/
VOC2Json.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
from utils.ImgDataInfo import ImgDataInfo
import json
import os
import random
def main():
# read config.json
with open('config.json') as json_file:
cfg = json.load(json_file)
data_dir = './data'
if not os.path.isdir(data_dir):
os.makedirs(data_dir)
img_ls = os.listdir(cfg['img_dir'])
# read xml to img info obj
img_info_ls = []
for img in img_ls:
try:
img_info = ImgDataInfo(img, cfg['img_dir'], cfg['annotation_dir'])(cfg["class_ls"])
img_info_ls.append(img_info)
except:
None
# shuffle
random.shuffle(img_info_ls)
# split training and validation
n_data = len(img_info_ls)
val_num = int(n_data * cfg["val_train_ratio"])
val_info_ls = img_info_ls[:val_num]
train_info_ls = img_info_ls[val_num:]
with open(os.path.join(data_dir, cfg["train_img_info"]), 'w') as outfile:
json.dump(train_info_ls, outfile)
with open(os.path.join(data_dir, cfg["val_img_info"]), 'w') as outfile:
json.dump(val_info_ls, outfile)
if __name__=="__main__":
main()