commit
a81bc381f7
@ -1,6 +1,7 @@
|
|||||||
"""Set up paths"""
|
"""Set up paths"""
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def add_path(path):
|
def add_path(path):
|
||||||
if path not in sys.path:
|
if path not in sys.path:
|
||||||
sys.path.insert(0, path)
|
sys.path.insert(0, path)
|
||||||
|
|||||||
105
labelImg.py
105
labelImg.py
@ -17,7 +17,8 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
# needed for py3+qt4
|
# needed for py3+qt4
|
||||||
# ref: http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
|
# ref: http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
|
||||||
# ref: http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
|
# ref:
|
||||||
|
# http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
|
||||||
if sys.version_info.major >= 3:
|
if sys.version_info.major >= 3:
|
||||||
import sip
|
import sip
|
||||||
sip.setapi('QVariant', 2)
|
sip.setapi('QVariant', 2)
|
||||||
@ -39,7 +40,8 @@ from pascal_voc_io import XML_EXT
|
|||||||
|
|
||||||
__appname__ = 'labelImg'
|
__appname__ = 'labelImg'
|
||||||
|
|
||||||
### Utility functions and classes.
|
# Utility functions and classes.
|
||||||
|
|
||||||
|
|
||||||
def u(x):
|
def u(x):
|
||||||
'''py2/py3 unicode helper'''
|
'''py2/py3 unicode helper'''
|
||||||
@ -52,6 +54,7 @@ def u(x):
|
|||||||
else:
|
else:
|
||||||
return x # py3
|
return x # py3
|
||||||
|
|
||||||
|
|
||||||
def have_qstring():
|
def have_qstring():
|
||||||
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
|
'''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.'))
|
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
||||||
@ -62,6 +65,7 @@ def util_qt_strlistclass():
|
|||||||
|
|
||||||
|
|
||||||
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)
|
||||||
if actions:
|
if actions:
|
||||||
@ -81,8 +85,10 @@ class WindowMixin(object):
|
|||||||
|
|
||||||
# PyQt5: TypeError: unhashable type: 'QListWidgetItem'
|
# PyQt5: TypeError: unhashable type: 'QListWidgetItem'
|
||||||
class HashableQListWidgetItem(QListWidgetItem):
|
class HashableQListWidgetItem(QListWidgetItem):
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
super(HashableQListWidgetItem, self).__init__(*args)
|
super(HashableQListWidgetItem, self).__init__(*args)
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(id(self))
|
return hash(id(self))
|
||||||
|
|
||||||
@ -138,14 +144,14 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
listLayout.addWidget(self.editButton) # , 0, Qt.AlignCenter)
|
listLayout.addWidget(self.editButton) # , 0, Qt.AlignCenter)
|
||||||
listLayout.addWidget(self.labelList)
|
listLayout.addWidget(self.labelList)
|
||||||
|
|
||||||
|
|
||||||
self.dock = QDockWidget(u'Box Labels', self)
|
self.dock = QDockWidget(u'Box Labels', self)
|
||||||
self.dock.setObjectName(u'Labels')
|
self.dock.setObjectName(u'Labels')
|
||||||
self.dock.setWidget(self.labelListContainer)
|
self.dock.setWidget(self.labelListContainer)
|
||||||
|
|
||||||
# Tzutalin 20160906 : Add file list and dock to move faster
|
# Tzutalin 20160906 : Add file list and dock to move faster
|
||||||
self.fileListWidget = QListWidget()
|
self.fileListWidget = QListWidget()
|
||||||
self.fileListWidget.itemDoubleClicked.connect(self.fileitemDoubleClicked)
|
self.fileListWidget.itemDoubleClicked.connect(
|
||||||
|
self.fileitemDoubleClicked)
|
||||||
filelistLayout = QVBoxLayout()
|
filelistLayout = QVBoxLayout()
|
||||||
filelistLayout.setContentsMargins(0, 0, 0, 0)
|
filelistLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
filelistLayout.addWidget(self.fileListWidget)
|
filelistLayout.addWidget(self.fileListWidget)
|
||||||
@ -248,7 +254,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
zoom = QWidgetAction(self)
|
zoom = QWidgetAction(self)
|
||||||
zoom.setDefaultWidget(self.zoomWidget)
|
zoom.setDefaultWidget(self.zoomWidget)
|
||||||
self.zoomWidget.setWhatsThis(
|
self.zoomWidget.setWhatsThis(
|
||||||
u"Zoom in or out of the image. Also accessible with"\
|
u"Zoom in or out of the image. Also accessible with"
|
||||||
" %s and %s from the canvas." % (fmtShortcut("Ctrl+[-+]"),
|
" %s and %s from the canvas." % (fmtShortcut("Ctrl+[-+]"),
|
||||||
fmtShortcut("Ctrl+Wheel")))
|
fmtShortcut("Ctrl+Wheel")))
|
||||||
self.zoomWidget.setEnabled(False)
|
self.zoomWidget.setEnabled(False)
|
||||||
@ -266,7 +272,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
'Ctrl+Shift+F', 'fit-width', u'Zoom follows window width',
|
'Ctrl+Shift+F', 'fit-width', u'Zoom follows window width',
|
||||||
checkable=True, enabled=False)
|
checkable=True, enabled=False)
|
||||||
# Group zoom controls into a list for easier toggling.
|
# Group zoom controls into a list for easier toggling.
|
||||||
zoomActions = (self.zoomWidget, zoomIn, zoomOut, zoomOrg, fitWindow, fitWidth)
|
zoomActions = (self.zoomWidget, zoomIn, zoomOut,
|
||||||
|
zoomOrg, fitWindow, fitWidth)
|
||||||
self.zoomMode = self.MANUAL_ZOOM
|
self.zoomMode = self.MANUAL_ZOOM
|
||||||
self.scalers = {
|
self.scalers = {
|
||||||
self.FIT_WINDOW: self.scaleFitWindow,
|
self.FIT_WINDOW: self.scaleFitWindow,
|
||||||
@ -295,7 +302,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
labelMenu = QMenu()
|
labelMenu = QMenu()
|
||||||
addActions(labelMenu, (edit, delete))
|
addActions(labelMenu, (edit, delete))
|
||||||
self.labelList.setContextMenuPolicy(Qt.CustomContextMenu)
|
self.labelList.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||||
self.labelList.customContextMenuRequested.connect(self.popLabelListMenu)
|
self.labelList.customContextMenuRequested.connect(
|
||||||
|
self.popLabelListMenu)
|
||||||
|
|
||||||
# Store actions for further handling.
|
# Store actions for further handling.
|
||||||
self.actions = struct(save=save, saveAs=saveAs, open=open, close=close,
|
self.actions = struct(save=save, saveAs=saveAs, open=open, close=close,
|
||||||
@ -306,13 +314,16 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
|
zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
|
||||||
fitWindow=fitWindow, fitWidth=fitWidth,
|
fitWindow=fitWindow, fitWidth=fitWidth,
|
||||||
zoomActions=zoomActions,
|
zoomActions=zoomActions,
|
||||||
fileMenuActions=(open,opendir,save,saveAs,close,quit),
|
fileMenuActions=(
|
||||||
|
open, opendir, save, saveAs, close, quit),
|
||||||
beginner=(), advanced=(),
|
beginner=(), advanced=(),
|
||||||
editMenu=(edit, copy, delete, None, color1, color2),
|
editMenu=(edit, copy, delete,
|
||||||
|
None, color1, color2),
|
||||||
beginnerContext=(create, edit, copy, delete),
|
beginnerContext=(create, edit, copy, delete),
|
||||||
advancedContext=(createMode, editMode, edit, copy,
|
advancedContext=(createMode, editMode, edit, copy,
|
||||||
delete, shapeLineColor, shapeFillColor),
|
delete, shapeLineColor, shapeFillColor),
|
||||||
onLoadActive=(close, create, createMode, editMode),
|
onLoadActive=(
|
||||||
|
close, create, createMode, editMode),
|
||||||
onShapesPresent=(saveAs, hideAll, showAll))
|
onShapesPresent=(saveAs, hideAll, showAll))
|
||||||
|
|
||||||
self.menus = struct(
|
self.menus = struct(
|
||||||
@ -406,7 +417,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.lastOpenDir = u(settings.get('lastOpenDir', None))
|
self.lastOpenDir = u(settings.get('lastOpenDir', None))
|
||||||
if os.path.exists(saveDir):
|
if os.path.exists(saveDir):
|
||||||
self.defaultSaveDir = saveDir
|
self.defaultSaveDir = 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()
|
||||||
|
|
||||||
# or simply:
|
# or simply:
|
||||||
@ -428,7 +440,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
|
|
||||||
# Populate the File menu dynamically.
|
# Populate the File menu dynamically.
|
||||||
self.updateFileMenu()
|
self.updateFileMenu()
|
||||||
# Since loading the file may take some time, make sure it runs in the background.
|
# Since loading the file may take some time, make sure it runs in the
|
||||||
|
# background.
|
||||||
self.queueEvent(partial(self.loadFile, self.filePath))
|
self.queueEvent(partial(self.loadFile, self.filePath))
|
||||||
|
|
||||||
# Callbacks:
|
# Callbacks:
|
||||||
@ -436,7 +449,6 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
|
|
||||||
self.populateModeActions()
|
self.populateModeActions()
|
||||||
|
|
||||||
|
|
||||||
## Support Functions ##
|
## Support Functions ##
|
||||||
|
|
||||||
def noShapes(self):
|
def noShapes(self):
|
||||||
@ -560,11 +572,13 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
|
|
||||||
def updateFileMenu(self):
|
def updateFileMenu(self):
|
||||||
currFilePath = self.filePath
|
currFilePath = self.filePath
|
||||||
|
|
||||||
def exists(filename):
|
def exists(filename):
|
||||||
return os.path.exists(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 != currFilePath and exists(f)]
|
files = [f for f in self.recentFiles if f !=
|
||||||
|
currFilePath and exists(f)]
|
||||||
for i, f in enumerate(files):
|
for i, f in enumerate(files):
|
||||||
icon = newIcon('labels')
|
icon = newIcon('labels')
|
||||||
action = QAction(
|
action = QAction(
|
||||||
@ -642,11 +656,12 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
def saveLabels(self, annotationFilePath):
|
def saveLabels(self, annotationFilePath):
|
||||||
annotationFilePath = u(annotationFilePath)
|
annotationFilePath = u(annotationFilePath)
|
||||||
lf = LabelFile()
|
lf = LabelFile()
|
||||||
|
|
||||||
def format_shape(s):
|
def format_shape(s):
|
||||||
return dict(label=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()
|
||||||
if s.fill_color != self.fillColor else None,
|
if s.fill_color != self.fillColor else None,
|
||||||
points=[(p.x(), p.y()) for p in s.points])
|
points=[(p.x(), p.y()) for p in s.points])
|
||||||
|
|
||||||
@ -654,7 +669,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
# Can add differrent annotation formats here
|
# Can add differrent annotation formats here
|
||||||
try:
|
try:
|
||||||
if self.usingPascalVocFormat is True:
|
if self.usingPascalVocFormat is True:
|
||||||
print ('Img: ' + self.filePath + ' -> Its xml: ' + annotationFilePath)
|
print ('Img: ' + self.filePath +
|
||||||
|
' -> Its xml: ' + annotationFilePath)
|
||||||
lf.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData,
|
lf.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData,
|
||||||
self.lineColor.getRgb(), self.fillColor.getRgb())
|
self.lineColor.getRgb(), self.fillColor.getRgb())
|
||||||
else:
|
else:
|
||||||
@ -687,14 +703,15 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
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)
|
||||||
|
|
||||||
## Callback functions:
|
# Callback functions:
|
||||||
def newShape(self):
|
def newShape(self):
|
||||||
"""Pop-up and give focus to the label editor.
|
"""Pop-up and give focus to the label editor.
|
||||||
|
|
||||||
position MUST be in global coordinates.
|
position MUST be in global coordinates.
|
||||||
"""
|
"""
|
||||||
if len(self.labelHist) > 0:
|
if len(self.labelHist) > 0:
|
||||||
self.labelDialog = LabelDialog(parent=self, listItem=self.labelHist)
|
self.labelDialog = LabelDialog(
|
||||||
|
parent=self, listItem=self.labelHist)
|
||||||
|
|
||||||
text = self.labelDialog.popUp(text=self.prevLabelText)
|
text = self.labelDialog.popUp(text=self.prevLabelText)
|
||||||
if text is not None:
|
if text is not None:
|
||||||
@ -707,7 +724,6 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.actions.editMode.setEnabled(True)
|
self.actions.editMode.setEnabled(True)
|
||||||
self.setDirty()
|
self.setDirty()
|
||||||
|
|
||||||
|
|
||||||
if text not in self.labelHist:
|
if text not in self.labelHist:
|
||||||
self.labelHist.append(text)
|
self.labelHist.append(text)
|
||||||
else:
|
else:
|
||||||
@ -771,7 +787,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
except LabelFileError as e:
|
except LabelFileError as e:
|
||||||
self.errorMessage(u'Error opening file',
|
self.errorMessage(u'Error opening file',
|
||||||
(u"<p><b>%s</b></p>"
|
(u"<p><b>%s</b></p>"
|
||||||
u"<p>Make sure <i>%s</i> is a valid label file.") \
|
u"<p>Make sure <i>%s</i> is a valid label file.")
|
||||||
% (e, unicodeFilePath))
|
% (e, unicodeFilePath))
|
||||||
self.status("Error reading %s" % unicodeFilePath)
|
self.status("Error reading %s" % unicodeFilePath)
|
||||||
return False
|
return False
|
||||||
@ -802,10 +818,11 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.addRecentFile(self.filePath)
|
self.addRecentFile(self.filePath)
|
||||||
self.toggleActions(True)
|
self.toggleActions(True)
|
||||||
|
|
||||||
## Label xml file and show bound box according to its filename
|
# Label xml file and show bound box according to its filename
|
||||||
if self.usingPascalVocFormat is True and \
|
if self.usingPascalVocFormat is True and \
|
||||||
self.defaultSaveDir is not None:
|
self.defaultSaveDir is not None:
|
||||||
basename = os.path.basename(os.path.splitext(self.filePath)[0]) + XML_EXT
|
basename = os.path.basename(
|
||||||
|
os.path.splitext(self.filePath)[0]) + XML_EXT
|
||||||
xmlPath = os.path.join(self.defaultSaveDir, basename)
|
xmlPath = os.path.join(self.defaultSaveDir, basename)
|
||||||
self.loadPascalXMLByFilename(xmlPath)
|
self.loadPascalXMLByFilename(xmlPath)
|
||||||
|
|
||||||
@ -905,7 +922,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
if dirpath is not None and len(dirpath) > 1:
|
if dirpath is not None and len(dirpath) > 1:
|
||||||
self.defaultSaveDir = dirpath
|
self.defaultSaveDir = dirpath
|
||||||
|
|
||||||
self.statusBar().showMessage('%s . Annotation will be saved to %s' %('Change saved folder', self.defaultSaveDir))
|
self.statusBar().showMessage('%s . Annotation will be saved to %s' %
|
||||||
|
('Change saved folder', self.defaultSaveDir))
|
||||||
self.statusBar().show()
|
self.statusBar().show()
|
||||||
|
|
||||||
def openAnnotation(self, _value=False):
|
def openAnnotation(self, _value=False):
|
||||||
@ -915,7 +933,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
path = os.path.dirname(u(self.filePath))\
|
path = os.path.dirname(u(self.filePath))\
|
||||||
if self.filePath else '.'
|
if self.filePath 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'])
|
||||||
@ -968,7 +986,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
def openNextImg(self, _value=False):
|
def openNextImg(self, _value=False):
|
||||||
# Proceding next image without dialog if having any label
|
# Proceding next image without dialog if having any label
|
||||||
if self.autoSaving is True and self.defaultSaveDir is not None:
|
if self.autoSaving is True and self.defaultSaveDir is not None:
|
||||||
if self.dirty is True and self.hasLabels():
|
if self.dirty is True:
|
||||||
self.saveFile()
|
self.saveFile()
|
||||||
|
|
||||||
if not self.mayContinue():
|
if not self.mayContinue():
|
||||||
@ -993,7 +1011,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
return
|
return
|
||||||
path = os.path.dirname(str(self.filePath))\
|
path = os.path.dirname(str(self.filePath))\
|
||||||
if self.filePath else '.'
|
if self.filePath 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])
|
||||||
@ -1003,21 +1021,20 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.loadFile(filename)
|
self.loadFile(filename)
|
||||||
|
|
||||||
def saveFile(self, _value=False):
|
def saveFile(self, _value=False):
|
||||||
assert not self.image.isNull(), "cannot save empty image"
|
|
||||||
if self.hasLabels():
|
|
||||||
if self.defaultSaveDir is not None and len(str(self.defaultSaveDir)):
|
if self.defaultSaveDir is not None and len(str(self.defaultSaveDir)):
|
||||||
# print('handle the image:' + self.filePath)
|
# print('handle the image:' + self.filePath)
|
||||||
imgFileName = os.path.basename(self.filePath)
|
imgFileName = os.path.basename(self.filePath)
|
||||||
savedFileName = os.path.splitext(imgFileName)[0] + LabelFile.suffix
|
savedFileName = os.path.splitext(
|
||||||
savedPath = os.path.join(str(self.defaultSaveDir), savedFileName)
|
imgFileName)[0] + LabelFile.suffix
|
||||||
|
savedPath = os.path.join(
|
||||||
|
str(self.defaultSaveDir), savedFileName)
|
||||||
self._saveFile(savedPath)
|
self._saveFile(savedPath)
|
||||||
else:
|
else:
|
||||||
self._saveFile(self.filePath if self.labelFile\
|
self._saveFile(self.filePath if self.labelFile
|
||||||
else self.saveFileDialog())
|
else self.saveFileDialog())
|
||||||
|
|
||||||
def saveFileAs(self, _value=False):
|
def saveFileAs(self, _value=False):
|
||||||
assert not self.image.isNull(), "cannot save empty image"
|
assert not self.image.isNull(), "cannot save empty image"
|
||||||
if self.hasLabels():
|
|
||||||
self._saveFile(self.saveFileDialog())
|
self._saveFile(self.saveFileDialog())
|
||||||
|
|
||||||
def saveFileDialog(self):
|
def saveFileDialog(self):
|
||||||
@ -1049,14 +1066,6 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.canvas.setEnabled(False)
|
self.canvas.setEnabled(False)
|
||||||
self.actions.saveAs.setEnabled(False)
|
self.actions.saveAs.setEnabled(False)
|
||||||
|
|
||||||
# Message Dialogs. #
|
|
||||||
def hasLabels(self):
|
|
||||||
if not self.itemsToShapes:
|
|
||||||
self.errorMessage(u'No objects labeled',
|
|
||||||
u'You must label at least one object to save the file.')
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def mayContinue(self):
|
def mayContinue(self):
|
||||||
return not (self.dirty and not self.discardChangesDialog())
|
return not (self.dirty and not self.discardChangesDialog())
|
||||||
|
|
||||||
@ -1092,9 +1101,6 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.setDirty()
|
self.setDirty()
|
||||||
|
|
||||||
def deleteSelectedShape(self):
|
def deleteSelectedShape(self):
|
||||||
yes, no = QMessageBox.Yes, QMessageBox.No
|
|
||||||
msg = u'You are about to permanently delete this Box, proceed anyway?'
|
|
||||||
if yes == QMessageBox.warning(self, u'Attention', msg, yes|no):
|
|
||||||
self.remLabel(self.canvas.deleteSelected())
|
self.remLabel(self.canvas.deleteSelected())
|
||||||
self.setDirty()
|
self.setDirty()
|
||||||
if self.noShapes():
|
if self.noShapes():
|
||||||
@ -1127,7 +1133,8 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
self.setDirty()
|
self.setDirty()
|
||||||
|
|
||||||
def loadPredefinedClasses(self):
|
def loadPredefinedClasses(self):
|
||||||
predefined_classes_path = os.path.join('data', 'predefined_classes.txt')
|
predefined_classes_path = os.path.join(
|
||||||
|
'data', 'predefined_classes.txt')
|
||||||
if os.path.exists(predefined_classes_path) is True:
|
if os.path.exists(predefined_classes_path) is True:
|
||||||
with codecs.open(predefined_classes_path, 'r', 'utf8') as f:
|
with codecs.open(predefined_classes_path, 'r', 'utf8') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
@ -1147,8 +1154,10 @@ class MainWindow(QMainWindow, WindowMixin):
|
|||||||
shapes = tVocParseReader.getShapes()
|
shapes = tVocParseReader.getShapes()
|
||||||
self.loadLabels(shapes)
|
self.loadLabels(shapes)
|
||||||
|
|
||||||
|
|
||||||
class Settings(object):
|
class Settings(object):
|
||||||
"""Convenience dict-like wrapper around QSettings."""
|
"""Convenience dict-like wrapper around QSettings."""
|
||||||
|
|
||||||
def __init__(self, types=None):
|
def __init__(self, types=None):
|
||||||
self.data = QSettings()
|
self.data = QSettings()
|
||||||
self.types = defaultdict(lambda: QVariant, types if types else {})
|
self.types = defaultdict(lambda: QVariant, types if types else {})
|
||||||
@ -1172,7 +1181,8 @@ class Settings(object):
|
|||||||
return str(value)
|
return str(value)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
method = getattr(QVariant, re.sub('^Q', 'to', t.__name__, count=1))
|
method = getattr(QVariant, re.sub(
|
||||||
|
'^Q', 'to', t.__name__, count=1))
|
||||||
return method(value)
|
return method(value)
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
# print(e)
|
# print(e)
|
||||||
@ -1183,6 +1193,7 @@ class Settings(object):
|
|||||||
def inverted(color):
|
def inverted(color):
|
||||||
return QColor(*[255 - v for v in color.getRgb()])
|
return QColor(*[255 - v for v in color.getRgb()])
|
||||||
|
|
||||||
|
|
||||||
def read(filename, default=None):
|
def read(filename, default=None):
|
||||||
try:
|
try:
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
@ -1190,6 +1201,7 @@ def read(filename, default=None):
|
|||||||
except:
|
except:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def get_main_app(argv=[]):
|
def get_main_app(argv=[]):
|
||||||
"""
|
"""
|
||||||
Standard boilerplate Qt application code.
|
Standard boilerplate Qt application code.
|
||||||
@ -1202,6 +1214,7 @@ def get_main_app(argv=[]):
|
|||||||
win.show()
|
win.show()
|
||||||
return app, win
|
return app, win
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
'''construct main app and run it'''
|
'''construct main app and run it'''
|
||||||
app, _win = get_main_app(argv)
|
app, _win = get_main_app(argv)
|
||||||
|
|||||||
@ -19,6 +19,8 @@ CURSOR_MOVE = Qt.ClosedHandCursor
|
|||||||
CURSOR_GRAB = Qt.OpenHandCursor
|
CURSOR_GRAB = Qt.OpenHandCursor
|
||||||
|
|
||||||
# class Canvas(QGLWidget):
|
# class Canvas(QGLWidget):
|
||||||
|
|
||||||
|
|
||||||
class Canvas(QWidget):
|
class Canvas(QWidget):
|
||||||
zoomRequest = pyqtSignal(int)
|
zoomRequest = pyqtSignal(int)
|
||||||
scrollRequest = pyqtSignal(int, int)
|
scrollRequest = pyqtSignal(int, int)
|
||||||
@ -106,7 +108,8 @@ class Canvas(QWidget):
|
|||||||
# Project the point to the pixmap's edges.
|
# Project the point to the pixmap's edges.
|
||||||
pos = self.intersectionPoint(self.current[-1], pos)
|
pos = self.intersectionPoint(self.current[-1], pos)
|
||||||
elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
|
elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
|
||||||
# Attract line to starting point and colorise to alert the user:
|
# Attract line to starting point and colorise to alert the
|
||||||
|
# user:
|
||||||
pos = self.current[0]
|
pos = self.current[0]
|
||||||
color = self.current.line_color
|
color = self.current.line_color
|
||||||
self.overrideCursor(CURSOR_POINT)
|
self.overrideCursor(CURSOR_POINT)
|
||||||
@ -164,7 +167,8 @@ class Canvas(QWidget):
|
|||||||
if self.selectedVertex():
|
if self.selectedVertex():
|
||||||
self.hShape.highlightClear()
|
self.hShape.highlightClear()
|
||||||
self.hVertex, self.hShape = None, shape
|
self.hVertex, self.hShape = None, shape
|
||||||
self.setToolTip("Click & drag to move shape '%s'" % shape.label)
|
self.setToolTip(
|
||||||
|
"Click & drag to move shape '%s'" % shape.label)
|
||||||
self.setStatusTip(self.toolTip())
|
self.setStatusTip(self.toolTip())
|
||||||
self.overrideCursor(CURSOR_GRAB)
|
self.overrideCursor(CURSOR_GRAB)
|
||||||
self.update()
|
self.update()
|
||||||
@ -221,6 +225,29 @@ class Canvas(QWidget):
|
|||||||
self.repaint()
|
self.repaint()
|
||||||
elif ev.button() == Qt.LeftButton and self.selectedShape:
|
elif ev.button() == Qt.LeftButton and self.selectedShape:
|
||||||
self.overrideCursor(CURSOR_GRAB)
|
self.overrideCursor(CURSOR_GRAB)
|
||||||
|
elif ev.button() == Qt.LeftButton:
|
||||||
|
if self.drawing():
|
||||||
|
if self.current and self.current.reachMaxPoints() is False:
|
||||||
|
initPos = self.current[0]
|
||||||
|
minX = initPos.x()
|
||||||
|
minY = initPos.y()
|
||||||
|
targetPos = self.line[1]
|
||||||
|
maxX = targetPos.x()
|
||||||
|
maxY = targetPos.y()
|
||||||
|
self.current.addPoint(QPointF(maxX, minY))
|
||||||
|
self.current.addPoint(targetPos)
|
||||||
|
self.current.addPoint(QPointF(minX, maxY))
|
||||||
|
self.current.addPoint(initPos)
|
||||||
|
self.line[0] = self.current[-1]
|
||||||
|
if self.current.isClosed():
|
||||||
|
self.finalise()
|
||||||
|
elif not self.outOfPixmap(pos):
|
||||||
|
self.current = Shape()
|
||||||
|
self.current.addPoint(pos)
|
||||||
|
self.line.points = [pos, pos]
|
||||||
|
self.setHiding()
|
||||||
|
self.drawingPolygon.emit(True)
|
||||||
|
self.update()
|
||||||
|
|
||||||
def endMove(self, copy=False):
|
def endMove(self, copy=False):
|
||||||
assert self.selectedShape and self.selectedShapeCopy
|
assert self.selectedShape and self.selectedShapeCopy
|
||||||
@ -507,7 +534,7 @@ class Canvas(QWidget):
|
|||||||
self.zoomRequest.emit(ev.delta())
|
self.zoomRequest.emit(ev.delta())
|
||||||
else:
|
else:
|
||||||
self.scrollRequest.emit(ev.delta(),
|
self.scrollRequest.emit(ev.delta(),
|
||||||
Qt.Horizontal if (Qt.ShiftModifier == int(mods))\
|
Qt.Horizontal if (Qt.ShiftModifier == int(mods))
|
||||||
else Qt.Vertical)
|
else Qt.Vertical)
|
||||||
else:
|
else:
|
||||||
self.scrollRequest.emit(ev.delta(), Qt.Horizontal)
|
self.scrollRequest.emit(ev.delta(), Qt.Horizontal)
|
||||||
@ -571,4 +598,3 @@ class Canvas(QWidget):
|
|||||||
self.restoreCursor()
|
self.restoreCursor()
|
||||||
self.pixmap = None
|
self.pixmap = None
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
|||||||
@ -8,13 +8,15 @@ except ImportError:
|
|||||||
|
|
||||||
BB = QDialogButtonBox
|
BB = QDialogButtonBox
|
||||||
|
|
||||||
|
|
||||||
class ColorDialog(QColorDialog):
|
class ColorDialog(QColorDialog):
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super(ColorDialog, self).__init__(parent)
|
super(ColorDialog, self).__init__(parent)
|
||||||
self.setOption(QColorDialog.ShowAlphaChannel)
|
self.setOption(QColorDialog.ShowAlphaChannel)
|
||||||
# The Mac native dialog does not support our restore button.
|
# The Mac native dialog does not support our restore button.
|
||||||
self.setOption(QColorDialog.DontUseNativeDialog)
|
self.setOption(QColorDialog.DontUseNativeDialog)
|
||||||
## Add a restore defaults button.
|
# Add a restore defaults button.
|
||||||
# The default is set at invocation time, so that it
|
# The default is set at invocation time, so that it
|
||||||
# works across dialogs for different elements.
|
# works across dialogs for different elements.
|
||||||
self.default = None
|
self.default = None
|
||||||
@ -33,4 +35,3 @@ class ColorDialog(QColorDialog):
|
|||||||
def checkRestore(self, button):
|
def checkRestore(self, button):
|
||||||
if self.bb.buttonRole(button) & BB.ResetRole and self.default:
|
if self.bb.buttonRole(button) & BB.ResetRole and self.default:
|
||||||
self.setCurrentColor(self.default)
|
self.setCurrentColor(self.default)
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,7 @@ from lib import newIcon, labelValidator
|
|||||||
|
|
||||||
BB = QDialogButtonBox
|
BB = QDialogButtonBox
|
||||||
|
|
||||||
|
|
||||||
class LabelDialog(QDialog):
|
class LabelDialog(QDialog):
|
||||||
|
|
||||||
def __init__(self, text="Enter object label", parent=None, listItem=None):
|
def __init__(self, text="Enter object label", parent=None, listItem=None):
|
||||||
|
|||||||
@ -11,9 +11,11 @@ from pascal_voc_io import PascalVocWriter
|
|||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class LabelFileError(Exception):
|
class LabelFileError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class LabelFile(object):
|
class LabelFile(object):
|
||||||
# It might be changed as window creates
|
# It might be changed as window creates
|
||||||
suffix = '.lif'
|
suffix = '.lif'
|
||||||
@ -35,18 +37,16 @@ class LabelFile(object):
|
|||||||
# Pascal format
|
# Pascal format
|
||||||
image = QImage()
|
image = QImage()
|
||||||
image.load(imagePath)
|
image.load(imagePath)
|
||||||
imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3]
|
imageShape = [image.height(), image.width(),
|
||||||
writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt,\
|
1 if image.isGrayscale() else 3]
|
||||||
|
writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt,
|
||||||
imageShape, localImgPath=imagePath)
|
imageShape, localImgPath=imagePath)
|
||||||
bSave = False
|
|
||||||
for shape in shapes:
|
for shape in shapes:
|
||||||
points = shape['points']
|
points = shape['points']
|
||||||
label = shape['label']
|
label = shape['label']
|
||||||
bndbox = LabelFile.convertPoints2BndBox(points)
|
bndbox = LabelFile.convertPoints2BndBox(points)
|
||||||
writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
|
writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
|
||||||
bSave = True
|
|
||||||
|
|
||||||
if bSave:
|
|
||||||
writer.save(targetFile=filename)
|
writer.save(targetFile=filename)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ except ImportError:
|
|||||||
def newIcon(icon):
|
def newIcon(icon):
|
||||||
return QIcon(':/' + icon)
|
return QIcon(':/' + icon)
|
||||||
|
|
||||||
|
|
||||||
def newButton(text, icon=None, slot=None):
|
def newButton(text, icon=None, slot=None):
|
||||||
b = QPushButton(text)
|
b = QPushButton(text)
|
||||||
if icon is not None:
|
if icon is not None:
|
||||||
@ -20,6 +21,7 @@ def newButton(text, icon=None, slot=None):
|
|||||||
b.clicked.connect(slot)
|
b.clicked.connect(slot)
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
|
||||||
def newAction(parent, text, slot=None, shortcut=None, icon=None,
|
def newAction(parent, text, slot=None, shortcut=None, icon=None,
|
||||||
tip=None, checkable=False, enabled=True):
|
tip=None, checkable=False, enabled=True):
|
||||||
"""Create a new action and assign callbacks, shortcuts, etc."""
|
"""Create a new action and assign callbacks, shortcuts, etc."""
|
||||||
@ -51,18 +53,21 @@ def addActions(widget, actions):
|
|||||||
else:
|
else:
|
||||||
widget.addAction(action)
|
widget.addAction(action)
|
||||||
|
|
||||||
|
|
||||||
def labelValidator():
|
def labelValidator():
|
||||||
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
|
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
|
||||||
|
|
||||||
|
|
||||||
class struct(object):
|
class struct(object):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.__dict__.update(kwargs)
|
self.__dict__.update(kwargs)
|
||||||
|
|
||||||
|
|
||||||
def distance(p):
|
def distance(p):
|
||||||
return sqrt(p.x() * p.x() + p.y() * p.y())
|
return sqrt(p.x() * p.x() + p.y() * p.y())
|
||||||
|
|
||||||
|
|
||||||
def fmtShortcut(text):
|
def fmtShortcut(text):
|
||||||
mod, key = text.split('+', 1)
|
mod, key = text.split('+', 1)
|
||||||
return '<b>%s</b>+<b>%s</b>' % (mod, key)
|
return '<b>%s</b>+<b>%s</b>' % (mod, key)
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import codecs
|
|||||||
|
|
||||||
XML_EXT = '.xml'
|
XML_EXT = '.xml'
|
||||||
|
|
||||||
|
|
||||||
class PascalVocWriter:
|
class PascalVocWriter:
|
||||||
|
|
||||||
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
|
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
|
||||||
@ -34,8 +35,7 @@ class PascalVocWriter:
|
|||||||
# Check conditions
|
# Check conditions
|
||||||
if self.filename is None or \
|
if self.filename is None or \
|
||||||
self.foldername is None or \
|
self.foldername is None or \
|
||||||
self.imgSize is None or \
|
self.imgSize is None:
|
||||||
len(self.boxlist) <= 0:
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
top = Element('annotation')
|
top = Element('annotation')
|
||||||
@ -102,7 +102,8 @@ class PascalVocWriter:
|
|||||||
self.appendObjects(root)
|
self.appendObjects(root)
|
||||||
out_file = None
|
out_file = None
|
||||||
if targetFile is None:
|
if targetFile is None:
|
||||||
out_file = codecs.open(self.filename + XML_EXT, 'w', encoding='utf-8')
|
out_file = codecs.open(
|
||||||
|
self.filename + XML_EXT, 'w', encoding='utf-8')
|
||||||
else:
|
else:
|
||||||
out_file = codecs.open(targetFile, 'w', encoding='utf-8')
|
out_file = codecs.open(targetFile, 'w', encoding='utf-8')
|
||||||
|
|
||||||
|
|||||||
@ -18,13 +18,14 @@ DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
|
|||||||
DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
|
DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
|
||||||
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
|
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
class Shape(object):
|
class Shape(object):
|
||||||
P_SQUARE, P_ROUND = range(2)
|
P_SQUARE, P_ROUND = range(2)
|
||||||
|
|
||||||
MOVE_VERTEX, NEAR_VERTEX = range(2)
|
MOVE_VERTEX, NEAR_VERTEX = range(2)
|
||||||
|
|
||||||
## The following class variables influence the drawing
|
# The following class variables influence the drawing
|
||||||
## of _all_ shape objects.
|
# of _all_ shape objects.
|
||||||
line_color = DEFAULT_LINE_COLOR
|
line_color = DEFAULT_LINE_COLOR
|
||||||
fill_color = DEFAULT_FILL_COLOR
|
fill_color = DEFAULT_FILL_COLOR
|
||||||
select_line_color = DEFAULT_SELECT_LINE_COLOR
|
select_line_color = DEFAULT_SELECT_LINE_COLOR
|
||||||
@ -181,4 +182,3 @@ class Shape(object):
|
|||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
self.points[key] = value
|
self.points[key] = value
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
class ToolBar(QToolBar):
|
class ToolBar(QToolBar):
|
||||||
|
|
||||||
def __init__(self, title):
|
def __init__(self, title):
|
||||||
super(ToolBar, self).__init__(title)
|
super(ToolBar, self).__init__(title)
|
||||||
layout = self.layout()
|
layout = self.layout()
|
||||||
@ -29,10 +30,10 @@ class ToolBar(QToolBar):
|
|||||||
class ToolButton(QToolButton):
|
class ToolButton(QToolButton):
|
||||||
"""ToolBar companion class which ensures all buttons have the same size."""
|
"""ToolBar companion class which ensures all buttons have the same size."""
|
||||||
minSize = (60, 60)
|
minSize = (60, 60)
|
||||||
|
|
||||||
def minimumSizeHint(self):
|
def minimumSizeHint(self):
|
||||||
ms = super(ToolButton, self).minimumSizeHint()
|
ms = super(ToolButton, self).minimumSizeHint()
|
||||||
w1, h1 = ms.width(), ms.height()
|
w1, h1 = ms.width(), ms.height()
|
||||||
w2, h2 = self.minSize
|
w2, h2 = self.minSize
|
||||||
ToolButton.minSize = max(w1, w2), max(h1, h2)
|
ToolButton.minSize = max(w1, w2), max(h1, h2)
|
||||||
return QSize(*ToolButton.minSize)
|
return QSize(*ToolButton.minSize)
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,9 @@ except ImportError:
|
|||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
|
|
||||||
|
|
||||||
class ZoomWidget(QSpinBox):
|
class ZoomWidget(QSpinBox):
|
||||||
|
|
||||||
def __init__(self, value=100):
|
def __init__(self, value=100):
|
||||||
super(ZoomWidget, self).__init__()
|
super(ZoomWidget, self).__init__()
|
||||||
self.setButtonSymbols(QAbstractSpinBox.NoButtons)
|
self.setButtonSymbols(QAbstractSpinBox.NoButtons)
|
||||||
@ -22,4 +24,3 @@ class ZoomWidget(QSpinBox):
|
|||||||
fm = QFontMetrics(self.font())
|
fm = QFontMetrics(self.font())
|
||||||
width = fm.width(str(self.maximum()))
|
width = fm.width(str(self.maximum()))
|
||||||
return QSize(width, height)
|
return QSize(width, height)
|
||||||
|
|
||||||
|
|||||||
@ -18,4 +18,3 @@ class TestMainWindow(TestCase):
|
|||||||
|
|
||||||
def test_noop(self):
|
def test_noop(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user