This commit is contained in:
tzutalin 2017-07-07 11:09:00 +08:00
parent 0efb50ac62
commit 27cf04aecc
3 changed files with 15 additions and 10 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ tags
cscope*
.ycm_extra_conf.py
.subvimrc
.vscode

View File

@ -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

View File

@ -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: