-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathparse_voc_xml.py
executable file
·155 lines (124 loc) · 4.56 KB
/
parse_voc_xml.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
# coding: utf-8
from __future__ import division
import xml.etree.ElementTree as ET
import os
names_dict = {}
cnt = 0
# -*- coding:utf-8 -*-
# 数据集划分,训练集,测试集,验证集
import xml.etree.ElementTree as ET
# 数据集划分,训练集,测试集,验证集
import random
import urllib.request
import os, tarfile
saveBasePath = r"./VOC2007/ImageSets" # txt文件保存目录
total_xml = os.listdir(r'./VOC2007/Annotations') # 获取标注文件(file_name.xml)
# 划分数据集为(训练,验证,测试集 = 49%,20%,30%)
val_percent = 0.2 # 传参
test_percent = 0.1
trainval_percent = 0.9
# print(trainval_percent)
tv = int(len(total_xml) * trainval_percent)
#tr = int(len(total_xml) * train_percent)
ta = int(tv * val_percent)
tr = int(tv -ta)
tt = int(len(total_xml) * test_percent)
# 打乱训练文件(洗牌)
trainval = random.sample(range(len(total_xml)), tv)
train = random.sample(trainval, tr)
print("train size", tr)
print("val size", ta)
print("Test size", tt)
# with open('/tmp/VOC2007/split.txt', 'w', encoding='utf-8') as f:
# f.write(str(val_percent))
ftrainval = open(os.path.join(saveBasePath, 'Main/trainval.txt'), 'w')
ftest = open(os.path.join(saveBasePath, 'Main/test.txt'), 'w')
ftrain = open(os.path.join(saveBasePath, 'Main/train.txt'), 'w')
fval = open(os.path.join(saveBasePath, 'Main/val.txt'), 'w')
for i in range(len(total_xml)): # 遍历所有 file_name.xml 文件
name = total_xml[i][:-4] + '\n' # 获取 file_name
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()
f = open( 'data/classes/visdrone.names', 'r' ).readlines()
for line in f:
line = line.strip()
names_dict[line] = cnt
cnt += 1
voc_07 = './VOC2007'
# voc_12 = '/data/VOCdevkit/VOC2012'
anno_path = [os.path.join(voc_07, 'Annotations')]
img_path = [os.path.join(voc_07, 'JPEGImages')]
trainval_path = [os.path.join(voc_07, 'ImageSets/Main/trainval.txt')]
test_path = [os.path.join(voc_07, 'ImageSets/Main/test.txt')]
def parse_xml(path):
tree = ET.parse(path)
img_name = path.split('/')[-1][:-4]
height = tree.findtext("./size/height")
width = tree.findtext("./size/width")
objects = [img_name, width, height]
for obj in tree.findall('object'):
difficult = obj.find('difficult').text
if difficult == '1':
continue
name = obj.find('name').text
bbox = obj.find('bndbox')
xmin = bbox.find('xmin').text
ymin = bbox.find('ymin').text
xmax = bbox.find('xmax').text
ymax = bbox.find('ymax').text
name = str(names_dict[name])
objects.extend([name, xmin, ymin, xmax, ymax])
if len(objects) > 1:
return objects
else:
return None
test_cnt = 0
def gen_test_txt(txt_path):
global test_cnt
f = open(txt_path, 'w')
for i, path in enumerate(test_path):
img_names = open(path, 'r').readlines()
for img_name in img_names:
img_name = img_name.strip()
xml_path = anno_path[i] + '/' + img_name + '.xml'
objects = parse_xml(xml_path)
if objects:
objects[0] = img_path[i] + '/' + img_name + '.jpg'
#print(objects[0])
if os.path.exists(objects[0]):
objects.insert(0, str(test_cnt))
test_cnt += 1
objects = ' '.join(objects) + '\n'
#print(objects)
f.write(objects)
f.close()
train_cnt = 0
def gen_train_txt(txt_path):
global train_cnt
f = open(txt_path, 'w')
for i, path in enumerate(trainval_path):
img_names = open(path, 'r').readlines()
for img_name in img_names:
img_name = img_name.strip()
xml_path = anno_path[i] + '/' + img_name + '.xml'
objects = parse_xml(xml_path)
if objects:
objects[0] = img_path[i] + '/' + img_name + '.jpg'
if os.path.exists(objects[0]):
objects.insert(0, str(train_cnt))
train_cnt += 1
objects = ' '.join(objects) + '\n'
f.write(objects)
f.close()
gen_train_txt('./train.txt')
gen_test_txt('./val.txt')