Add ustr.py, add more test, and fix build error for py3
This commit is contained in:
parent
4ee8287e51
commit
9a7ecb4a07
58
labelImg.py
58
labelImg.py
@ -16,8 +16,8 @@ try:
|
||||
from PyQt5.QtWidgets import *
|
||||
except ImportError:
|
||||
# needed for py3+qt4
|
||||
# ref: http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
|
||||
# ref:
|
||||
# Ref:
|
||||
# http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
|
||||
# http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
|
||||
if sys.version_info.major >= 3:
|
||||
import sip
|
||||
@ -37,24 +37,13 @@ from labelFile import LabelFile, LabelFileError
|
||||
from toolBar import ToolBar
|
||||
from pascal_voc_io import PascalVocReader
|
||||
from pascal_voc_io import XML_EXT
|
||||
from ustr import ustr
|
||||
|
||||
__appname__ = 'labelImg'
|
||||
|
||||
# Utility functions and classes.
|
||||
|
||||
|
||||
def u(x):
|
||||
'''py2/py3 unicode helper'''
|
||||
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.'))
|
||||
@ -378,7 +367,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
|
||||
# Application state.
|
||||
self.image = QImage()
|
||||
self.filePath = u(defaultFilename)
|
||||
self.filePath = ustr(defaultFilename)
|
||||
self.recentFiles = []
|
||||
self.maxRecent = 7
|
||||
self.lineColor = None
|
||||
@ -427,8 +416,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
position = settings.get('window/position', QPoint(0, 0))
|
||||
self.resize(size)
|
||||
self.move(position)
|
||||
saveDir = u(settings.get('savedir', None))
|
||||
self.lastOpenDir = u(settings.get('lastOpenDir', None))
|
||||
saveDir = ustr(settings.get('savedir', None))
|
||||
self.lastOpenDir = ustr(settings.get('lastOpenDir', None))
|
||||
if os.path.exists(saveDir):
|
||||
self.defaultSaveDir = saveDir
|
||||
self.statusBar().showMessage('%s started. Annotation will be saved to %s' %
|
||||
@ -616,7 +605,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
|
||||
# Tzutalin 20160906 : Add file list and dock to move faster
|
||||
def fileitemDoubleClicked(self, item=None):
|
||||
currIndex = self.mImgList.index(u(item.text()))
|
||||
currIndex = self.mImgList.index(ustr(item.text()))
|
||||
if currIndex < len(self.mImgList):
|
||||
filename = self.mImgList[currIndex]
|
||||
if filename:
|
||||
@ -703,7 +692,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
self.canvas.loadShapes(s)
|
||||
|
||||
def saveLabels(self, annotationFilePath):
|
||||
annotationFilePath = u(annotationFilePath)
|
||||
annotationFilePath = ustr(annotationFilePath)
|
||||
if self.labelFile is None:
|
||||
self.labelFile = LabelFile()
|
||||
self.labelFile.verified = self.canvas.verified
|
||||
@ -829,7 +818,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
if filePath is None:
|
||||
filePath = self.settings.get('filename')
|
||||
|
||||
unicodeFilePath = u(filePath)
|
||||
unicodeFilePath = ustr(filePath)
|
||||
# Tzutalin 20160906 : Add file list and dock to move faster
|
||||
# Highlight the file item
|
||||
if unicodeFilePath and self.fileListWidget.count() > 0:
|
||||
@ -949,7 +938,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
s['recentFiles'] = self.recentFiles
|
||||
s['advanced'] = not self._beginner
|
||||
if self.defaultSaveDir is not None and len(self.defaultSaveDir) > 1:
|
||||
s['savedir'] = str(self.defaultSaveDir)
|
||||
s['savedir'] = ustr(self.defaultSaveDir)
|
||||
else:
|
||||
s['savedir'] = ""
|
||||
|
||||
@ -972,18 +961,18 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
for file in files:
|
||||
if file.lower().endswith(tuple(extensions)):
|
||||
relatviePath = os.path.join(root, file)
|
||||
path = u(os.path.abspath(relatviePath))
|
||||
path = ustr(os.path.abspath(relatviePath))
|
||||
images.append(path)
|
||||
images.sort(key=lambda x: x.lower())
|
||||
return images
|
||||
|
||||
def changeSavedir(self, _value=False):
|
||||
if self.defaultSaveDir is not None:
|
||||
path = str(self.defaultSaveDir)
|
||||
path = ustr(self.defaultSaveDir)
|
||||
else:
|
||||
path = '.'
|
||||
|
||||
dirpath = str(QFileDialog.getExistingDirectory(self,
|
||||
dirpath = ustr(QFileDialog.getExistingDirectory(self,
|
||||
'%s - Save to the directory' % __appname__, path, QFileDialog.ShowDirsOnly
|
||||
| QFileDialog.DontResolveSymlinks))
|
||||
|
||||
@ -998,7 +987,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
if self.filePath is None:
|
||||
return
|
||||
|
||||
path = os.path.dirname(u(self.filePath))\
|
||||
path = os.path.dirname(ustr(self.filePath))\
|
||||
if self.filePath else '.'
|
||||
if self.usingPascalVocFormat:
|
||||
filters = "Open Annotation XML file (%s)" % \
|
||||
@ -1019,7 +1008,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
if self.lastOpenDir is not None and len(self.lastOpenDir) > 1:
|
||||
path = self.lastOpenDir
|
||||
|
||||
dirpath = u(QFileDialog.getExistingDirectory(self,
|
||||
dirpath = ustr(QFileDialog.getExistingDirectory(self,
|
||||
'%s - Open Directory' % __appname__, path, QFileDialog.ShowDirsOnly
|
||||
| QFileDialog.DontResolveSymlinks))
|
||||
|
||||
@ -1092,7 +1081,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
def openFile(self, _value=False):
|
||||
if not self.mayContinue():
|
||||
return
|
||||
path = os.path.dirname(u(self.filePath)) if self.filePath else '.'
|
||||
path = os.path.dirname(ustr(self.filePath)) if self.filePath else '.'
|
||||
formats = ['*.%s' % fmt.data().decode("ascii").lower() for fmt in QImageReader.supportedImageFormats()]
|
||||
filters = "Image & Label files (%s)" % ' '.join(formats + ['*%s' % LabelFile.suffix])
|
||||
filename = QFileDialog.getOpenFileName(self, '%s - Choose Image or Label file' % __appname__, path, filters)
|
||||
@ -1102,14 +1091,13 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
self.loadFile(filename)
|
||||
|
||||
def saveFile(self, _value=False):
|
||||
if self.defaultSaveDir is not None and len(str(self.defaultSaveDir)):
|
||||
if self.defaultSaveDir is not None and len(ustr(self.defaultSaveDir)):
|
||||
# print('handle the image:' + self.filePath)
|
||||
imgFileName = os.path.basename(self.filePath)
|
||||
savedFileName = os.path.splitext(
|
||||
imgFileName)[0] + LabelFile.suffix
|
||||
savedPath = os.path.join(
|
||||
str(self.defaultSaveDir), savedFileName)
|
||||
self._saveFile(savedPath)
|
||||
if self.filePath:
|
||||
imgFileName = os.path.basename(self.filePath)
|
||||
savedFileName = os.path.splitext(imgFileName)[0] + LabelFile.suffix
|
||||
savedPath = os.path.join(ustr(self.defaultSaveDir), savedFileName)
|
||||
self._saveFile(savedPath)
|
||||
else:
|
||||
self._saveFile(self.filePath if self.labelFile
|
||||
else self.saveFileDialog())
|
||||
@ -1260,7 +1248,7 @@ class Settings(object):
|
||||
t = self.types.get(key)
|
||||
if t is not None and t != QVariant:
|
||||
if t is str:
|
||||
return str(value)
|
||||
return ustr(value)
|
||||
else:
|
||||
try:
|
||||
method = getattr(QVariant, re.sub(
|
||||
|
||||
@ -89,12 +89,10 @@ class PascalVocWriter:
|
||||
pose = SubElement(object_item, 'pose')
|
||||
pose.text = "Unspecified"
|
||||
truncated = SubElement(object_item, 'truncated')
|
||||
# max == height or min
|
||||
if int(each_object['ymax']) == int(self.imgSize[0]) or (int(each_object['ymin'])== 1):
|
||||
truncated.text = "1"
|
||||
# max == width or min
|
||||
truncated.text = "1" # max == height or min
|
||||
elif (int(each_object['xmax'])==int(self.imgSize[1])) or (int(each_object['xmin'])== 1):
|
||||
truncated.text = "1"
|
||||
truncated.text = "1" # max == width or min
|
||||
else:
|
||||
truncated.text = "0"
|
||||
difficult = SubElement(object_item, 'Difficult')
|
||||
@ -143,11 +141,10 @@ class PascalVocReader:
|
||||
xmax = int(bndbox.find('xmax').text)
|
||||
ymax = int(bndbox.find('ymax').text)
|
||||
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
|
||||
|
||||
self.shapes.append((label, points, None, None, difficult))
|
||||
|
||||
def parseXML(self):
|
||||
assert self.filepath.endswith('.xml'), "Unsupport file format"
|
||||
assert self.filepath.endswith(XML_EXT), "Unsupport file format"
|
||||
parser = etree.XMLParser(encoding='utf-8')
|
||||
xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
|
||||
filename = xmltree.find('filename').text
|
||||
|
||||
14
libs/ustr.py
Normal file
14
libs/ustr.py
Normal file
@ -0,0 +1,14 @@
|
||||
import sys
|
||||
|
||||
def ustr(x):
|
||||
'''py2/py3 unicode helper'''
|
||||
|
||||
if sys.version_info < (3, 0, 0):
|
||||
from PyQt4.QtCore import QString
|
||||
if type(x) == str:
|
||||
return x.decode('utf-8')
|
||||
if type(x) == QString:
|
||||
return unicode(x)
|
||||
return x
|
||||
else:
|
||||
return x # py3
|
||||
@ -1,8 +1,12 @@
|
||||
import _init_path
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from labelImg import get_main_app
|
||||
|
||||
from pascal_voc_io import PascalVocWriter
|
||||
from pascal_voc_io import PascalVocReader
|
||||
|
||||
|
||||
class TestMainWindow(TestCase):
|
||||
|
||||
@ -18,3 +22,13 @@ class TestMainWindow(TestCase):
|
||||
|
||||
def test_noop(self):
|
||||
pass
|
||||
|
||||
# Test Write/Read
|
||||
writer = PascalVocWriter('tests', 'test', (512, 512, 1), localImgPath='tests/test.bmp')
|
||||
difficult = 1
|
||||
writer.addBndBox(60, 40, 430, 504, 'person', difficult)
|
||||
writer.addBndBox(113, 40, 450, 403, 'face', difficult)
|
||||
writer.save('tests/test.xml')
|
||||
|
||||
reader = PascalVocReader('tests/test.xml')
|
||||
shapes = reader.getShapes()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user