app.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. import argparse
  2. import functools
  3. import os.path
  4. import subprocess
  5. import sys
  6. import warnings
  7. from qtpy import QT_VERSION
  8. from qtpy import QtCore
  9. from qtpy.QtCore import Qt
  10. from qtpy import QtGui
  11. from qtpy import QtWidgets
  12. import yaml
  13. QT5 = QT_VERSION[0] == '5'
  14. from labelme.canvas import Canvas
  15. from labelme.colorDialog import ColorDialog
  16. from labelme.config import default_config
  17. from labelme.labelDialog import LabelDialog
  18. from labelme.labelFile import LabelFile
  19. from labelme.labelFile import LabelFileError
  20. from labelme.lib import addActions
  21. from labelme.lib import fmtShortcut
  22. from labelme.lib import newAction
  23. from labelme.lib import newIcon
  24. from labelme.lib import struct
  25. from labelme.shape import DEFAULT_FILL_COLOR
  26. from labelme.shape import DEFAULT_LINE_COLOR
  27. from labelme.shape import Shape
  28. from labelme.toolBar import ToolBar
  29. from labelme.zoomWidget import ZoomWidget
  30. __appname__ = 'labelme'
  31. # FIXME
  32. # - [medium] Set max zoom value to something big enough for FitWidth/Window
  33. # TODO(unknown):
  34. # - [high] Automatically add file suffix when saving.
  35. # - [high] Add polygon movement with arrow keys
  36. # - [high] Deselect shape when clicking and already selected(?)
  37. # - [medium] Zoom should keep the image centered.
  38. # - [medium] Add undo button for vertex addition.
  39. # - [low,maybe] Open images with drag & drop.
  40. # - [low,maybe] Preview images on file dialogs.
  41. # - [low,maybe] Sortable label list.
  42. # - Zoom is too "steppy".
  43. # Utility functions and classes.
  44. class WindowMixin(object):
  45. def menu(self, title, actions=None):
  46. menu = self.menuBar().addMenu(title)
  47. if actions:
  48. addActions(menu, actions)
  49. return menu
  50. def toolbar(self, title, actions=None):
  51. toolbar = ToolBar(title)
  52. toolbar.setObjectName('%sToolBar' % title)
  53. # toolbar.setOrientation(Qt.Vertical)
  54. toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
  55. if actions:
  56. addActions(toolbar, actions)
  57. self.addToolBar(Qt.LeftToolBarArea, toolbar)
  58. return toolbar
  59. class EscapableQListWidget(QtWidgets.QListWidget):
  60. def keyPressEvent(self, event):
  61. if event.key() == Qt.Key_Escape:
  62. self.clearSelection()
  63. class LabelQListWidget(QtWidgets.QListWidget):
  64. def __init__(self, *args, **kwargs):
  65. super(LabelQListWidget, self).__init__(*args, **kwargs)
  66. self.canvas = None
  67. self.itemsToShapes = []
  68. def get_shape_from_item(self, item):
  69. for index, (item_, shape) in enumerate(self.itemsToShapes):
  70. if item_ is item:
  71. return shape
  72. def get_item_from_shape(self, shape):
  73. for index, (item, shape_) in enumerate(self.itemsToShapes):
  74. if shape_ is shape:
  75. return item
  76. def clear(self):
  77. super(LabelQListWidget, self).clear()
  78. self.itemsToShapes = []
  79. def setParent(self, parent):
  80. self.parent = parent
  81. def dropEvent(self, event):
  82. shapes = self.shapes
  83. super(LabelQListWidget, self).dropEvent(event)
  84. if self.shapes == shapes:
  85. return
  86. if self.canvas is None:
  87. raise RuntimeError('self.canvas must be set beforehand.')
  88. self.parent.setDirty()
  89. self.canvas.shapes = self.shapes
  90. @property
  91. def shapes(self):
  92. shapes = []
  93. for i in range(self.count()):
  94. item = self.item(i)
  95. shape = self.get_shape_from_item(item)
  96. shapes.append(shape)
  97. return shapes
  98. class MainWindow(QtWidgets.QMainWindow, WindowMixin):
  99. FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = 0, 1, 2
  100. def __init__(self, filename=None, output=None, store_data=True,
  101. labels=None, sort_labels=True):
  102. super(MainWindow, self).__init__()
  103. self.setWindowTitle(__appname__)
  104. # Whether we need to save or not.
  105. self.dirty = False
  106. self._noSelectionSlot = False
  107. self.screencastViewer = "firefox"
  108. self.screencast = "screencast.ogv"
  109. # Main widgets and related state.
  110. self.labelDialog = LabelDialog(parent=self, labels=labels,
  111. sort_labels=sort_labels)
  112. self.labelList = LabelQListWidget()
  113. self.lastOpenDir = None
  114. self.labelList.itemActivated.connect(self.labelSelectionChanged)
  115. self.labelList.itemSelectionChanged.connect(self.labelSelectionChanged)
  116. self.labelList.itemDoubleClicked.connect(self.editLabel)
  117. # Connect to itemChanged to detect checkbox changes.
  118. self.labelList.itemChanged.connect(self.labelItemChanged)
  119. self.labelList.setDragDropMode(
  120. QtWidgets.QAbstractItemView.InternalMove)
  121. self.labelList.setParent(self)
  122. listLayout = QtWidgets.QVBoxLayout()
  123. listLayout.setContentsMargins(0, 0, 0, 0)
  124. self.editButton = QtWidgets.QToolButton()
  125. self.editButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
  126. listLayout.addWidget(self.editButton) # 0, Qt.AlignCenter)
  127. listLayout.addWidget(self.labelList)
  128. self.labelListContainer = QtWidgets.QWidget()
  129. self.labelListContainer.setLayout(listLayout)
  130. self.uniqLabelList = EscapableQListWidget()
  131. self.uniqLabelList.setToolTip(
  132. "Select label to start annotating for it. "
  133. "Press 'Esc' to deselect.")
  134. if labels:
  135. self.uniqLabelList.addItems(labels)
  136. self.uniqLabelList.sortItems()
  137. self.labelsdock = QtWidgets.QDockWidget(u'Label List', self)
  138. self.labelsdock.setObjectName(u'Label List')
  139. self.labelsdock.setWidget(self.uniqLabelList)
  140. self.dock = QtWidgets.QDockWidget('Polygon Labels', self)
  141. self.dock.setObjectName('Labels')
  142. self.dock.setWidget(self.labelListContainer)
  143. self.fileListWidget = QtWidgets.QListWidget()
  144. self.fileListWidget.itemSelectionChanged.connect(
  145. self.fileSelectionChanged)
  146. filelistLayout = QtWidgets.QVBoxLayout()
  147. filelistLayout.setContentsMargins(0, 0, 0, 0)
  148. filelistLayout.addWidget(self.fileListWidget)
  149. fileListContainer = QtWidgets.QWidget()
  150. fileListContainer.setLayout(filelistLayout)
  151. self.filedock = QtWidgets.QDockWidget(u'File List', self)
  152. self.filedock.setObjectName(u'Files')
  153. self.filedock.setWidget(fileListContainer)
  154. self.zoomWidget = ZoomWidget()
  155. self.colorDialog = ColorDialog(parent=self)
  156. self.canvas = self.labelList.canvas = Canvas()
  157. self.canvas.zoomRequest.connect(self.zoomRequest)
  158. scrollArea = QtWidgets.QScrollArea()
  159. scrollArea.setWidget(self.canvas)
  160. scrollArea.setWidgetResizable(True)
  161. self.scrollBars = {
  162. Qt.Vertical: scrollArea.verticalScrollBar(),
  163. Qt.Horizontal: scrollArea.horizontalScrollBar(),
  164. }
  165. self.canvas.scrollRequest.connect(self.scrollRequest)
  166. self.canvas.newShape.connect(self.newShape)
  167. self.canvas.shapeMoved.connect(self.setDirty)
  168. self.canvas.selectionChanged.connect(self.shapeSelectionChanged)
  169. self.canvas.drawingPolygon.connect(self.toggleDrawingSensitive)
  170. self.setCentralWidget(scrollArea)
  171. self.addDockWidget(Qt.RightDockWidgetArea, self.labelsdock)
  172. self.addDockWidget(Qt.RightDockWidgetArea, self.dock)
  173. self.addDockWidget(Qt.RightDockWidgetArea, self.filedock)
  174. self.filedock.setFeatures(QtWidgets.QDockWidget.DockWidgetFloatable)
  175. self.dockFeatures = (QtWidgets.QDockWidget.DockWidgetClosable |
  176. QtWidgets.QDockWidget.DockWidgetFloatable)
  177. self.dock.setFeatures(self.dock.features() ^ self.dockFeatures)
  178. config = self.getConfig()
  179. # Actions
  180. action = functools.partial(newAction, self)
  181. shortcuts = config['shortcuts']
  182. quit = action('&Quit', self.close, shortcuts['quit'], 'quit',
  183. 'Quit application')
  184. open_ = action('&Open', self.openFile, shortcuts['open'], 'open',
  185. 'Open image or label file')
  186. opendir = action('&Open Dir', self.openDirDialog,
  187. shortcuts['open_dir'], 'open', u'Open Dir')
  188. openNextImg = action('&Next Image', self.openNextImg,
  189. shortcuts['open_next'], 'next', u'Open Next')
  190. openPrevImg = action('&Prev Image', self.openPrevImg,
  191. shortcuts['open_prev'], 'prev', u'Open Prev')
  192. save = action('&Save', self.saveFile, shortcuts['save'], 'save',
  193. 'Save labels to file', enabled=False)
  194. saveAs = action('&Save As', self.saveFileAs, shortcuts['save_as'],
  195. 'save-as', 'Save labels to a different file',
  196. enabled=False)
  197. close = action('&Close', self.closeFile, shortcuts['close'], 'close',
  198. 'Close current file')
  199. color1 = action('Polygon &Line Color', self.chooseColor1,
  200. shortcuts['edit_line_color'], 'color_line',
  201. 'Choose polygon line color')
  202. color2 = action('Polygon &Fill Color', self.chooseColor2,
  203. shortcuts['edit_fill_color'], 'color',
  204. 'Choose polygon fill color')
  205. createMode = action('Create\nPolygo&ns', self.setCreateMode,
  206. shortcuts['create_polygon'], 'objects',
  207. 'Start drawing polygons', enabled=True)
  208. editMode = action('&Edit\nPolygons', self.setEditMode,
  209. shortcuts['edit_polygon'], 'edit',
  210. 'Move and edit polygons', enabled=False)
  211. delete = action('Delete\nPolygon', self.deleteSelectedShape,
  212. shortcuts['delete_polygon'], 'cancel',
  213. 'Delete', enabled=False)
  214. copy = action('&Duplicate\nPolygon', self.copySelectedShape,
  215. shortcuts['duplicate_polygon'], 'copy',
  216. 'Create a duplicate of the selected polygon',
  217. enabled=False)
  218. undoLastPoint = action('Undo last point', self.canvas.undoLastPoint,
  219. shortcuts['undo_last_point'], 'undoLastPoint',
  220. 'Undo last drawn point', enabled=False)
  221. hideAll = action('&Hide\nPolygons',
  222. functools.partial(self.togglePolygons, False),
  223. icon='eye', tip='Hide all polygons', enabled=False)
  224. showAll = action('&Show\nPolygons',
  225. functools.partial(self.togglePolygons, True),
  226. icon='eye', tip='Show all polygons', enabled=False)
  227. help = action('&Tutorial', self.tutorial, icon='help',
  228. tip='Show screencast of introductory tutorial')
  229. zoom = QtWidgets.QWidgetAction(self)
  230. zoom.setDefaultWidget(self.zoomWidget)
  231. self.zoomWidget.setWhatsThis(
  232. "Zoom in or out of the image. Also accessible with"
  233. " %s and %s from the canvas." %
  234. (fmtShortcut('%s,%s' % (shortcuts['zoom_in'],
  235. shortcuts['zoom_out'])),
  236. fmtShortcut("Ctrl+Wheel")))
  237. self.zoomWidget.setEnabled(False)
  238. zoomIn = action('Zoom &In', functools.partial(self.addZoom, 10),
  239. shortcuts['zoom_in'], 'zoom-in',
  240. 'Increase zoom level', enabled=False)
  241. zoomOut = action('&Zoom Out', functools.partial(self.addZoom, -10),
  242. shortcuts['zoom_out'], 'zoom-out',
  243. 'Decrease zoom level', enabled=False)
  244. zoomOrg = action('&Original size',
  245. functools.partial(self.setZoom, 100),
  246. shortcuts['zoom_to_original'], 'zoom',
  247. 'Zoom to original size', enabled=False)
  248. fitWindow = action('&Fit Window', self.setFitWindow,
  249. shortcuts['fit_window'], 'fit-window',
  250. 'Zoom follows window size', checkable=True,
  251. enabled=False)
  252. fitWidth = action('Fit &Width', self.setFitWidth,
  253. shortcuts['fit_width'], 'fit-width',
  254. 'Zoom follows window width',
  255. checkable=True, enabled=False)
  256. # Group zoom controls into a list for easier toggling.
  257. zoomActions = (self.zoomWidget, zoomIn, zoomOut, zoomOrg,
  258. fitWindow, fitWidth)
  259. self.zoomMode = self.MANUAL_ZOOM
  260. self.scalers = {
  261. self.FIT_WINDOW: self.scaleFitWindow,
  262. self.FIT_WIDTH: self.scaleFitWidth,
  263. # Set to one to scale to 100% when loading files.
  264. self.MANUAL_ZOOM: lambda: 1,
  265. }
  266. edit = action('&Edit Label', self.editLabel, shortcuts['edit_label'],
  267. 'edit', 'Modify the label of the selected polygon',
  268. enabled=False)
  269. self.editButton.setDefaultAction(edit)
  270. shapeLineColor = action(
  271. 'Shape &Line Color', self.chshapeLineColor, icon='color-line',
  272. tip='Change the line color for this specific shape', enabled=False)
  273. shapeFillColor = action(
  274. 'Shape &Fill Color', self.chshapeFillColor, icon='color',
  275. tip='Change the fill color for this specific shape', enabled=False)
  276. labels = self.dock.toggleViewAction()
  277. labels.setText('Show/Hide Label Panel')
  278. # Lavel list context menu.
  279. labelMenu = QtWidgets.QMenu()
  280. addActions(labelMenu, (edit, delete))
  281. self.labelList.setContextMenuPolicy(Qt.CustomContextMenu)
  282. self.labelList.customContextMenuRequested.connect(
  283. self.popLabelListMenu)
  284. # Store actions for further handling.
  285. self.actions = struct(
  286. save=save, saveAs=saveAs, open=open_, close=close,
  287. lineColor=color1, fillColor=color2,
  288. delete=delete, edit=edit, copy=copy,
  289. undoLastPoint=undoLastPoint,
  290. createMode=createMode, editMode=editMode,
  291. shapeLineColor=shapeLineColor, shapeFillColor=shapeFillColor,
  292. zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
  293. fitWindow=fitWindow, fitWidth=fitWidth,
  294. zoomActions=zoomActions,
  295. fileMenuActions=(open_, opendir, save, saveAs, close, quit),
  296. tool=(),
  297. editMenu=(edit, copy, delete, None, undoLastPoint,
  298. None, color1, color2),
  299. menu=(
  300. createMode, editMode, edit, copy,
  301. delete, shapeLineColor, shapeFillColor,
  302. undoLastPoint,
  303. ),
  304. onLoadActive=(close, createMode, editMode),
  305. onShapesPresent=(saveAs, hideAll, showAll),
  306. )
  307. self.menus = struct(
  308. file=self.menu('&File'),
  309. edit=self.menu('&Edit'),
  310. view=self.menu('&View'),
  311. help=self.menu('&Help'),
  312. recentFiles=QtWidgets.QMenu('Open &Recent'),
  313. labelList=labelMenu,
  314. )
  315. addActions(self.menus.file, (open_, opendir, self.menus.recentFiles,
  316. save, saveAs, close, None, quit))
  317. addActions(self.menus.help, (help,))
  318. addActions(self.menus.view, (
  319. labels, None,
  320. hideAll, showAll, None,
  321. zoomIn, zoomOut, zoomOrg, None,
  322. fitWindow, fitWidth))
  323. self.menus.file.aboutToShow.connect(self.updateFileMenu)
  324. # Custom context menu for the canvas widget:
  325. addActions(self.canvas.menus[0], self.actions.menu)
  326. addActions(self.canvas.menus[1], (
  327. action('&Copy here', self.copyShape),
  328. action('&Move here', self.moveShape)))
  329. self.tools = self.toolbar('Tools')
  330. self.actions.tool = (
  331. open_, opendir, openNextImg, openPrevImg, save,
  332. None, createMode, copy, delete, editMode, None,
  333. zoomIn, zoom, zoomOut, fitWindow, fitWidth)
  334. self.statusBar().showMessage('%s started.' % __appname__)
  335. self.statusBar().show()
  336. # Application state.
  337. self.image = QtGui.QImage()
  338. self.imagePath = None
  339. self.labeling_once = output is not None
  340. self.output = output
  341. self._store_data = store_data
  342. self.recentFiles = []
  343. self.maxRecent = 7
  344. self.lineColor = None
  345. self.fillColor = None
  346. self.zoom_level = 100
  347. self.fit_window = False
  348. if filename is not None and os.path.isdir(filename):
  349. self.importDirImages(filename)
  350. else:
  351. self.filename = filename
  352. # XXX: Could be completely declarative.
  353. # Restore application settings.
  354. self.settings = QtCore.QSettings('labelme', 'labelme')
  355. # FIXME: QSettings.value can return None on PyQt4
  356. self.recentFiles = self.settings.value('recentFiles', []) or []
  357. size = self.settings.value('window/size', QtCore.QSize(600, 500))
  358. position = self.settings.value('window/position', QtCore.QPoint(0, 0))
  359. self.resize(size)
  360. self.move(position)
  361. # or simply:
  362. # self.restoreGeometry(settings['window/geometry']
  363. self.restoreState(
  364. self.settings.value('window/state', QtCore.QByteArray()))
  365. self.lineColor = QtGui.QColor(
  366. self.settings.value('line/color', Shape.line_color))
  367. self.fillColor = QtGui.QColor(
  368. self.settings.value('fill/color', Shape.fill_color))
  369. Shape.line_color = self.lineColor
  370. Shape.fill_color = self.fillColor
  371. # Populate the File menu dynamically.
  372. self.updateFileMenu()
  373. # Since loading the file may take some time,
  374. # make sure it runs in the background.
  375. if self.filename is not None:
  376. self.queueEvent(functools.partial(self.loadFile, self.filename))
  377. # Callbacks:
  378. self.zoomWidget.valueChanged.connect(self.paintCanvas)
  379. self.populateModeActions()
  380. # self.firstStart = True
  381. # if self.firstStart:
  382. # QWhatsThis.enterWhatsThisMode()
  383. # Support Functions
  384. def getConfig(self):
  385. # shortcuts for actions
  386. home = os.path.expanduser('~')
  387. config_file = os.path.join(home, '.labelmerc')
  388. # default config
  389. config = default_config.copy()
  390. def update_dict(target_dict, new_dict):
  391. for key, value in new_dict.items():
  392. if key not in target_dict:
  393. print('Skipping unexpected key in config: {}'.format(key))
  394. continue
  395. if isinstance(target_dict[key], dict) and \
  396. isinstance(value, dict):
  397. update_dict(target_dict[key], value)
  398. else:
  399. target_dict[key] = value
  400. if os.path.exists(config_file):
  401. user_config = yaml.load(open(config_file)) or {}
  402. update_dict(config, user_config)
  403. # save config
  404. try:
  405. yaml.safe_dump(config, open(config_file, 'w'),
  406. default_flow_style=False)
  407. except Exception:
  408. warnings.warn('Failed to save config: {}'.format(config_file))
  409. return config
  410. def noShapes(self):
  411. return not self.labelList.itemsToShapes
  412. def populateModeActions(self):
  413. tool, menu = self.actions.tool, self.actions.menu
  414. self.tools.clear()
  415. addActions(self.tools, tool)
  416. self.canvas.menus[0].clear()
  417. addActions(self.canvas.menus[0], menu)
  418. self.menus.edit.clear()
  419. actions = (self.actions.createMode, self.actions.editMode)
  420. addActions(self.menus.edit, actions + self.actions.editMenu)
  421. def setDirty(self):
  422. self.dirty = True
  423. self.actions.save.setEnabled(True)
  424. title = __appname__
  425. if self.filename is not None:
  426. title = '{} - {}*'.format(title, self.filename)
  427. self.setWindowTitle(title)
  428. def setClean(self):
  429. self.dirty = False
  430. self.actions.save.setEnabled(False)
  431. self.actions.createMode.setEnabled(True)
  432. title = __appname__
  433. if self.filename is not None:
  434. title = '{} - {}'.format(title, self.filename)
  435. self.setWindowTitle(title)
  436. def toggleActions(self, value=True):
  437. """Enable/Disable widgets which depend on an opened image."""
  438. for z in self.actions.zoomActions:
  439. z.setEnabled(value)
  440. for action in self.actions.onLoadActive:
  441. action.setEnabled(value)
  442. def queueEvent(self, function):
  443. QtCore.QTimer.singleShot(0, function)
  444. def status(self, message, delay=5000):
  445. self.statusBar().showMessage(message, delay)
  446. def resetState(self):
  447. self.labelList.clear()
  448. self.filename = None
  449. self.imageData = None
  450. self.labelFile = None
  451. self.canvas.resetState()
  452. def currentItem(self):
  453. items = self.labelList.selectedItems()
  454. if items:
  455. return items[0]
  456. return None
  457. def addRecentFile(self, filename):
  458. if filename in self.recentFiles:
  459. self.recentFiles.remove(filename)
  460. elif len(self.recentFiles) >= self.maxRecent:
  461. self.recentFiles.pop()
  462. self.recentFiles.insert(0, filename)
  463. # Callbacks
  464. def tutorial(self):
  465. subprocess.Popen([self.screencastViewer, self.screencast])
  466. def toggleDrawingSensitive(self, drawing=True):
  467. """Toggle drawing sensitive.
  468. In the middle of drawing, toggling between modes should be disabled.
  469. """
  470. self.actions.editMode.setEnabled(not drawing)
  471. self.actions.undoLastPoint.setEnabled(drawing)
  472. def toggleDrawMode(self, edit=True):
  473. self.canvas.setEditing(edit)
  474. self.actions.createMode.setEnabled(edit)
  475. self.actions.editMode.setEnabled(not edit)
  476. def setCreateMode(self):
  477. self.toggleDrawMode(False)
  478. def setEditMode(self):
  479. self.toggleDrawMode(True)
  480. def updateFileMenu(self):
  481. current = self.filename
  482. def exists(filename):
  483. return os.path.exists(str(filename))
  484. menu = self.menus.recentFiles
  485. menu.clear()
  486. files = [f for f in self.recentFiles if f != current and exists(f)]
  487. for i, f in enumerate(files):
  488. icon = newIcon('labels')
  489. action = QtWidgets.QAction(
  490. icon, '&%d %s' % (i + 1, QtCore.QFileInfo(f).fileName()), self)
  491. action.triggered.connect(functools.partial(self.loadRecent, f))
  492. menu.addAction(action)
  493. def popLabelListMenu(self, point):
  494. self.menus.labelList.exec_(self.labelList.mapToGlobal(point))
  495. def editLabel(self, item=None):
  496. if not self.canvas.editing():
  497. return
  498. item = item if item else self.currentItem()
  499. text = self.labelDialog.popUp(item.text())
  500. if text is None:
  501. return
  502. item.setText(text)
  503. self.setDirty()
  504. if not self.uniqLabelList.findItems(text, Qt.MatchExactly):
  505. self.uniqLabelList.addItem(text)
  506. self.uniqLabelList.sortItems()
  507. def fileSelectionChanged(self):
  508. items = self.fileListWidget.selectedItems()
  509. if not items:
  510. return
  511. item = items[0]
  512. if not self.mayContinue():
  513. return
  514. currIndex = self.imageList.index(str(item.text()))
  515. if currIndex < len(self.imageList):
  516. filename = self.imageList[currIndex]
  517. if filename:
  518. self.loadFile(filename)
  519. # React to canvas signals.
  520. def shapeSelectionChanged(self, selected=False):
  521. if self._noSelectionSlot:
  522. self._noSelectionSlot = False
  523. else:
  524. shape = self.canvas.selectedShape
  525. if shape:
  526. item = self.labelList.get_item_from_shape(shape)
  527. item.setSelected(True)
  528. else:
  529. self.labelList.clearSelection()
  530. self.actions.delete.setEnabled(selected)
  531. self.actions.copy.setEnabled(selected)
  532. self.actions.edit.setEnabled(selected)
  533. self.actions.shapeLineColor.setEnabled(selected)
  534. self.actions.shapeFillColor.setEnabled(selected)
  535. def addLabel(self, shape):
  536. item = QtWidgets.QListWidgetItem(shape.label)
  537. item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
  538. item.setCheckState(Qt.Checked)
  539. self.labelList.itemsToShapes.append((item, shape))
  540. self.labelList.addItem(item)
  541. if not self.uniqLabelList.findItems(shape.label, Qt.MatchExactly):
  542. self.uniqLabelList.addItem(shape.label)
  543. self.uniqLabelList.sortItems()
  544. self.labelDialog.addLabelHistory(item.text())
  545. for action in self.actions.onShapesPresent:
  546. action.setEnabled(True)
  547. def remLabel(self, shape):
  548. item = self.labelList.get_item_from_shape(shape)
  549. self.labelList.takeItem(self.labelList.row(item))
  550. def loadLabels(self, shapes):
  551. s = []
  552. for label, points, line_color, fill_color in shapes:
  553. shape = Shape(label=label)
  554. for x, y in points:
  555. shape.addPoint(QtCore.QPointF(x, y))
  556. shape.close()
  557. s.append(shape)
  558. self.addLabel(shape)
  559. if line_color:
  560. shape.line_color = QtGui.QColor(*line_color)
  561. if fill_color:
  562. shape.fill_color = QtGui.QColor(*fill_color)
  563. self.canvas.loadShapes(s)
  564. def saveLabels(self, filename):
  565. lf = LabelFile()
  566. def format_shape(s):
  567. return dict(label=str(s.label),
  568. line_color=s.line_color.getRgb()
  569. if s.line_color != self.lineColor else None,
  570. fill_color=s.fill_color.getRgb()
  571. if s.fill_color != self.fillColor else None,
  572. points=[(p.x(), p.y()) for p in s.points])
  573. shapes = [format_shape(shape) for shape in self.labelList.shapes]
  574. try:
  575. imagePath = os.path.relpath(
  576. self.imagePath, os.path.dirname(filename))
  577. imageData = self.imageData if self._store_data else None
  578. lf.save(filename, shapes, imagePath, imageData,
  579. self.lineColor.getRgb(), self.fillColor.getRgb())
  580. self.labelFile = lf
  581. # disable allows next and previous image to proceed
  582. # self.filename = filename
  583. return True
  584. except LabelFileError as e:
  585. self.errorMessage('Error saving label data', '<b>%s</b>' % e)
  586. return False
  587. def copySelectedShape(self):
  588. self.addLabel(self.canvas.copySelectedShape())
  589. # fix copy and delete
  590. self.shapeSelectionChanged(True)
  591. def labelSelectionChanged(self):
  592. item = self.currentItem()
  593. if item and self.canvas.editing():
  594. self._noSelectionSlot = True
  595. shape = self.labelList.get_shape_from_item(item)
  596. self.canvas.selectShape(shape)
  597. def labelItemChanged(self, item):
  598. shape = self.labelList.get_shape_from_item(item)
  599. label = str(item.text())
  600. if label != shape.label:
  601. shape.label = str(item.text())
  602. self.setDirty()
  603. else: # User probably changed item visibility
  604. self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked)
  605. # Callback functions:
  606. def newShape(self):
  607. """Pop-up and give focus to the label editor.
  608. position MUST be in global coordinates.
  609. """
  610. items = self.uniqLabelList.selectedItems()
  611. text = None
  612. if items:
  613. text = items[0].text()
  614. text = self.labelDialog.popUp(text)
  615. if text is not None:
  616. self.addLabel(self.canvas.setLastLabel(text))
  617. self.actions.editMode.setEnabled(True)
  618. self.setDirty()
  619. else:
  620. self.canvas.undoLastLine()
  621. def scrollRequest(self, delta, orientation):
  622. units = - delta * 0.1 # natural scroll
  623. bar = self.scrollBars[orientation]
  624. bar.setValue(bar.value() + bar.singleStep() * units)
  625. def setZoom(self, value):
  626. self.actions.fitWidth.setChecked(False)
  627. self.actions.fitWindow.setChecked(False)
  628. self.zoomMode = self.MANUAL_ZOOM
  629. self.zoomWidget.setValue(value)
  630. def addZoom(self, increment=10):
  631. self.setZoom(self.zoomWidget.value() + increment)
  632. def zoomRequest(self, delta, pos):
  633. canvas_width_old = self.canvas.width()
  634. units = delta * 0.1
  635. self.addZoom(units)
  636. canvas_width_new = self.canvas.width()
  637. if canvas_width_old != canvas_width_new:
  638. canvas_scale_factor = canvas_width_new / canvas_width_old
  639. x_shift = round(pos.x() * canvas_scale_factor) - pos.x()
  640. y_shift = round(pos.y() * canvas_scale_factor) - pos.y()
  641. self.scrollBars[Qt.Horizontal].setValue(
  642. self.scrollBars[Qt.Horizontal].value() + x_shift)
  643. self.scrollBars[Qt.Vertical].setValue(
  644. self.scrollBars[Qt.Vertical].value() + y_shift)
  645. def setFitWindow(self, value=True):
  646. if value:
  647. self.actions.fitWidth.setChecked(False)
  648. self.zoomMode = self.FIT_WINDOW if value else self.MANUAL_ZOOM
  649. self.adjustScale()
  650. def setFitWidth(self, value=True):
  651. if value:
  652. self.actions.fitWindow.setChecked(False)
  653. self.zoomMode = self.FIT_WIDTH if value else self.MANUAL_ZOOM
  654. self.adjustScale()
  655. def togglePolygons(self, value):
  656. for item, shape in self.labelList.itemsToShapes:
  657. item.setCheckState(Qt.Checked if value else Qt.Unchecked)
  658. def loadFile(self, filename=None):
  659. """Load the specified file, or the last opened file if None."""
  660. self.resetState()
  661. self.canvas.setEnabled(False)
  662. if filename is None:
  663. filename = self.settings.value('filename', '')
  664. filename = str(filename)
  665. if not QtCore.QFile.exists(filename):
  666. self.errorMessage(
  667. 'Error opening file', 'No such file: <b>%s</b>' % filename)
  668. return False
  669. # assumes same name, but json extension
  670. self.status("Loading %s..." % os.path.basename(str(filename)))
  671. label_file = os.path.splitext(filename)[0] + '.json'
  672. if QtCore.QFile.exists(label_file) and \
  673. LabelFile.isLabelFile(label_file):
  674. try:
  675. self.labelFile = LabelFile(label_file)
  676. # FIXME: PyQt4 installed via Anaconda fails to load JPEG
  677. # and JSON encoded images.
  678. # https://github.com/ContinuumIO/anaconda-issues/issues/131
  679. if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
  680. raise LabelFileError(
  681. 'Failed loading image data from label file.\n'
  682. 'Maybe this is a known issue of PyQt4 built on'
  683. ' Anaconda, and may be fixed by installing PyQt5.')
  684. except LabelFileError as e:
  685. self.errorMessage(
  686. 'Error opening file',
  687. "<p><b>%s</b></p>"
  688. "<p>Make sure <i>%s</i> is a valid label file."
  689. % (e, label_file))
  690. self.status("Error reading %s" % label_file)
  691. return False
  692. self.imageData = self.labelFile.imageData
  693. self.imagePath = os.path.join(os.path.dirname(label_file),
  694. self.labelFile.imagePath)
  695. self.lineColor = QtGui.QColor(*self.labelFile.lineColor)
  696. self.fillColor = QtGui.QColor(*self.labelFile.fillColor)
  697. else:
  698. # Load image:
  699. # read data first and store for saving into label file.
  700. self.imageData = read(filename, None)
  701. if self.imageData is not None:
  702. # the filename is image not JSON
  703. self.imagePath = filename
  704. self.labelFile = None
  705. image = QtGui.QImage.fromData(self.imageData)
  706. if image.isNull():
  707. formats = ['*.{}'.format(fmt.data().decode())
  708. for fmt in QtGui.QImageReader.supportedImageFormats()]
  709. self.errorMessage(
  710. 'Error opening file',
  711. '<p>Make sure <i>{0}</i> is a valid image file.<br/>'
  712. 'Supported image formats: {1}</p>'
  713. .format(filename, ','.join(formats)))
  714. self.status("Error reading %s" % filename)
  715. return False
  716. self.image = image
  717. self.filename = filename
  718. self.canvas.loadPixmap(QtGui.QPixmap.fromImage(image))
  719. if self.labelFile:
  720. self.loadLabels(self.labelFile.shapes)
  721. self.setClean()
  722. self.canvas.setEnabled(True)
  723. self.adjustScale(initial=True)
  724. self.paintCanvas()
  725. self.addRecentFile(self.filename)
  726. self.toggleActions(True)
  727. self.status("Loaded %s" % os.path.basename(str(filename)))
  728. if filename in self.imageList:
  729. self.fileListWidget.setCurrentRow(self.imageList.index(filename))
  730. return True
  731. def resizeEvent(self, event):
  732. if self.canvas and not self.image.isNull()\
  733. and self.zoomMode != self.MANUAL_ZOOM:
  734. self.adjustScale()
  735. super(MainWindow, self).resizeEvent(event)
  736. def paintCanvas(self):
  737. assert not self.image.isNull(), "cannot paint null image"
  738. self.canvas.scale = 0.01 * self.zoomWidget.value()
  739. self.canvas.adjustSize()
  740. self.canvas.update()
  741. def adjustScale(self, initial=False):
  742. value = self.scalers[self.FIT_WINDOW if initial else self.zoomMode]()
  743. self.zoomWidget.setValue(int(100 * value))
  744. def scaleFitWindow(self):
  745. """Figure out the size of the pixmap to fit the main widget."""
  746. e = 2.0 # So that no scrollbars are generated.
  747. w1 = self.centralWidget().width() - e
  748. h1 = self.centralWidget().height() - e
  749. a1 = w1 / h1
  750. # Calculate a new scale value based on the pixmap's aspect ratio.
  751. w2 = self.canvas.pixmap.width() - 0.0
  752. h2 = self.canvas.pixmap.height() - 0.0
  753. a2 = w2 / h2
  754. return w1 / w2 if a2 >= a1 else h1 / h2
  755. def scaleFitWidth(self):
  756. # The epsilon does not seem to work too well here.
  757. w = self.centralWidget().width() - 2.0
  758. return w / self.canvas.pixmap.width()
  759. def closeEvent(self, event):
  760. if not self.mayContinue():
  761. event.ignore()
  762. self.settings.setValue(
  763. 'filename', self.filename if self.filename else '')
  764. self.settings.setValue('window/size', self.size())
  765. self.settings.setValue('window/position', self.pos())
  766. self.settings.setValue('window/state', self.saveState())
  767. self.settings.setValue('line/color', self.lineColor)
  768. self.settings.setValue('fill/color', self.fillColor)
  769. self.settings.setValue('recentFiles', self.recentFiles)
  770. # ask the use for where to save the labels
  771. # self.settings.setValue('window/geometry', self.saveGeometry())
  772. # User Dialogs #
  773. def loadRecent(self, filename):
  774. if self.mayContinue():
  775. self.loadFile(filename)
  776. def openPrevImg(self, _value=False):
  777. if not self.mayContinue():
  778. return
  779. if len(self.imageList) <= 0:
  780. return
  781. if self.filename is None:
  782. return
  783. currIndex = self.imageList.index(self.filename)
  784. if currIndex - 1 >= 0:
  785. filename = self.imageList[currIndex - 1]
  786. if filename:
  787. self.loadFile(filename)
  788. def openNextImg(self, _value=False):
  789. if not self.mayContinue():
  790. return
  791. if len(self.imageList) <= 0:
  792. return
  793. filename = None
  794. if self.filename is None:
  795. filename = self.imageList[0]
  796. else:
  797. currIndex = self.imageList.index(self.filename)
  798. if currIndex + 1 < len(self.imageList):
  799. filename = self.imageList[currIndex + 1]
  800. if filename:
  801. self.loadFile(filename)
  802. def openFile(self, _value=False):
  803. if not self.mayContinue():
  804. return
  805. path = os.path.dirname(str(self.filename)) if self.filename else '.'
  806. formats = ['*.{}'.format(fmt.data().decode())
  807. for fmt in QtGui.QImageReader.supportedImageFormats()]
  808. filters = "Image & Label files (%s)" % ' '.join(
  809. formats + ['*%s' % LabelFile.suffix])
  810. filename = QtWidgets.QFileDialog.getOpenFileName(
  811. self, '%s - Choose Image or Label file' % __appname__,
  812. path, filters)
  813. if QT5:
  814. filename, _ = filename
  815. filename = str(filename)
  816. if filename:
  817. self.loadFile(filename)
  818. def saveFile(self, _value=False):
  819. assert not self.image.isNull(), "cannot save empty image"
  820. if self.hasLabels():
  821. if self.labelFile:
  822. # DL20180323 - overwrite when in directory
  823. self._saveFile(self.labelFile.filename)
  824. elif self.output:
  825. self._saveFile(self.output)
  826. else:
  827. self._saveFile(self.saveFileDialog())
  828. def saveFileAs(self, _value=False):
  829. assert not self.image.isNull(), "cannot save empty image"
  830. if self.hasLabels():
  831. self._saveFile(self.saveFileDialog())
  832. def saveFileDialog(self):
  833. caption = '%s - Choose File' % __appname__
  834. filters = 'Label files (*%s)' % LabelFile.suffix
  835. dlg = QtWidgets.QFileDialog(self, caption, self.currentPath(), filters)
  836. dlg.setDefaultSuffix(LabelFile.suffix[1:])
  837. dlg.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
  838. dlg.setOption(QtWidgets.QFileDialog.DontConfirmOverwrite, False)
  839. dlg.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, False)
  840. basename = os.path.splitext(self.filename)[0]
  841. default_labelfile_name = os.path.join(
  842. self.currentPath(), basename + LabelFile.suffix)
  843. filename = dlg.getSaveFileName(
  844. self, 'Choose File', default_labelfile_name,
  845. 'Label files (*%s)' % LabelFile.suffix)
  846. if QT5:
  847. filename, _ = filename
  848. filename = str(filename)
  849. return filename
  850. def _saveFile(self, filename):
  851. if filename and self.saveLabels(filename):
  852. self.addRecentFile(filename)
  853. self.setClean()
  854. if self.labeling_once:
  855. self.close()
  856. def closeFile(self, _value=False):
  857. if not self.mayContinue():
  858. return
  859. self.resetState()
  860. self.setClean()
  861. self.toggleActions(False)
  862. self.canvas.setEnabled(False)
  863. self.actions.saveAs.setEnabled(False)
  864. # Message Dialogs. #
  865. def hasLabels(self):
  866. if not self.labelList.itemsToShapes:
  867. self.errorMessage(
  868. 'No objects labeled',
  869. 'You must label at least one object to save the file.')
  870. return False
  871. return True
  872. def mayContinue(self):
  873. if not self.dirty:
  874. return True
  875. mb = QtWidgets.QMessageBox
  876. msg = 'Save annotations to "{}" before closing?'.format(self.filename)
  877. answer = mb.question(self,
  878. 'Save annotations?',
  879. msg,
  880. mb.Save | mb.Discard | mb.Cancel,
  881. mb.Save)
  882. if answer == mb.Discard:
  883. return True
  884. elif answer == mb.Save:
  885. self.saveFile()
  886. return True
  887. else: # answer == mb.Cancel
  888. return False
  889. def errorMessage(self, title, message):
  890. return QtWidgets.QMessageBox.critical(
  891. self, title, '<p><b>%s</b></p>%s' % (title, message))
  892. def currentPath(self):
  893. return os.path.dirname(str(self.filename)) if self.filename else '.'
  894. def chooseColor1(self):
  895. color = self.colorDialog.getColor(
  896. self.lineColor, 'Choose line color', default=DEFAULT_LINE_COLOR)
  897. if color:
  898. self.lineColor = color
  899. # Change the color for all shape lines:
  900. Shape.line_color = self.lineColor
  901. self.canvas.update()
  902. self.setDirty()
  903. def chooseColor2(self):
  904. color = self.colorDialog.getColor(
  905. self.fillColor, 'Choose fill color', default=DEFAULT_FILL_COLOR)
  906. if color:
  907. self.fillColor = color
  908. Shape.fill_color = self.fillColor
  909. self.canvas.update()
  910. self.setDirty()
  911. def deleteSelectedShape(self):
  912. yes, no = QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No
  913. msg = 'You are about to permanently delete this polygon, ' \
  914. 'proceed anyway?'
  915. if yes == QtWidgets.QMessageBox.warning(self, 'Attention', msg,
  916. yes | no):
  917. self.remLabel(self.canvas.deleteSelected())
  918. self.setDirty()
  919. if self.noShapes():
  920. for action in self.actions.onShapesPresent:
  921. action.setEnabled(False)
  922. def chshapeLineColor(self):
  923. color = self.colorDialog.getColor(
  924. self.lineColor, 'Choose line color', default=DEFAULT_LINE_COLOR)
  925. if color:
  926. self.canvas.selectedShape.line_color = color
  927. self.canvas.update()
  928. self.setDirty()
  929. def chshapeFillColor(self):
  930. color = self.colorDialog.getColor(
  931. self.fillColor, 'Choose fill color', default=DEFAULT_FILL_COLOR)
  932. if color:
  933. self.canvas.selectedShape.fill_color = color
  934. self.canvas.update()
  935. self.setDirty()
  936. def copyShape(self):
  937. self.canvas.endMove(copy=True)
  938. self.addLabel(self.canvas.selectedShape)
  939. self.setDirty()
  940. def moveShape(self):
  941. self.canvas.endMove(copy=False)
  942. self.setDirty()
  943. def openDirDialog(self, _value=False, dirpath=None):
  944. if not self.mayContinue():
  945. return
  946. defaultOpenDirPath = dirpath if dirpath else '.'
  947. if self.lastOpenDir and os.path.exists(self.lastOpenDir):
  948. defaultOpenDirPath = self.lastOpenDir
  949. else:
  950. defaultOpenDirPath = os.path.dirname(self.filename) \
  951. if self.filename else '.'
  952. targetDirPath = str(QtWidgets.QFileDialog.getExistingDirectory(
  953. self, '%s - Open Directory' % __appname__, defaultOpenDirPath,
  954. QtWidgets.QFileDialog.ShowDirsOnly |
  955. QtWidgets.QFileDialog.DontResolveSymlinks))
  956. self.importDirImages(targetDirPath)
  957. @property
  958. def imageList(self):
  959. lst = []
  960. for i in range(self.fileListWidget.count()):
  961. item = self.fileListWidget.item(i)
  962. lst.append(item.text())
  963. return lst
  964. def importDirImages(self, dirpath):
  965. if not self.mayContinue() or not dirpath:
  966. return
  967. self.lastOpenDir = dirpath
  968. self.filename = None
  969. self.fileListWidget.clear()
  970. for imgPath in self.scanAllImages(dirpath):
  971. item = QtWidgets.QListWidgetItem(imgPath)
  972. self.fileListWidget.addItem(item)
  973. self.openNextImg()
  974. def scanAllImages(self, folderPath):
  975. extensions = ['.%s' % fmt.data().decode("ascii").lower()
  976. for fmt in QtGui.QImageReader.supportedImageFormats()]
  977. images = []
  978. for root, dirs, files in os.walk(folderPath):
  979. for file in files:
  980. if file.lower().endswith(tuple(extensions)):
  981. relativePath = os.path.join(root, file)
  982. images.append(relativePath)
  983. images.sort(key=lambda x: x.lower())
  984. return images
  985. def inverted(color):
  986. return QtGui.QColor(*[255 - v for v in color.getRgb()])
  987. def read(filename, default=None):
  988. try:
  989. with open(filename, 'rb') as f:
  990. return f.read()
  991. except Exception:
  992. return default
  993. def main():
  994. """Standard boilerplate Qt application code."""
  995. parser = argparse.ArgumentParser()
  996. parser.add_argument('filename', nargs='?', help='image or label filename')
  997. parser.add_argument('--output', '-O', '-o', help='output label name')
  998. parser.add_argument('--nodata', dest='store_data', action='store_false',
  999. help='stop storing image data to JSON file')
  1000. parser.add_argument('--labels',
  1001. help='comma separated list of labels OR file '
  1002. 'containing one label per line')
  1003. parser.add_argument('--nosortlabels', dest='sort_labels',
  1004. action='store_false', help='stop sorting labels')
  1005. args = parser.parse_args()
  1006. if args.labels is not None:
  1007. if os.path.isfile(args.labels):
  1008. args.labels = [l.strip() for l in open(args.labels, 'r')
  1009. if l.strip()]
  1010. else:
  1011. args.labels = [l for l in args.labels.split(',') if l]
  1012. app = QtWidgets.QApplication(sys.argv)
  1013. app.setApplicationName(__appname__)
  1014. app.setWindowIcon(newIcon("icon"))
  1015. win = MainWindow(
  1016. filename=args.filename,
  1017. output=args.output,
  1018. store_data=args.store_data,
  1019. labels=args.labels,
  1020. sort_labels=args.sort_labels,
  1021. )
  1022. win.show()
  1023. win.raise_()
  1024. sys.exit(app.exec_())