-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.py
198 lines (149 loc) · 7.83 KB
/
converter.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
188
189
190
191
192
193
194
195
196
"""
Location: OPENIMAGEDOWNLOADER parent directory
Usage:
Start from OPENIMAGEDOWNLOADER root directory.
The script will create directories called PASCAL_XML (similar to the Label directories) in the Dataset Subdirectories.
These directories contain the XML files.
You need to download the Images and generate ToolKit-Style-Labels via the TOOLKIT before using this script.
"""
import os
import argparse
import argparse
import cv2
from tqdm import tqdm
from sys import exit
from textwrap import dedent
from lxml import etree
from modules.utils import bcolors as bc
XML_DIR = 'PASCAL_XML'
COMMANDS = ["xml", "yolo"]
def converter():
os.chdir(os.path.join("OID", "Dataset"))
DIRS = os.listdir(os.getcwd())
for DIR in DIRS:
if DIR == "train":
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 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)
#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(path.text)
width.text = str(img.shape[1])
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[1]
xmin_l = str(int(float(l[2])))
ymin_l = str(int(float(l[3])))
xmax_l = str(int(float(l[4])))
ymax_l = str(int(float(l[5])))
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("..")
elif DIR == "test":
print("[INFO] TEST DIRECTORY IS ALREADY DONE")
def converterXML(version):
os.chdir(os.path.join("OID", "Dataset_XML"))
DIRS = os.listdir(os.getcwd())
print(DIRS)
def converterYAML(version):
os.chdir(os.path.join("OID", "Dataset_YAML"))
DIRS = os.listdir(os.getcwd())
print(DIRS)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="TO PASCAL XML/YAML FORMAT")
parser.add_argument("command",
metavar="<command> 'xml' or 'yaml'.",
help="'xml' or 'yaml'.")
parser.add_argument('--version', required=False, choices=['v4', 'v5', 'v6'],
metavar="v4 or v5 or v6",
help='Open Image dataset version to download')
args = parser.parse_args()
if args.command == COMMANDS[0]:
print(bc.INFO + "Activating XML converter format" + bc.ENDC)
#TODO: Integrate converterXML()
converterXML(args.version)
elif args.command == COMMANDS[1]:
print(bc.INFO + "Activating YAML converter format" + bc.ENDC)
#TODO: Integrate converterYOLO()