Fix qstring/asic code convert for python 2

This commit is contained in:
tzutalin 2017-02-16 11:00:49 +08:00
parent 8239d1811b
commit 70721ad597

View File

@ -42,12 +42,15 @@ __appname__ = 'labelImg'
def u(x): def u(x):
'''py2/py3 unicode helper''' '''py2/py3 unicode helper'''
try: if sys.version_info < (3, 0, 0):
return x.decode('utf8') # py2 if type(x) == str:
except AttributeError: return x.decode('utf-8')
if type(x) == QString:
return unicode(x)
return x
else:
return x # py3 return x # py3
def have_qstring(): def have_qstring():
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type''' '''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.')) return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
@ -350,7 +353,7 @@ class MainWindow(QMainWindow, WindowMixin):
# Application state. # Application state.
self.image = QImage() self.image = QImage()
self.filename = filename self.filename = u(filename)
self.recentFiles = [] self.recentFiles = []
self.maxRecent = 7 self.maxRecent = 7
self.lineColor = None self.lineColor = None
@ -581,7 +584,7 @@ class MainWindow(QMainWindow, WindowMixin):
# Tzutalin 20160906 : Add file list and dock to move faster # Tzutalin 20160906 : Add file list and dock to move faster
def fileitemDoubleClicked(self, item=None): def fileitemDoubleClicked(self, item=None):
currIndex = self.mImgList.index(item.text()) currIndex = self.mImgList.index(u(item.text()))
if currIndex < len(self.mImgList): if currIndex < len(self.mImgList):
filename = self.mImgList[currIndex] filename = self.mImgList[currIndex]
if filename: if filename:
@ -647,12 +650,12 @@ class MainWindow(QMainWindow, WindowMixin):
shapes = [format_shape(shape) for shape in self.canvas.shapes] shapes = [format_shape(shape) for shape in self.canvas.shapes]
# Can add differrent annotation formats here # Can add differrent annotation formats here
try: try:
filename = u(filename)
if self.usingPascalVocFormat is True: if self.usingPascalVocFormat is True:
print('savePascalVocFormat save to:' + filename) lf.savePascalVocFormat(filename, shapes, filename, self.imageData,
lf.savePascalVocFormat(filename, shapes, str(self.filename), self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb()) self.lineColor.getRgb(), self.fillColor.getRgb())
else: else:
lf.save(filename, shapes, str(self.filename), self.imageData, lf.save(filename, shapes, filename, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb()) self.lineColor.getRgb(), self.fillColor.getRgb())
self.labelFile = lf self.labelFile = lf
self.filename = filename self.filename = filename
@ -758,6 +761,7 @@ class MainWindow(QMainWindow, WindowMixin):
fileWidgetItem.setSelected(True) fileWidgetItem.setSelected(True)
if filename and QFile.exists(filename): if filename and QFile.exists(filename):
filename = u(filename)
if LabelFile.isLabelFile(filename): if LabelFile.isLabelFile(filename):
try: try:
self.labelFile = LabelFile(filename) self.labelFile = LabelFile(filename)
@ -782,9 +786,9 @@ class MainWindow(QMainWindow, WindowMixin):
u"<p>Make sure <i>%s</i> is a valid image file." % filename) u"<p>Make sure <i>%s</i> is a valid image file." % filename)
self.status("Error reading %s" % filename) self.status("Error reading %s" % filename)
return False return False
self.status("Loaded %s" % os.path.basename(str(filename))) self.status("Loaded %s" % os.path.basename(filename))
self.image = image self.image = image
self.filename = filename self.filename = u(filename)
self.canvas.loadPixmap(QPixmap.fromImage(image)) self.canvas.loadPixmap(QPixmap.fromImage(image))
if self.labelFile: if self.labelFile:
self.loadLabels(self.labelFile.shapes) self.loadLabels(self.labelFile.shapes)
@ -904,7 +908,7 @@ class MainWindow(QMainWindow, WindowMixin):
if self.filename is None: if self.filename is None:
return return
path = os.path.dirname(str(self.filename))\ path = os.path.dirname(u(self.filename))\
if self.filename else '.' if self.filename else '.'
if self.usingPascalVocFormat: if self.usingPascalVocFormat:
formats = ['*.%s' % str(fmt).lower()\ formats = ['*.%s' % str(fmt).lower()\
@ -987,8 +991,8 @@ class MainWindow(QMainWindow, WindowMixin):
for fmt in QImageReader.supportedImageFormats()] for fmt in QImageReader.supportedImageFormats()]
filters = "Image & Label files (%s)" % \ filters = "Image & Label files (%s)" % \
' '.join(formats + ['*%s' % LabelFile.suffix]) ' '.join(formats + ['*%s' % LabelFile.suffix])
filename = str(QFileDialog.getOpenFileName(self, filename = QFileDialog.getOpenFileName(self,
'%s - Choose Image or Label file' % __appname__, path, filters)) '%s - Choose Image or Label file' % __appname__, path, filters)
if filename: if filename:
self.loadFile(filename) self.loadFile(filename)
@ -1061,7 +1065,7 @@ class MainWindow(QMainWindow, WindowMixin):
'<p><b>%s</b></p>%s' % (title, message)) '<p><b>%s</b></p>%s' % (title, message))
def currentPath(self): def currentPath(self):
return os.path.dirname(str(self.filename)) if self.filename else '.' return os.path.dirname(self.filename) if self.filename else '.'
def chooseColor1(self): def chooseColor1(self):
color = self.colorDialog.getColor(self.lineColor, u'Choose line color', color = self.colorDialog.getColor(self.lineColor, u'Choose line color',