Skip to content

Commit 3fce087

Browse files
authored
Add -c option to add custom class file (#13)
#12 Support custom class files.
1 parent bcf6e96 commit 3fce087

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ sudo pip install -r requirements.txt
99
### Usage
1010

1111
```bash
12-
python xmltotxt.py -xml xml -out out
12+
python xmltotxt.py -c cls.txt -xml xml -out out
1313
```
14+
#### Mandatory arguments
15+
-xml
16+
#### Optional arguments
17+
-c, -out
1418

1519
### Example
1620

reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_xml_files(self):
1616
return xml_filenames
1717

1818
@staticmethod
19-
def get_classes(filename="classes.txt"):
19+
def get_classes(filename):
2020
with open(os.path.join(os.path.dirname(os.path.realpath('__file__')), filename), "r", encoding="utf8") as f:
2121
lines = f.readlines()
2222
return {value: key for (key, value) in enumerate(list(map(lambda x: x.strip(), lines)))}

transformer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66

77
class Transformer(object):
8-
def __init__(self, xml_dir, out_dir):
8+
def __init__(self, xml_dir, out_dir, class_file):
99
self.xml_dir = xml_dir
1010
self.out_dir = out_dir
11+
self.class_file = class_file
1112

1213
def transform(self):
1314
reader = Reader(xml_dir=self.xml_dir)
1415
xml_files = reader.get_xml_files()
15-
classes = reader.get_classes()
16+
classes = reader.get_classes(self.class_file)
1617
object_mapper = ObjectMapper()
1718
annotations = object_mapper.bind_files(xml_files, xml_dir=self.xml_dir)
1819
self.write_to_txt(annotations, classes)

xmltotxt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def main():
1010
parser = argparse.ArgumentParser(description="Formatter from ImageNet xml to Darknet text format")
1111
parser.add_argument("-xml", help="Relative location of xml files directory", required=True)
1212
parser.add_argument("-out", help="Relative location of output txt files directory", default="out")
13+
parser.add_argument("-c", help="Relative path to classes file", default="classes.txt")
1314
args = parser.parse_args()
1415

1516
xml_dir = os.path.join(os.path.dirname(os.path.realpath('__file__')), args.xml)
@@ -22,10 +23,20 @@ def main():
2223
os.makedirs(out_dir)
2324

2425
if not os.access(out_dir, os.W_OK):
25-
print("%s folder is not writeable.")
26+
print("%s folder is not writeable." % out_dir)
2627
sys.exit()
28+
29+
class_file = os.path.join(os.path.dirname(os.path.realpath('__file__')), args.c)
2730

28-
transformer = Transformer(xml_dir=xml_dir, out_dir=out_dir)
31+
if not os.access(class_file, os.F_OK):
32+
print("%s file is missing." % class_file)
33+
sys.exit()
34+
35+
if not os.access(class_file, os.R_OK):
36+
print("%s file is not readable." % class_file)
37+
sys.exit()
38+
39+
transformer = Transformer(xml_dir=xml_dir, out_dir=out_dir, class_file=class_file)
2940
transformer.transform()
3041

3142

0 commit comments

Comments
 (0)