Revert "Fix encode filename issues python2"
This reverts commit 62507a195829e3f0eb93a390997e91033cb52e47.
This commit is contained in:
parent
62507a1958
commit
8239d1811b
50
labelImg.py
50
labelImg.py
@ -43,9 +43,9 @@ __appname__ = 'labelImg'
|
|||||||
def u(x):
|
def u(x):
|
||||||
'''py2/py3 unicode helper'''
|
'''py2/py3 unicode helper'''
|
||||||
try:
|
try:
|
||||||
return unicode(x) # py2
|
return x.decode('utf8') # py2
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return str(x) # py3
|
return x # py3
|
||||||
|
|
||||||
|
|
||||||
def have_qstring():
|
def have_qstring():
|
||||||
@ -399,8 +399,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(u(saveDir)):
|
if os.path.exists(str(saveDir)):
|
||||||
self.defaultSaveDir = u(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()
|
||||||
|
|
||||||
@ -556,7 +556,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(u(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)]
|
||||||
@ -581,7 +581,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(u(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:
|
||||||
@ -637,7 +637,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=u(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()\
|
||||||
@ -649,10 +649,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, u(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, u(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
|
||||||
@ -675,9 +675,9 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
|
|
||||||
def labelItemChanged(self, item):
|
def labelItemChanged(self, item):
|
||||||
shape = self.itemsToShapes[item]
|
shape = self.itemsToShapes[item]
|
||||||
label = u(item.text())
|
label = item.text()
|
||||||
if label != shape.label:
|
if label != shape.label:
|
||||||
shape.label = u(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)
|
||||||
@ -748,7 +748,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.resetState()
|
self.resetState()
|
||||||
self.canvas.setEnabled(False)
|
self.canvas.setEnabled(False)
|
||||||
if filename is None:
|
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
|
# Tzutalin 20160906 : Add file list and dock to move faster
|
||||||
# Highlight the file item
|
# 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)
|
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(u(filename)))
|
self.status("Loaded %s" % os.path.basename(str(filename)))
|
||||||
self.image = image
|
self.image = image
|
||||||
self.filename = u(filename)
|
self.filename = 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)
|
||||||
@ -886,11 +886,11 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
|
|
||||||
def changeSavedir(self, _value=False):
|
def changeSavedir(self, _value=False):
|
||||||
if self.defaultSaveDir is not None:
|
if self.defaultSaveDir is not None:
|
||||||
path = u(self.defaultSaveDir)
|
path = str(self.defaultSaveDir)
|
||||||
else:
|
else:
|
||||||
path = u'.'
|
path = '.'
|
||||||
|
|
||||||
dirpath = u(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))
|
||||||
|
|
||||||
@ -904,14 +904,14 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
if self.filename is None:
|
if self.filename is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
path = os.path.dirname(u(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' % str(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 = u(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)
|
||||||
|
|
||||||
@ -919,13 +919,13 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
if not self.mayContinue():
|
if not self.mayContinue():
|
||||||
return
|
return
|
||||||
|
|
||||||
path = os.path.dirname(u(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 = u(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))
|
||||||
|
|
||||||
@ -981,13 +981,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(u(self.filename))\
|
path = os.path.dirname(str(self.filename))\
|
||||||
if self.filename else u'.'
|
if self.filename else '.'
|
||||||
formats = ['*.%s' % str(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 = u(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)
|
||||||
@ -1061,7 +1061,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(u(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',
|
||||||
|
|||||||
@ -5,7 +5,8 @@ import sys
|
|||||||
from xml.etree import ElementTree
|
from xml.etree import ElementTree
|
||||||
from xml.etree.ElementTree import Element, SubElement
|
from xml.etree.ElementTree import Element, SubElement
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
import io
|
import codecs
|
||||||
|
|
||||||
|
|
||||||
class PascalVocWriter:
|
class PascalVocWriter:
|
||||||
|
|
||||||
@ -100,9 +101,9 @@ class PascalVocWriter:
|
|||||||
self.appendObjects(root)
|
self.appendObjects(root)
|
||||||
out_file = None
|
out_file = None
|
||||||
if targetFile is 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:
|
else:
|
||||||
out_file = io.open(targetFile, 'w', encoding='utf-8')
|
out_file = codecs.open(targetFile, 'w', encoding='utf-8')
|
||||||
|
|
||||||
prettifyResult = self.prettify(root)
|
prettifyResult = self.prettify(root)
|
||||||
out_file.write(prettifyResult.decode('utf8'))
|
out_file.write(prettifyResult.decode('utf8'))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user