forked from aimuch/AITools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DOTA_txt2xml.py
187 lines (151 loc) · 6.26 KB
/
DOTA_txt2xml.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
import os
import scipy.misc as misc
from xml.dom.minidom import Document
import numpy as np
import copy
import cv2
import sys
import shutil
sys.path.append('../../..')
from help_utils.tools import mkdir
def save_to_xml(save_path, im_height, im_width, objects_axis, label_name):
im_depth = 0
object_num = len(objects_axis)
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder_name = doc.createTextNode('VOC2007')
folder.appendChild(folder_name)
annotation.appendChild(folder)
filename = doc.createElement('filename')
filename_name = doc.createTextNode('000024.jpg')
filename.appendChild(filename_name)
annotation.appendChild(filename)
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
database.appendChild(doc.createTextNode('The VOC2007 Database'))
source.appendChild(database)
annotation_s = doc.createElement('annotation')
annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007'))
source.appendChild(annotation_s)
image = doc.createElement('image')
image.appendChild(doc.createTextNode('flickr'))
source.appendChild(image)
flickrid = doc.createElement('flickrid')
flickrid.appendChild(doc.createTextNode('322409915'))
source.appendChild(flickrid)
owner = doc.createElement('owner')
annotation.appendChild(owner)
flickrid_o = doc.createElement('flickrid')
flickrid_o.appendChild(doc.createTextNode('knautia'))
owner.appendChild(flickrid_o)
name_o = doc.createElement('name')
name_o.appendChild(doc.createTextNode('yang'))
owner.appendChild(name_o)
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
width.appendChild(doc.createTextNode(str(im_width)))
height = doc.createElement('height')
height.appendChild(doc.createTextNode(str(im_height)))
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode(str(im_depth)))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode('0'))
annotation.appendChild(segmented)
for i in range(object_num):
objects = doc.createElement('object')
annotation.appendChild(objects)
object_name = doc.createElement('name')
object_name.appendChild(doc.createTextNode(label_name[int(objects_axis[i][-1])]))
objects.appendChild(object_name)
pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode('Unspecified'))
objects.appendChild(pose)
truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode('1'))
objects.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode('0'))
objects.appendChild(difficult)
bndbox = doc.createElement('bndbox')
objects.appendChild(bndbox)
x0 = doc.createElement('x0')
x0.appendChild(doc.createTextNode(str((objects_axis[i][0]))))
bndbox.appendChild(x0)
y0 = doc.createElement('y0')
y0.appendChild(doc.createTextNode(str((objects_axis[i][1]))))
bndbox.appendChild(y0)
x1 = doc.createElement('x1')
x1.appendChild(doc.createTextNode(str((objects_axis[i][2]))))
bndbox.appendChild(x1)
y1 = doc.createElement('y1')
y1.appendChild(doc.createTextNode(str((objects_axis[i][3]))))
bndbox.appendChild(y1)
x2 = doc.createElement('x2')
x2.appendChild(doc.createTextNode(str((objects_axis[i][4]))))
bndbox.appendChild(x2)
y2 = doc.createElement('y2')
y2.appendChild(doc.createTextNode(str((objects_axis[i][5]))))
bndbox.appendChild(y2)
x3 = doc.createElement('x3')
x3.appendChild(doc.createTextNode(str((objects_axis[i][6]))))
bndbox.appendChild(x3)
y3 = doc.createElement('y3')
y3.appendChild(doc.createTextNode(str((objects_axis[i][7]))))
bndbox.appendChild(y3)
f = open(save_path, 'w')
f.write(doc.toprettyxml(indent=''))
f.close()
class_list = ['bus', 'car', 'child', 'cyclist',
'dog', 'electric_cyclist', 'motocyclist',
'person', 'person_sitting',
'train', 'tricyclist',
'truck', 'van', 'cat']
def format_label(txt_list):
format_data = []
for i in txt_list:
if len(i.split(' ')) < 9:
continue
format_data.append(
[float(xy) for xy in i.split(' ')[:8]] + [class_list.index(i.split(' ')[8])]
)
if i.split(' ')[8] not in class_list:
print('warning found a new label :', i.split(' ')[8])
exit()
return np.array(format_data)
def move_file(image_path, save_dir, boxes_all, width, height):
if len(boxes_all) > 0:
dst_xml_path = os.path.join(save_dir, 'labeltxt')
dst_img_path = os.path.join(save_dir, 'images')
if not os.path.exists(dst_xml_path):
os.makedirs(dst_xml_path)
if not os.path.exists(dst_img_path):
os.makedirs(dst_img_path)
dst_img_path_ = os.path.join(dst_img_path, os.path.basename(image_path))
shutil.copyfile(image_path, dst_img_path_)
xml = os.path.join(dst_xml_path, os.path.basename(image_path).replace(".jpg", ".xml"))
save_to_xml(xml, width, height, boxes_all, class_list)
print('class_list', len(class_list))
raw_data = 'data/FISHEYE/train/'
raw_images_dir = os.path.join(raw_data, 'images')
raw_label_dir = os.path.join(raw_data, 'labelTxt')
save_dir = 'data/FISHEYE/FISHEYE_train/trainval/'
images = [i for i in os.listdir(raw_images_dir) if 'jpg' in i]
labels = [i for i in os.listdir(raw_label_dir) if 'txt' in i]
print('find image', len(images))
print('find label', len(labels))
min_length = 1e10
max_length = 1
img_h, img_w = 1080, 1920
for idx, img in enumerate(images):
print(idx, 'read image', img)
img_path = os.path.join(raw_images_dir, img)
txt_data = open(os.path.join(raw_label_dir, img.replace('jpg', 'txt')), 'r').readlines()
box = format_label(txt_data)
move_file(img_path, save_dir, box, img_w, img_h)