forked from positive666/yolo_research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeTxt.py
58 lines (43 loc) · 1.16 KB
/
makeTxt.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
"""
Usage:
$ python makeTxt.py --path 'DataPath'
"""
import os
import random
import argparse
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default='/all_labelData', help='data path')
opt = parser.parse_args()
print(opt)
return opt
trainval_percent = 0.9
train_percent = 0.9
opt = parse_opt()
xmlfilepath = opt.path + '/Annotations'
txtsavepath = opt.path + '/ImageSets'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open(opt.path + '/ImageSets/trainval.txt', 'w')
ftest = open(opt.path + '/ImageSets/test.txt', 'w')
ftrain = open(opt.path + '/ImageSets/train.txt', 'w')
fval = open(opt.path + '/ImageSets/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()