make py2 unicode-handling py3-compatibe

This commit is contained in:
Ryan Flynn 2016-12-31 14:53:09 -05:00
parent 67a8583e22
commit c7ddf4a3ef
2 changed files with 39 additions and 25 deletions

View File

@ -29,6 +29,15 @@ __appname__ = 'labelImg'
### Utility functions and classes. ### Utility functions and classes.
def u(x):
'''unicode helper'''
try:
return x.decode('utf8') # py2
except AttributeError:
return x # py3
class WindowMixin(object): class WindowMixin(object):
def menu(self, title, actions=None): def menu(self, title, actions=None):
menu = self.menuBar().addMenu(title) menu = self.menuBar().addMenu(title)
@ -342,8 +351,8 @@ class MainWindow(QMainWindow, WindowMixin):
self.move(position) self.move(position)
saveDir = settings.get('savedir', None) saveDir = settings.get('savedir', None)
self.lastOpenDir = settings.get('lastOpenDir', None) self.lastOpenDir = settings.get('lastOpenDir', None)
if os.path.exists(unicode(saveDir)): if os.path.exists(str(saveDir)):
self.defaultSaveDir = unicode(saveDir) self.defaultSaveDir = str(saveDir)
self.statusBar().showMessage('%s started. Annotation will be saved to %s' %(__appname__, self.defaultSaveDir)) self.statusBar().showMessage('%s started. Annotation will be saved to %s' %(__appname__, self.defaultSaveDir))
self.statusBar().show() self.statusBar().show()
@ -493,7 +502,7 @@ class MainWindow(QMainWindow, WindowMixin):
def updateFileMenu(self): def updateFileMenu(self):
current = self.filename current = self.filename
def exists(filename): def exists(filename):
return os.path.exists(unicode(filename)) return os.path.exists(filename)
menu = self.menus.recentFiles menu = self.menus.recentFiles
menu.clear() menu.clear()
files = [f for f in self.recentFiles if f != current and exists(f)] files = [f for f in self.recentFiles if f != current and exists(f)]
@ -518,7 +527,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(str(item.text())) currIndex = self.mImgList.index(item.text())
if currIndex < len(self.mImgList): if currIndex < len(self.mImgList):
filename = self.mImgList[currIndex] filename = self.mImgList[currIndex]
if filename: if filename:
@ -574,7 +583,7 @@ class MainWindow(QMainWindow, WindowMixin):
def saveLabels(self, filename): def saveLabels(self, filename):
lf = LabelFile() lf = LabelFile()
def format_shape(s): def format_shape(s):
return dict(label=unicode(s.label), return dict(label=s.label,
line_color=s.line_color.getRgb()\ line_color=s.line_color.getRgb()\
if s.line_color != self.lineColor else None, if s.line_color != self.lineColor else None,
fill_color=s.fill_color.getRgb()\ fill_color=s.fill_color.getRgb()\
@ -586,10 +595,10 @@ class MainWindow(QMainWindow, WindowMixin):
try: try:
if self.usingPascalVocFormat is True: if self.usingPascalVocFormat is True:
print('savePascalVocFormat save to:' + filename) print('savePascalVocFormat save to:' + filename)
lf.savePascalVocFormat(filename, shapes, unicode(self.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, unicode(self.filename), self.imageData, lf.save(filename, shapes, str(self.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
@ -612,9 +621,9 @@ class MainWindow(QMainWindow, WindowMixin):
def labelItemChanged(self, item): def labelItemChanged(self, item):
shape = self.itemsToShapes[item] shape = self.itemsToShapes[item]
label = unicode(item.text()) label = item.text()
if label != shape.label: if label != shape.label:
shape.label = unicode(item.text()) shape.label = item.text()
self.setDirty() self.setDirty()
else: # User probably changed item visibility else: # User probably changed item visibility
self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked) self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked)
@ -686,7 +695,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.canvas.setEnabled(False) self.canvas.setEnabled(False)
if filename is None: if filename is None:
filename = self.settings['filename'] filename = self.settings['filename']
filename = unicode(filename) filename = filename
# Tzutalin 20160906 : Add file list and dock to move faster # Tzutalin 20160906 : Add file list and dock to move faster
# Highlight the file item # Highlight the file item
@ -720,7 +729,7 @@ 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(unicode(filename))) self.status("Loaded %s" % os.path.basename(str(filename)))
self.image = image self.image = image
self.filename = filename self.filename = filename
self.canvas.loadPixmap(QPixmap.fromImage(image)) self.canvas.loadPixmap(QPixmap.fromImage(image))
@ -820,17 +829,18 @@ class MainWindow(QMainWindow, WindowMixin):
for file in files: for file in files:
if file.lower().endswith(tuple(extensions)): if file.lower().endswith(tuple(extensions)):
relatviePath = os.path.join(root, file) relatviePath = os.path.join(root, file)
images.append(os.path.abspath(relatviePath)) path = u(os.path.abspath(relatviePath))
images.append(path)
images.sort(key=lambda x: x.lower()) images.sort(key=lambda x: x.lower())
return images return images
def changeSavedir(self, _value=False): def changeSavedir(self, _value=False):
if self.defaultSaveDir is not None: if self.defaultSaveDir is not None:
path = unicode(self.defaultSaveDir) path = str(self.defaultSaveDir)
else: else:
path = '.' path = '.'
dirpath = unicode(QFileDialog.getExistingDirectory(self, dirpath = str(QFileDialog.getExistingDirectory(self,
'%s - Save to the directory' % __appname__, path, QFileDialog.ShowDirsOnly '%s - Save to the directory' % __appname__, path, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks)) | QFileDialog.DontResolveSymlinks))
@ -844,14 +854,14 @@ class MainWindow(QMainWindow, WindowMixin):
if self.filename is None: if self.filename is None:
return return
path = os.path.dirname(unicode(self.filename))\ path = os.path.dirname(str(self.filename))\
if self.filename else '.' if self.filename else '.'
if self.usingPascalVocFormat: if self.usingPascalVocFormat:
formats = ['*.%s' % unicode(fmt).lower()\ formats = ['*.%s' % str(fmt).lower()\
for fmt in QImageReader.supportedImageFormats()] for fmt in QImageReader.supportedImageFormats()]
filters = "Open Annotation XML file (%s)" % \ filters = "Open Annotation XML file (%s)" % \
' '.join(formats + ['*.xml']) ' '.join(formats + ['*.xml'])
filename = unicode(QFileDialog.getOpenFileName(self, filename = str(QFileDialog.getOpenFileName(self,
'%s - Choose a xml file' % __appname__, path, filters)) '%s - Choose a xml file' % __appname__, path, filters))
self.loadPascalXMLByFilename(filename) self.loadPascalXMLByFilename(filename)
@ -859,13 +869,13 @@ class MainWindow(QMainWindow, WindowMixin):
if not self.mayContinue(): if not self.mayContinue():
return return
path = os.path.dirname(unicode(self.filename))\ path = os.path.dirname(self.filename)\
if self.filename else '.' if self.filename else '.'
if self.lastOpenDir is not None and len(self.lastOpenDir) > 1: if self.lastOpenDir is not None and len(self.lastOpenDir) > 1:
path = self.lastOpenDir path = self.lastOpenDir
dirpath = unicode(QFileDialog.getExistingDirectory(self, dirpath = str(QFileDialog.getExistingDirectory(self,
'%s - Open Directory' % __appname__, path, QFileDialog.ShowDirsOnly '%s - Open Directory' % __appname__, path, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks)) | QFileDialog.DontResolveSymlinks))
@ -921,13 +931,13 @@ class MainWindow(QMainWindow, WindowMixin):
def openFile(self, _value=False): def openFile(self, _value=False):
if not self.mayContinue(): if not self.mayContinue():
return return
path = os.path.dirname(unicode(self.filename))\ path = os.path.dirname(str(self.filename))\
if self.filename else '.' if self.filename else '.'
formats = ['*.%s' % unicode(fmt).lower()\ formats = ['*.%s' % str(fmt).lower()\
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 = unicode(QFileDialog.getOpenFileName(self, filename = str(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)
@ -1002,7 +1012,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(unicode(self.filename)) if self.filename else '.' return os.path.dirname(str(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',

View File

@ -74,7 +74,11 @@ class PascalVocWriter:
for each_object in self.boxlist: for each_object in self.boxlist:
object_item = SubElement(top, 'object') object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name') name = SubElement(object_item, 'name')
try:
name.text = unicode(each_object['name']) name.text = unicode(each_object['name'])
except NameError:
# Py3: NameError: name 'unicode' is not defined
name.text = each_object['name']
pose = SubElement(object_item, 'pose') pose = SubElement(object_item, 'pose')
pose.text = "Unspecified" pose.text = "Unspecified"
truncated = SubElement(object_item, 'truncated') truncated = SubElement(object_item, 'truncated')
@ -101,7 +105,7 @@ class PascalVocWriter:
out_file = open(targetFile, 'w') out_file = open(targetFile, 'w')
prettifyResult = self.prettify(root) prettifyResult = self.prettify(root)
out_file.write(prettifyResult) out_file.write(prettifyResult.decode('utf8'))
out_file.close() out_file.close()