diff --git a/labelImg.py b/labelImg.py index 43033eb7..08f592d2 100755 --- a/labelImg.py +++ b/labelImg.py @@ -43,9 +43,9 @@ __appname__ = 'labelImg' def u(x): '''py2/py3 unicode helper''' try: - return x.decode('utf8') # py2 + return unicode(x) # py2 except AttributeError: - return x # py3 + return str(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(str(saveDir)): - self.defaultSaveDir = str(saveDir) + if os.path.exists(u(saveDir)): + self.defaultSaveDir = u(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(filename) + return os.path.exists(u(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(item.text()) + currIndex = self.mImgList.index(u(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=s.label, + return dict(label=u(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, str(self.filename), self.imageData, + lf.savePascalVocFormat(filename, shapes, u(self.filename), self.imageData, self.lineColor.getRgb(), self.fillColor.getRgb()) else: - lf.save(filename, shapes, str(self.filename), self.imageData, + lf.save(filename, shapes, u(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 = item.text() + label = u(item.text()) if label != shape.label: - shape.label = item.text() + shape.label = u(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 = self.settings.get('filename') + filename = u(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"
Make sure %s is a valid image file." % filename) self.status("Error reading %s" % filename) return False - self.status("Loaded %s" % os.path.basename(str(filename))) + self.status("Loaded %s" % os.path.basename(u(filename))) self.image = image - self.filename = filename + self.filename = u(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 = str(self.defaultSaveDir) + path = u(self.defaultSaveDir) else: - path = '.' + path = u'.' - dirpath = str(QFileDialog.getExistingDirectory(self, + dirpath = u(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(str(self.filename))\ + path = os.path.dirname(u(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 = str(QFileDialog.getOpenFileName(self, + filename = u(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(self.filename)\ + path = os.path.dirname(u(self.filename))\ if self.filename else '.' if self.lastOpenDir is not None and len(self.lastOpenDir) > 1: path = self.lastOpenDir - dirpath = str(QFileDialog.getExistingDirectory(self, + dirpath = u(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(str(self.filename))\ - if self.filename else '.' + path = os.path.dirname(u(self.filename))\ + if self.filename else u'.' formats = ['*.%s' % str(fmt).lower()\ for fmt in QImageReader.supportedImageFormats()] filters = "Image & Label files (%s)" % \ ' '.join(formats + ['*%s' % LabelFile.suffix]) - filename = str(QFileDialog.getOpenFileName(self, + filename = u(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): '
%s
%s' % (title, message)) def currentPath(self): - return os.path.dirname(str(self.filename)) if self.filename else '.' + return os.path.dirname(u(self.filename)) if self.filename else '.' def chooseColor1(self): color = self.colorDialog.getColor(self.lineColor, u'Choose line color', diff --git a/libs/pascal_voc_io.py b/libs/pascal_voc_io.py index 5ac664e3..edd750a5 100644 --- a/libs/pascal_voc_io.py +++ b/libs/pascal_voc_io.py @@ -5,8 +5,7 @@ import sys from xml.etree import ElementTree from xml.etree.ElementTree import Element, SubElement from lxml import etree -import codecs - +import io class PascalVocWriter: @@ -101,9 +100,9 @@ class PascalVocWriter: self.appendObjects(root) out_file = None if targetFile is None: - out_file = codecs.open(self.filename + '.xml', 'w', encoding='utf-8') + out_file = io.open(self.filename + '.xml', 'w', encoding='utf-8') else: - out_file = codecs.open(targetFile, 'w', encoding='utf-8') + out_file = io.open(targetFile, 'w', encoding='utf-8') prettifyResult = self.prettify(root) out_file.write(prettifyResult.decode('utf8'))