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* cscope*
.ycm_extra_conf.py .ycm_extra_conf.py
.subvimrc .subvimrc
.vscode

View File

@ -33,14 +33,15 @@ class LabelFile(object):
imgFolderPath = os.path.dirname(imagePath) imgFolderPath = os.path.dirname(imagePath)
imgFolderName = os.path.split(imgFolderPath)[-1] imgFolderName = os.path.split(imgFolderPath)[-1]
imgFileName = os.path.basename(imagePath) 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 # Read from file path because self.imageData might be empty if saving to
# Pascal format # Pascal format
image = QImage() image = QImage()
image.load(imagePath) image.load(imagePath)
imageShape = [image.height(), image.width(), imageShape = [image.height(), image.width(),
1 if image.isGrayscale() else 3] 1 if image.isGrayscale() else 3]
writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt, writer = PascalVocWriter(imgFolderName, imgFileName,
imageShape, localImgPath=imagePath) imageShape, localImgPath=imagePath)
writer.verified = self.verified writer.verified = self.verified

View File

@ -7,7 +7,7 @@ from lxml import etree
import codecs import codecs
XML_EXT = '.xml' XML_EXT = '.xml'
ENCODE_METHOD = 'utf-8'
class PascalVocWriter: class PascalVocWriter:
@ -20,14 +20,16 @@ class PascalVocWriter:
self.localImgPath = localImgPath self.localImgPath = localImgPath
self.verified = False self.verified = False
def prettify(self, elem): def prettify(self, elem):
""" """
Return a pretty-printed XML string for the Element. Return a pretty-printed XML string for the Element.
""" """
rough_string = ElementTree.tostring(elem, 'utf8') rough_string = ElementTree.tostring(elem, 'utf8')
root = etree.fromstring(rough_string) 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): def genXML(self):
""" """
@ -48,8 +50,9 @@ class PascalVocWriter:
filename = SubElement(top, 'filename') filename = SubElement(top, 'filename')
filename.text = self.filename filename.text = self.filename
localImgPath = SubElement(top, 'path') if self.localImgPath is not None:
localImgPath.text = self.localImgPath localImgPath = SubElement(top, 'path')
localImgPath.text = self.localImgPath
source = SubElement(top, 'source') source = SubElement(top, 'source')
database = SubElement(source, 'database') database = SubElement(source, 'database')
@ -112,9 +115,9 @@ class PascalVocWriter:
out_file = None out_file = None
if targetFile is None: if targetFile is None:
out_file = codecs.open( out_file = codecs.open(
self.filename + XML_EXT, 'w', encoding='utf-8') self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD)
else: else:
out_file = codecs.open(targetFile, 'w', encoding='utf-8') out_file = codecs.open(targetFile, 'w', encoding=ENCODE_METHOD)
prettifyResult = self.prettify(root) prettifyResult = self.prettify(root)
out_file.write(prettifyResult.decode('utf8')) out_file.write(prettifyResult.decode('utf8'))
@ -144,7 +147,7 @@ class PascalVocReader:
def parseXML(self): def parseXML(self):
assert self.filepath.endswith(XML_EXT), "Unsupport file format" 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() xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
filename = xmltree.find('filename').text filename = xmltree.find('filename').text
try: try: