From 27cf04aeccfe58fc7f58ea315b68b9f103c1114e Mon Sep 17 00:00:00 2001 From: tzutalin Date: Fri, 7 Jul 2017 11:09:00 +0800 Subject: [PATCH] Fix issue #96 --- .gitignore | 1 + libs/labelFile.py | 5 +++-- libs/pascal_voc_io.py | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 64cefe08..278fd72d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ tags cscope* .ycm_extra_conf.py .subvimrc +.vscode diff --git a/libs/labelFile.py b/libs/labelFile.py index 437ee033..509ab9d6 100644 --- a/libs/labelFile.py +++ b/libs/labelFile.py @@ -33,14 +33,15 @@ class LabelFile(object): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) - imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] + print imgFileName + #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] - writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt, + writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified diff --git a/libs/pascal_voc_io.py b/libs/pascal_voc_io.py index eefb3459..15aac581 100644 --- a/libs/pascal_voc_io.py +++ b/libs/pascal_voc_io.py @@ -7,7 +7,7 @@ from lxml import etree import codecs XML_EXT = '.xml' - +ENCODE_METHOD = 'utf-8' class PascalVocWriter: @@ -20,14 +20,16 @@ class PascalVocWriter: self.localImgPath = localImgPath self.verified = False - def prettify(self, elem): """ Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf8') root = etree.fromstring(rough_string) - return etree.tostring(root, pretty_print=True) + return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(' ', '\t') + # minidom does not support UTF-8 + '''reparsed = minidom.parseString(rough_string) + return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)''' def genXML(self): """ @@ -48,8 +50,9 @@ class PascalVocWriter: filename = SubElement(top, 'filename') filename.text = self.filename - localImgPath = SubElement(top, 'path') - localImgPath.text = self.localImgPath + if self.localImgPath is not None: + localImgPath = SubElement(top, 'path') + localImgPath.text = self.localImgPath source = SubElement(top, 'source') database = SubElement(source, 'database') @@ -112,9 +115,9 @@ class PascalVocWriter: out_file = None if targetFile is None: out_file = codecs.open( - self.filename + XML_EXT, 'w', encoding='utf-8') + self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD) else: - out_file = codecs.open(targetFile, 'w', encoding='utf-8') + out_file = codecs.open(targetFile, 'w', encoding=ENCODE_METHOD) prettifyResult = self.prettify(root) out_file.write(prettifyResult.decode('utf8')) @@ -144,7 +147,7 @@ class PascalVocReader: def parseXML(self): assert self.filepath.endswith(XML_EXT), "Unsupport file format" - parser = etree.XMLParser(encoding='utf-8') + parser = etree.XMLParser(encoding=ENCODE_METHOD) xmltree = ElementTree.parse(self.filepath, parser=parser).getroot() filename = xmltree.find('filename').text try: