forked from maximenkooo/ClothesDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_xml.py
168 lines (129 loc) · 6.2 KB
/
convert_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
156
157
158
159
160
161
162
163
164
165
166
167
168
import os
from tqdm import tqdm
from sys import exit
import argparse
import cv2
from textwrap import dedent
from lxml import etree
XML_DIR = 'annot'
#os.chdir('Dataset')
os.chdir(os.path.join("OID", "Dataset" ))
DIRS = os.listdir(os.getcwd())
for DIR in DIRS:
if os.path.isdir(DIR):
os.chdir(DIR)
print("Currently in Subdirectory:", DIR)
CLASS_DIRS = os.listdir(os.getcwd())
for CLASS_DIR in CLASS_DIRS:
if " " in CLASS_DIR:
os.rename(CLASS_DIR, CLASS_DIR.replace(" ", "_"))
CLASS_DIRS = os.listdir(os.getcwd())
for CLASS_DIR in CLASS_DIRS:
#if " " in CLASS_DIR:
# os.rename(CLASS_DIR, CLASS_DIR.replace(" ", "_"))
if os.path.isdir(CLASS_DIR):
os.chdir(CLASS_DIR)
print("\n" + "Creating PASCAL VOC XML Files for Class:", CLASS_DIR)
# Create Directory for annotations if it does not exist yet
if not os.path.exists(XML_DIR):
os.makedirs(XML_DIR)
print(os.getcwd())
#Read Labels from OIDv4 ToolKit
os.chdir("Label")
#Create PASCAL XML
for filename in tqdm(os.listdir(os.getcwd())):
if filename.endswith(".txt"):
filename_str = str.split(filename, ".")[0]
annotation = etree.Element("annotation")
os.chdir("..")
folder = etree.Element("folder")
folder.text = os.path.basename(os.getcwd())
annotation.append(folder)
filename_xml = etree.Element("filename")
filename_xml.text = filename_str + ".jpg"
annotation.append(filename_xml)
path = etree.Element("path")
path.text = os.path.join(os.path.dirname(os.path.abspath(filename)), filename_str + ".jpg")
annotation.append(path)
source = etree.Element("source")
annotation.append(source)
database = etree.Element("database")
database.text = "Unknown"
source.append(database)
size = etree.Element("size")
annotation.append(size)
width = etree.Element("width")
height = etree.Element("height")
depth = etree.Element("depth")
img = cv2.imread(filename_xml.text)
try:
width.text = str(img.shape[1])
except AttributeError:
#os.chdir("..")
os.chdir("Label")
continue
height.text = str(img.shape[0])
depth.text = str(img.shape[2])
size.append(width)
size.append(height)
size.append(depth)
segmented = etree.Element("segmented")
segmented.text = "0"
annotation.append(segmented)
os.chdir("Label")
label_original = open(filename, 'r')
# Labels from OIDv4 Toolkit: name_of_class X_min Y_min X_max Y_max
for line in label_original:
line = line.strip()
l = line.split(' ')
class_name = l[0]
try:
xmin_l = str(int(float(l[1])))
add1 = 0
except ValueError:
class_name = l[0]+"_"+l[1]
add1 = 1
xmin_l = str(int(float(l[1+add1])))
ymin_l = str(int(float(l[2+add1])))
xmax_l = str(int(float(l[3+add1])))
ymax_l = str(int(float(l[4+add1])))
obj = etree.Element("object")
annotation.append(obj)
name = etree.Element("name")
name.text = class_name
obj.append(name)
pose = etree.Element("pose")
pose.text = "Unspecified"
obj.append(pose)
truncated = etree.Element("truncated")
truncated.text = "0"
obj.append(truncated)
difficult = etree.Element("difficult")
difficult.text = "0"
obj.append(difficult)
bndbox = etree.Element("bndbox")
obj.append(bndbox)
xmin = etree.Element("xmin")
xmin.text = xmin_l
bndbox.append(xmin)
ymin = etree.Element("ymin")
ymin.text = ymin_l
bndbox.append(ymin)
xmax = etree.Element("xmax")
xmax.text = xmax_l
bndbox.append(xmax)
ymax = etree.Element("ymax")
ymax.text = ymax_l
bndbox.append(ymax)
os.chdir("..")
os.chdir(XML_DIR)
# write xml to file
s = etree.tostring(annotation, pretty_print=True)
with open(filename_str + ".xml", 'wb') as f:
f.write(s)
f.close()
os.chdir("..")
os.chdir("Label")
os.chdir("..")
os.chdir("..")
os.chdir("..")