Skip to content

Commit

Permalink
Merge pull request #9 from shashwat1998/master
Browse files Browse the repository at this point in the history
Extended tool for subdirectories
  • Loading branch information
isabek authored Jul 6, 2020
2 parents 53f2427 + a68b0d6 commit fe8d4c9
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# XmlToTxt
ImageNet file xml format to [Darknet](https://github.com/pjreddie/darknet) text format.
Works well with directories and subdirectories.

### Installation
```bash
Expand Down Expand Up @@ -41,3 +42,4 @@ Output text file.
### Motivation

I used [Darknet](https://github.com/pjreddie/darknet) for real-time object detection and classification. Sometimes you need to collect your own training dataset for train your model. I collected training dataset images and fine awesome [tool](https://github.com/tzutalin/labelImg) for labeling images. But it generates xml files. So I needed to implement tool which translates from ImageNet xml format to Darknet text format.
Also compatible with latest [YOLOv5](https://github.com/ultralytics/yolov5) by Ultralytics.
12 changes: 7 additions & 5 deletions objectmapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging

import os
import declxml as xml


Expand All @@ -25,14 +25,16 @@ def __init__(self):
xml.string("filename")
])

def bind(self, xml_file_path):
return xml.parse_from_file(self.processor, xml_file_path=xml_file_path)
def bind(self, xml_file_path, xml_dir):
ann = xml.parse_from_file(self.processor, xml_file_path=os.path.join(xml_dir, xml_file_path))
ann.filename = xml_file_path
return ann

def bind_files(self, xml_file_paths):
def bind_files(self, xml_file_paths, xml_dir):
result = []
for xml_file_path in xml_file_paths:
try:
result.append(self.bind(xml_file_path=xml_file_path))
result.append(self.bind(xml_file_path=xml_file_path, xml_dir=xml_dir))
except Exception as e:
logging.error("%s", e.args)
return result
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ def __init__(self, xml_dir):

def get_xml_files(self):
xml_filenames = []
for xml_filename in os.listdir(self.xml_dir):
if xml_filename.endswith(".xml"):
xml_filenames.append(os.path.join(self.xml_dir, xml_filename))
for root, subdirectories, files in os.walk(self.xml_dir):
for filename in files:
if filename.endswith(".xml"):
file_path = os.path.join(root, filename)
file_path = os.path.relpath(file_path, start=self.xml_dir)
xml_filenames.append(file_path)
return xml_filenames

@staticmethod
Expand Down
7 changes: 5 additions & 2 deletions transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ def transform(self):
xml_files = reader.get_xml_files()
classes = reader.get_classes()
object_mapper = ObjectMapper()
annotations = object_mapper.bind_files(xml_files)
annotations = object_mapper.bind_files(xml_files, xml_dir=self.xml_dir)
self.write_to_txt(annotations, classes)

def write_to_txt(self, annotations, classes):
for annotation in annotations:
with open(os.path.join(self.out_dir, self.darknet_filename_format(annotation.filename)), "w+") as f:
output_path = os.path.join(self.out_dir, self.darknet_filename_format(annotation.filename))
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
with open(output_path, "w+") as f:
f.write(self.to_darknet_format(annotation, classes))

def to_darknet_format(self, annotation, classes):
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit fe8d4c9

Please sign in to comment.