Revert "Fix encode filename issues python2"

This reverts commit 62507a195829e3f0eb93a390997e91033cb52e47.
This commit is contained in:
tzutalin 2017-02-15 20:35:57 +08:00
parent 62507a1958
commit 8239d1811b
2 changed files with 29 additions and 28 deletions

View File

@ -43,9 +43,9 @@ __appname__ = 'labelImg'
def u(x):
'''py2/py3 unicode helper'''
try:
return unicode(x) # py2
return x.decode('utf8') # py2
except AttributeError:
return str(x) # py3
return x # py3
def have_qstring():
@ -399,8 +399,8 @@ class MainWindow(QMainWindow, WindowMixin):
self.move(position)
saveDir = settings.get('savedir', None)
self.lastOpenDir = settings.get('lastOpenDir', None)
if os.path.exists(u(saveDir)):
self.defaultSaveDir = u(saveDir)
if os.path.exists(str(saveDir)):
self.defaultSaveDir = str(saveDir)
self.statusBar().showMessage('%s started. Annotation will be saved to %s' %(__appname__, self.defaultSaveDir))
self.statusBar().show()
@ -556,7 +556,7 @@ class MainWindow(QMainWindow, WindowMixin):
def updateFileMenu(self):
current = self.filename
def exists(filename):
return os.path.exists(u(filename))
return os.path.exists(filename)
menu = self.menus.recentFiles
menu.clear()
files = [f for f in self.recentFiles if f != current and exists(f)]
@ -581,7 +581,7 @@ class MainWindow(QMainWindow, WindowMixin):
# Tzutalin 20160906 : Add file list and dock to move faster
def fileitemDoubleClicked(self, item=None):
currIndex = self.mImgList.index(u(item.text()))
currIndex = self.mImgList.index(item.text())
if currIndex < len(self.mImgList):
filename = self.mImgList[currIndex]
if filename:
@ -637,7 +637,7 @@ class MainWindow(QMainWindow, WindowMixin):
def saveLabels(self, filename):
lf = LabelFile()
def format_shape(s):
return dict(label=u(s.label),
return dict(label=s.label,
line_color=s.line_color.getRgb()\
if s.line_color != self.lineColor else None,
fill_color=s.fill_color.getRgb()\
@ -649,10 +649,10 @@ class MainWindow(QMainWindow, WindowMixin):
try:
if self.usingPascalVocFormat is True:
print('savePascalVocFormat save to:' + filename)
lf.savePascalVocFormat(filename, shapes, u(self.filename), self.imageData,
lf.savePascalVocFormat(filename, shapes, str(self.filename), self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
else:
lf.save(filename, shapes, u(self.filename), self.imageData,
lf.save(filename, shapes, str(self.filename), self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
self.labelFile = lf
self.filename = filename
@ -675,9 +675,9 @@ class MainWindow(QMainWindow, WindowMixin):
def labelItemChanged(self, item):
shape = self.itemsToShapes[item]
label = u(item.text())
label = item.text()
if label != shape.label:
shape.label = u(item.text())
shape.label = item.text()
self.setDirty()
else: # User probably changed item visibility
self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked)
@ -748,7 +748,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.resetState()
self.canvas.setEnabled(False)
if filename is None:
filename = u(self.settings.get('filename'))
filename = self.settings.get('filename')
# Tzutalin 20160906 : Add file list and dock to move faster
# Highlight the file item
@ -782,9 +782,9 @@ class MainWindow(QMainWindow, WindowMixin):
u"<p>Make sure <i>%s</i> is a valid image file." % filename)
self.status("Error reading %s" % filename)
return False
self.status("Loaded %s" % os.path.basename(u(filename)))
self.status("Loaded %s" % os.path.basename(str(filename)))
self.image = image
self.filename = u(filename)
self.filename = filename
self.canvas.loadPixmap(QPixmap.fromImage(image))
if self.labelFile:
self.loadLabels(self.labelFile.shapes)
@ -886,11 +886,11 @@ class MainWindow(QMainWindow, WindowMixin):
def changeSavedir(self, _value=False):
if self.defaultSaveDir is not None:
path = u(self.defaultSaveDir)
path = str(self.defaultSaveDir)
else:
path = u'.'
path = '.'
dirpath = u(QFileDialog.getExistingDirectory(self,
dirpath = str(QFileDialog.getExistingDirectory(self,
'%s - Save to the directory' % __appname__, path, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks))
@ -904,14 +904,14 @@ class MainWindow(QMainWindow, WindowMixin):
if self.filename is None:
return
path = os.path.dirname(u(self.filename))\
path = os.path.dirname(str(self.filename))\
if self.filename else '.'
if self.usingPascalVocFormat:
formats = ['*.%s' % str(fmt).lower()\
for fmt in QImageReader.supportedImageFormats()]
filters = "Open Annotation XML file (%s)" % \
' '.join(formats + ['*.xml'])
filename = u(QFileDialog.getOpenFileName(self,
filename = str(QFileDialog.getOpenFileName(self,
'%s - Choose a xml file' % __appname__, path, filters))
self.loadPascalXMLByFilename(filename)
@ -919,13 +919,13 @@ class MainWindow(QMainWindow, WindowMixin):
if not self.mayContinue():
return
path = os.path.dirname(u(self.filename))\
path = os.path.dirname(self.filename)\
if self.filename else '.'
if self.lastOpenDir is not None and len(self.lastOpenDir) > 1:
path = self.lastOpenDir
dirpath = u(QFileDialog.getExistingDirectory(self,
dirpath = str(QFileDialog.getExistingDirectory(self,
'%s - Open Directory' % __appname__, path, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks))
@ -981,13 +981,13 @@ class MainWindow(QMainWindow, WindowMixin):
def openFile(self, _value=False):
if not self.mayContinue():
return
path = os.path.dirname(u(self.filename))\
if self.filename else u'.'
path = os.path.dirname(str(self.filename))\
if self.filename else '.'
formats = ['*.%s' % str(fmt).lower()\
for fmt in QImageReader.supportedImageFormats()]
filters = "Image & Label files (%s)" % \
' '.join(formats + ['*%s' % LabelFile.suffix])
filename = u(QFileDialog.getOpenFileName(self,
filename = str(QFileDialog.getOpenFileName(self,
'%s - Choose Image or Label file' % __appname__, path, filters))
if filename:
self.loadFile(filename)
@ -1061,7 +1061,7 @@ class MainWindow(QMainWindow, WindowMixin):
'<p><b>%s</b></p>%s' % (title, message))
def currentPath(self):
return os.path.dirname(u(self.filename)) if self.filename else '.'
return os.path.dirname(str(self.filename)) if self.filename else '.'
def chooseColor1(self):
color = self.colorDialog.getColor(self.lineColor, u'Choose line color',

View File

@ -5,7 +5,8 @@ import sys
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from lxml import etree
import io
import codecs
class PascalVocWriter:
@ -100,9 +101,9 @@ class PascalVocWriter:
self.appendObjects(root)
out_file = None
if targetFile is None:
out_file = io.open(self.filename + '.xml', 'w', encoding='utf-8')
out_file = codecs.open(self.filename + '.xml', 'w', encoding='utf-8')
else:
out_file = io.open(targetFile, 'w', encoding='utf-8')
out_file = codecs.open(targetFile, 'w', encoding='utf-8')
prettifyResult = self.prettify(root)
out_file.write(prettifyResult.decode('utf8'))