Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Fix qstring/asic code convert for python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tzutalin committed Feb 16, 2017
1 parent 8239d18 commit 70721ad
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions labelImg.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@

def u(x):
'''py2/py3 unicode helper'''
try:
return x.decode('utf8') # py2
except AttributeError:
if sys.version_info < (3, 0, 0):
if type(x) == str:
return x.decode('utf-8')
if type(x) == QString:
return unicode(x)
return x
else:
return x # py3


def have_qstring():
'''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.'))
Expand Down Expand Up @@ -350,7 +353,7 @@ def __init__(self, filename=None):

# Application state.
self.image = QImage()
self.filename = filename
self.filename = u(filename)
self.recentFiles = []
self.maxRecent = 7
self.lineColor = None
Expand Down Expand Up @@ -581,7 +584,7 @@ def editLabel(self, item=None):

# 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:
Expand Down Expand Up @@ -647,12 +650,12 @@ def format_shape(s):
shapes = [format_shape(shape) for shape in self.canvas.shapes]
# Can add differrent annotation formats here
try:
filename = u(filename)
if self.usingPascalVocFormat is True:
print('savePascalVocFormat save to:' + filename)
lf.savePascalVocFormat(filename, shapes, str(self.filename), self.imageData,
lf.savePascalVocFormat(filename, shapes, filename, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
else:
lf.save(filename, shapes, str(self.filename), self.imageData,
lf.save(filename, shapes, filename, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
self.labelFile = lf
self.filename = filename
Expand Down Expand Up @@ -758,6 +761,7 @@ def loadFile(self, filename=None):
fileWidgetItem.setSelected(True)

if filename and QFile.exists(filename):
filename = u(filename)
if LabelFile.isLabelFile(filename):
try:
self.labelFile = LabelFile(filename)
Expand All @@ -782,9 +786,9 @@ def loadFile(self, filename=None):
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(str(filename)))
self.status("Loaded %s" % os.path.basename(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)
Expand Down Expand Up @@ -904,7 +908,7 @@ def openAnnotation(self, _value=False):
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()\
Expand Down Expand Up @@ -987,8 +991,8 @@ def openFile(self, _value=False):
for fmt in QImageReader.supportedImageFormats()]
filters = "Image & Label files (%s)" % \
' '.join(formats + ['*%s' % LabelFile.suffix])
filename = str(QFileDialog.getOpenFileName(self,
'%s - Choose Image or Label file' % __appname__, path, filters))
filename = QFileDialog.getOpenFileName(self,
'%s - Choose Image or Label file' % __appname__, path, filters)
if filename:
self.loadFile(filename)

Expand Down Expand Up @@ -1061,7 +1065,7 @@ def errorMessage(self, title, message):
'<p><b>%s</b></p>%s' % (title, message))

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):
color = self.colorDialog.getColor(self.lineColor, u'Choose line color',
Expand Down

0 comments on commit 70721ad

Please sign in to comment.