Skip to content

Commit

Permalink
Add -c option to add custom class file (#13)
Browse files Browse the repository at this point in the history
#12 Support custom class files.
  • Loading branch information
shashwat1998 authored Sep 24, 2020
1 parent bcf6e96 commit 3fce087
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ sudo pip install -r requirements.txt
### Usage

```bash
python xmltotxt.py -xml xml -out out
python xmltotxt.py -c cls.txt -xml xml -out out
```
#### Mandatory arguments
-xml
#### Optional arguments
-c, -out

### Example

Expand Down
2 changes: 1 addition & 1 deletion reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_xml_files(self):
return xml_filenames

@staticmethod
def get_classes(filename="classes.txt"):
def get_classes(filename):
with open(os.path.join(os.path.dirname(os.path.realpath('__file__')), filename), "r", encoding="utf8") as f:
lines = f.readlines()
return {value: key for (key, value) in enumerate(list(map(lambda x: x.strip(), lines)))}
5 changes: 3 additions & 2 deletions transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@


class Transformer(object):
def __init__(self, xml_dir, out_dir):
def __init__(self, xml_dir, out_dir, class_file):
self.xml_dir = xml_dir
self.out_dir = out_dir
self.class_file = class_file

def transform(self):
reader = Reader(xml_dir=self.xml_dir)
xml_files = reader.get_xml_files()
classes = reader.get_classes()
classes = reader.get_classes(self.class_file)
object_mapper = ObjectMapper()
annotations = object_mapper.bind_files(xml_files, xml_dir=self.xml_dir)
self.write_to_txt(annotations, classes)
Expand Down
15 changes: 13 additions & 2 deletions xmltotxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def main():
parser = argparse.ArgumentParser(description="Formatter from ImageNet xml to Darknet text format")
parser.add_argument("-xml", help="Relative location of xml files directory", required=True)
parser.add_argument("-out", help="Relative location of output txt files directory", default="out")
parser.add_argument("-c", help="Relative path to classes file", default="classes.txt")
args = parser.parse_args()

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

if not os.access(out_dir, os.W_OK):
print("%s folder is not writeable.")
print("%s folder is not writeable." % out_dir)
sys.exit()

class_file = os.path.join(os.path.dirname(os.path.realpath('__file__')), args.c)

transformer = Transformer(xml_dir=xml_dir, out_dir=out_dir)
if not os.access(class_file, os.F_OK):
print("%s file is missing." % class_file)
sys.exit()

if not os.access(class_file, os.R_OK):
print("%s file is not readable." % class_file)
sys.exit()

transformer = Transformer(xml_dir=xml_dir, out_dir=out_dir, class_file=class_file)
transformer.transform()


Expand Down

0 comments on commit 3fce087

Please sign in to comment.