canvas.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. from qtpy import QtCore
  2. from qtpy import QtGui
  3. from qtpy import QtWidgets
  4. from labelme import QT5
  5. from labelme.shape import Shape
  6. import labelme.utils
  7. # TODO(unknown):
  8. # - [maybe] Find optimal epsilon value.
  9. CURSOR_DEFAULT = QtCore.Qt.ArrowCursor
  10. CURSOR_POINT = QtCore.Qt.PointingHandCursor
  11. CURSOR_DRAW = QtCore.Qt.CrossCursor
  12. CURSOR_MOVE = QtCore.Qt.ClosedHandCursor
  13. CURSOR_GRAB = QtCore.Qt.OpenHandCursor
  14. class Canvas(QtWidgets.QWidget):
  15. zoomRequest = QtCore.Signal(int, QtCore.QPoint)
  16. scrollRequest = QtCore.Signal(int, int)
  17. newShape = QtCore.Signal()
  18. selectionChanged = QtCore.Signal(list)
  19. shapeMoved = QtCore.Signal()
  20. drawingPolygon = QtCore.Signal(bool)
  21. vertexSelected = QtCore.Signal(bool)
  22. CREATE, EDIT = 0, 1
  23. CREATE, EDIT = 0, 1
  24. # polygon, rectangle, line, or point
  25. _createMode = "polygon"
  26. _fill_drawing = False
  27. def __init__(self, *args, **kwargs):
  28. self.epsilon = kwargs.pop("epsilon", 10.0)
  29. self.double_click = kwargs.pop("double_click", "close")
  30. if self.double_click not in [None, "close"]:
  31. raise ValueError(
  32. "Unexpected value for double_click event: {}".format(
  33. self.double_click
  34. )
  35. )
  36. self.num_backups = kwargs.pop("num_backups", 10)
  37. super(Canvas, self).__init__(*args, **kwargs)
  38. # Initialise local state.
  39. self.mode = self.EDIT
  40. self.shapes = []
  41. self.shapesBackups = []
  42. self.current = None
  43. self.selectedShapes = [] # save the selected shapes here
  44. self.selectedShapesCopy = []
  45. # self.line represents:
  46. # - createMode == 'polygon': edge from last point to current
  47. # - createMode == 'rectangle': diagonal line of the rectangle
  48. # - createMode == 'line': the line
  49. # - createMode == 'point': the point
  50. self.line = Shape()
  51. self.prevPoint = QtCore.QPoint()
  52. self.prevMovePoint = QtCore.QPoint()
  53. self.offsets = QtCore.QPoint(), QtCore.QPoint()
  54. self.scale = 1.0
  55. self.pixmap = QtGui.QPixmap()
  56. self.visible = {}
  57. self._hideBackround = False
  58. self.hideBackround = False
  59. self.hShape = None
  60. self.prevhShape = None
  61. self.hVertex = None
  62. self.prevhVertex = None
  63. self.hEdge = None
  64. self.prevhEdge = None
  65. self.movingShape = False
  66. self.snapping = True
  67. self._painter = QtGui.QPainter()
  68. self._cursor = CURSOR_DEFAULT
  69. # Menus:
  70. # 0: right-click without selection and dragging of shapes
  71. # 1: right-click with selection and dragging of shapes
  72. self.menus = (QtWidgets.QMenu(), QtWidgets.QMenu())
  73. # Set widget options.
  74. self.setMouseTracking(True)
  75. self.setFocusPolicy(QtCore.Qt.WheelFocus)
  76. def fillDrawing(self):
  77. return self._fill_drawing
  78. def setFillDrawing(self, value):
  79. self._fill_drawing = value
  80. @property
  81. def createMode(self):
  82. return self._createMode
  83. @createMode.setter
  84. def createMode(self, value):
  85. if value not in [
  86. "polygon",
  87. "rectangle",
  88. "circle",
  89. "line",
  90. "point",
  91. "linestrip",
  92. ]:
  93. raise ValueError("Unsupported createMode: %s" % value)
  94. self._createMode = value
  95. def storeShapes(self):
  96. shapesBackup = []
  97. for shape in self.shapes:
  98. shapesBackup.append(shape.copy())
  99. if len(self.shapesBackups) > self.num_backups:
  100. self.shapesBackups = self.shapesBackups[-self.num_backups - 1 :]
  101. self.shapesBackups.append(shapesBackup)
  102. @property
  103. def isShapeRestorable(self):
  104. # We save the state AFTER each edit (not before) so for an
  105. # edit to be undoable, we expect the CURRENT and the PREVIOUS state
  106. # to be in the undo stack.
  107. if len(self.shapesBackups) < 2:
  108. return False
  109. return True
  110. def restoreShape(self):
  111. # This does _part_ of the job of restoring shapes.
  112. # The complete process is also done in app.py::undoShapeEdit
  113. # and app.py::loadShapes and our own Canvas::loadShapes function.
  114. if not self.isShapeRestorable:
  115. return
  116. self.shapesBackups.pop() # latest
  117. # The application will eventually call Canvas.loadShapes which will
  118. # push this right back onto the stack.
  119. shapesBackup = self.shapesBackups.pop()
  120. self.shapes = shapesBackup
  121. self.selectedShapes = []
  122. for shape in self.shapes:
  123. shape.selected = False
  124. self.update()
  125. def enterEvent(self, ev):
  126. self.overrideCursor(self._cursor)
  127. def leaveEvent(self, ev):
  128. self.unHighlight()
  129. self.restoreCursor()
  130. def focusOutEvent(self, ev):
  131. self.restoreCursor()
  132. def isVisible(self, shape):
  133. return self.visible.get(shape, True)
  134. def drawing(self):
  135. return self.mode == self.CREATE
  136. def editing(self):
  137. return self.mode == self.EDIT
  138. def setEditing(self, value=True):
  139. self.mode = self.EDIT if value else self.CREATE
  140. if not value: # Create
  141. self.unHighlight()
  142. self.deSelectShape()
  143. def unHighlight(self):
  144. if self.hShape:
  145. self.hShape.highlightClear()
  146. self.update()
  147. self.prevhShape = self.hShape
  148. self.prevhVertex = self.hVertex
  149. self.prevhEdge = self.hEdge
  150. self.hShape = self.hVertex = self.hEdge = None
  151. def selectedVertex(self):
  152. return self.hVertex is not None
  153. def selectedEdge(self):
  154. return self.hEdge is not None
  155. def mouseMoveEvent(self, ev):
  156. """Update line with last point and current coordinates."""
  157. try:
  158. if QT5:
  159. pos = self.transformPos(ev.localPos())
  160. else:
  161. pos = self.transformPos(ev.posF())
  162. except AttributeError:
  163. return
  164. self.prevMovePoint = pos
  165. self.restoreCursor()
  166. # Polygon drawing.
  167. if self.drawing():
  168. self.line.shape_type = self.createMode
  169. self.overrideCursor(CURSOR_DRAW)
  170. if not self.current:
  171. return
  172. if self.outOfPixmap(pos):
  173. # Don't allow the user to draw outside the pixmap.
  174. # Project the point to the pixmap's edges.
  175. pos = self.intersectionPoint(self.current[-1], pos)
  176. elif (
  177. self.snapping
  178. and len(self.current) > 1
  179. and self.createMode == "polygon"
  180. and self.closeEnough(pos, self.current[0])
  181. ):
  182. # Attract line to starting point and
  183. # colorise to alert the user.
  184. pos = self.current[0]
  185. self.overrideCursor(CURSOR_POINT)
  186. self.current.highlightVertex(0, Shape.NEAR_VERTEX)
  187. if self.createMode in ["polygon", "linestrip"]:
  188. self.line[0] = self.current[-1]
  189. self.line[1] = pos
  190. elif self.createMode == "rectangle":
  191. self.line.points = [self.current[0], pos]
  192. self.line.close()
  193. elif self.createMode == "circle":
  194. self.line.points = [self.current[0], pos]
  195. self.line.shape_type = "circle"
  196. elif self.createMode == "line":
  197. self.line.points = [self.current[0], pos]
  198. self.line.close()
  199. elif self.createMode == "point":
  200. self.line.points = [self.current[0]]
  201. self.line.close()
  202. self.repaint()
  203. self.current.highlightClear()
  204. return
  205. # Polygon copy moving.
  206. if QtCore.Qt.RightButton & ev.buttons():
  207. if self.selectedShapesCopy and self.prevPoint:
  208. self.overrideCursor(CURSOR_MOVE)
  209. self.boundedMoveShapes(self.selectedShapesCopy, pos)
  210. self.repaint()
  211. elif self.selectedShapes:
  212. self.selectedShapesCopy = [
  213. s.copy() for s in self.selectedShapes
  214. ]
  215. self.repaint()
  216. return
  217. # Polygon/Vertex moving.
  218. if QtCore.Qt.LeftButton & ev.buttons():
  219. if self.selectedVertex():
  220. self.boundedMoveVertex(pos)
  221. self.repaint()
  222. self.movingShape = True
  223. elif self.selectedShapes and self.prevPoint:
  224. self.overrideCursor(CURSOR_MOVE)
  225. self.boundedMoveShapes(self.selectedShapes, pos)
  226. self.repaint()
  227. self.movingShape = True
  228. return
  229. # Just hovering over the canvas, 2 possibilities:
  230. # - Highlight shapes
  231. # - Highlight vertex
  232. # Update shape/vertex fill and tooltip value accordingly.
  233. self.setToolTip(self.tr("Image"))
  234. for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
  235. # Look for a nearby vertex to highlight. If that fails,
  236. # check if we happen to be inside a shape.
  237. index = shape.nearestVertex(pos, self.epsilon / self.scale)
  238. index_edge = shape.nearestEdge(pos, self.epsilon / self.scale)
  239. if index is not None:
  240. if self.selectedVertex():
  241. self.hShape.highlightClear()
  242. self.prevhVertex = self.hVertex = index
  243. self.prevhShape = self.hShape = shape
  244. self.prevhEdge = self.hEdge
  245. self.hEdge = None
  246. shape.highlightVertex(index, shape.MOVE_VERTEX)
  247. self.overrideCursor(CURSOR_POINT)
  248. self.setToolTip(self.tr("Click & drag to move point"))
  249. self.setStatusTip(self.toolTip())
  250. self.update()
  251. break
  252. elif index_edge is not None and shape.canAddPoint():
  253. if self.selectedVertex():
  254. self.hShape.highlightClear()
  255. self.prevhVertex = self.hVertex
  256. self.hVertex = None
  257. self.prevhShape = self.hShape = shape
  258. self.prevhEdge = self.hEdge = index_edge
  259. self.overrideCursor(CURSOR_POINT)
  260. self.setToolTip(self.tr("Click to create point"))
  261. self.setStatusTip(self.toolTip())
  262. self.update()
  263. break
  264. elif shape.containsPoint(pos):
  265. if self.selectedVertex():
  266. self.hShape.highlightClear()
  267. self.prevhVertex = self.hVertex
  268. self.hVertex = None
  269. self.prevhShape = self.hShape = shape
  270. self.prevhEdge = self.hEdge
  271. self.hEdge = None
  272. self.setToolTip(
  273. self.tr("Click & drag to move shape '%s'") % shape.label
  274. )
  275. self.setStatusTip(self.toolTip())
  276. self.overrideCursor(CURSOR_GRAB)
  277. self.update()
  278. break
  279. else: # Nothing found, clear highlights, reset state.
  280. self.unHighlight()
  281. self.vertexSelected.emit(self.hVertex is not None)
  282. def addPointToEdge(self):
  283. shape = self.prevhShape
  284. index = self.prevhEdge
  285. point = self.prevMovePoint
  286. if shape is None or index is None or point is None:
  287. return
  288. shape.insertPoint(index, point)
  289. shape.highlightVertex(index, shape.MOVE_VERTEX)
  290. self.hShape = shape
  291. self.hVertex = index
  292. self.hEdge = None
  293. self.movingShape = True
  294. def removeSelectedPoint(self):
  295. shape = self.prevhShape
  296. point = self.prevMovePoint
  297. if shape is None or point is None:
  298. return
  299. index = shape.nearestVertex(point, self.epsilon)
  300. shape.removePoint(index)
  301. # shape.highlightVertex(index, shape.MOVE_VERTEX)
  302. self.hShape = shape
  303. self.hVertex = None
  304. self.hEdge = None
  305. self.movingShape = True # Save changes
  306. def mousePressEvent(self, ev):
  307. if QT5:
  308. pos = self.transformPos(ev.localPos())
  309. else:
  310. pos = self.transformPos(ev.posF())
  311. if ev.button() == QtCore.Qt.LeftButton:
  312. if self.drawing():
  313. if self.current:
  314. # Add point to existing shape.
  315. if self.createMode == "polygon":
  316. self.current.addPoint(self.line[1])
  317. self.line[0] = self.current[-1]
  318. if self.current.isClosed():
  319. self.finalise()
  320. elif self.createMode in ["rectangle", "circle", "line"]:
  321. assert len(self.current.points) == 1
  322. self.current.points = self.line.points
  323. self.finalise()
  324. elif self.createMode == "linestrip":
  325. self.current.addPoint(self.line[1])
  326. self.line[0] = self.current[-1]
  327. if int(ev.modifiers()) == QtCore.Qt.ControlModifier:
  328. self.finalise()
  329. elif not self.outOfPixmap(pos):
  330. # Create new shape.
  331. self.current = Shape(shape_type=self.createMode)
  332. self.current.addPoint(pos)
  333. if self.createMode == "point":
  334. self.finalise()
  335. else:
  336. if self.createMode == "circle":
  337. self.current.shape_type = "circle"
  338. self.line.points = [pos, pos]
  339. self.setHiding()
  340. self.drawingPolygon.emit(True)
  341. self.update()
  342. elif self.editing():
  343. if self.selectedEdge():
  344. self.addPointToEdge()
  345. group_mode = int(ev.modifiers()) == QtCore.Qt.ControlModifier
  346. self.selectShapePoint(pos, multiple_selection_mode=group_mode)
  347. self.prevPoint = pos
  348. self.repaint()
  349. elif ev.button() == QtCore.Qt.RightButton and self.editing():
  350. group_mode = int(ev.modifiers()) == QtCore.Qt.ControlModifier
  351. self.selectShapePoint(pos, multiple_selection_mode=group_mode)
  352. self.prevPoint = pos
  353. self.repaint()
  354. def mouseReleaseEvent(self, ev):
  355. if ev.button() == QtCore.Qt.RightButton:
  356. menu = self.menus[len(self.selectedShapesCopy) > 0]
  357. self.restoreCursor()
  358. if (
  359. not menu.exec_(self.mapToGlobal(ev.pos()))
  360. and self.selectedShapesCopy
  361. ):
  362. # Cancel the move by deleting the shadow copy.
  363. self.selectedShapesCopy = []
  364. self.repaint()
  365. elif ev.button() == QtCore.Qt.LeftButton and self.selectedVertex():
  366. if (
  367. self.editing()
  368. and int(ev.modifiers()) == QtCore.Qt.ShiftModifier
  369. ):
  370. # Delete point if: left-click + SHIFT on a point
  371. self.removeSelectedPoint()
  372. if self.movingShape and self.hShape:
  373. index = self.shapes.index(self.hShape)
  374. if (
  375. self.shapesBackups[-1][index].points
  376. != self.shapes[index].points
  377. ):
  378. self.storeShapes()
  379. self.shapeMoved.emit()
  380. self.movingShape = False
  381. def endMove(self, copy):
  382. assert self.selectedShapes and self.selectedShapesCopy
  383. assert len(self.selectedShapesCopy) == len(self.selectedShapes)
  384. if copy:
  385. for i, shape in enumerate(self.selectedShapesCopy):
  386. self.shapes.append(shape)
  387. self.selectedShapes[i].selected = False
  388. self.selectedShapes[i] = shape
  389. else:
  390. for i, shape in enumerate(self.selectedShapesCopy):
  391. self.selectedShapes[i].points = shape.points
  392. self.selectedShapesCopy = []
  393. self.repaint()
  394. self.storeShapes()
  395. return True
  396. def hideBackroundShapes(self, value):
  397. self.hideBackround = value
  398. if self.selectedShapes:
  399. # Only hide other shapes if there is a current selection.
  400. # Otherwise the user will not be able to select a shape.
  401. self.setHiding(True)
  402. self.update()
  403. def setHiding(self, enable=True):
  404. self._hideBackround = self.hideBackround if enable else False
  405. def canCloseShape(self):
  406. return self.drawing() and self.current and len(self.current) > 2
  407. def mouseDoubleClickEvent(self, ev):
  408. # We need at least 4 points here, since the mousePress handler
  409. # adds an extra one before this handler is called.
  410. if (
  411. self.double_click == "close"
  412. and self.canCloseShape()
  413. and len(self.current) > 3
  414. ):
  415. self.current.popPoint()
  416. self.finalise()
  417. def selectShapes(self, shapes):
  418. self.setHiding()
  419. self.selectionChanged.emit(shapes)
  420. self.update()
  421. def selectShapePoint(self, point, multiple_selection_mode):
  422. """Select the first shape created which contains this point."""
  423. if self.selectedVertex(): # A vertex is marked for selection.
  424. index, shape = self.hVertex, self.hShape
  425. shape.highlightVertex(index, shape.MOVE_VERTEX)
  426. else:
  427. for shape in reversed(self.shapes):
  428. if self.isVisible(shape) and shape.containsPoint(point):
  429. self.calculateOffsets(shape, point)
  430. self.setHiding()
  431. if multiple_selection_mode:
  432. if shape not in self.selectedShapes:
  433. self.selectionChanged.emit(
  434. self.selectedShapes + [shape]
  435. )
  436. else:
  437. self.selectionChanged.emit([shape])
  438. return
  439. self.deSelectShape()
  440. def calculateOffsets(self, shape, point):
  441. rect = shape.boundingRect()
  442. x1 = rect.x() - point.x()
  443. y1 = rect.y() - point.y()
  444. x2 = (rect.x() + rect.width() - 1) - point.x()
  445. y2 = (rect.y() + rect.height() - 1) - point.y()
  446. self.offsets = QtCore.QPoint(x1, y1), QtCore.QPoint(x2, y2)
  447. def boundedMoveVertex(self, pos):
  448. index, shape = self.hVertex, self.hShape
  449. point = shape[index]
  450. if self.outOfPixmap(pos):
  451. pos = self.intersectionPoint(point, pos)
  452. shape.moveVertexBy(index, pos - point)
  453. def boundedMoveShapes(self, shapes, pos):
  454. if self.outOfPixmap(pos):
  455. return False # No need to move
  456. o1 = pos + self.offsets[0]
  457. if self.outOfPixmap(o1):
  458. pos -= QtCore.QPoint(min(0, o1.x()), min(0, o1.y()))
  459. o2 = pos + self.offsets[1]
  460. if self.outOfPixmap(o2):
  461. pos += QtCore.QPoint(
  462. min(0, self.pixmap.width() - o2.x()),
  463. min(0, self.pixmap.height() - o2.y()),
  464. )
  465. # XXX: The next line tracks the new position of the cursor
  466. # relative to the shape, but also results in making it
  467. # a bit "shaky" when nearing the border and allows it to
  468. # go outside of the shape's area for some reason.
  469. # self.calculateOffsets(self.selectedShapes, pos)
  470. dp = pos - self.prevPoint
  471. if dp:
  472. for shape in shapes:
  473. shape.moveBy(dp)
  474. self.prevPoint = pos
  475. return True
  476. return False
  477. def deSelectShape(self):
  478. if self.selectedShapes:
  479. self.setHiding(False)
  480. self.selectionChanged.emit([])
  481. self.update()
  482. def deleteSelected(self):
  483. deleted_shapes = []
  484. if self.selectedShapes:
  485. for shape in self.selectedShapes:
  486. self.shapes.remove(shape)
  487. deleted_shapes.append(shape)
  488. self.storeShapes()
  489. self.selectedShapes = []
  490. self.update()
  491. return deleted_shapes
  492. def deleteShape(self, shape):
  493. if shape in self.selectedShapes:
  494. self.selectedShapes.remove(shape)
  495. if shape in self.shapes:
  496. self.shapes.remove(shape)
  497. self.storeShapes()
  498. self.update()
  499. def copySelectedShapes(self):
  500. if self.selectedShapes:
  501. self.selectedShapesCopy = [s.copy() for s in self.selectedShapes]
  502. self.boundedShiftShapes(self.selectedShapesCopy)
  503. self.endMove(copy=True)
  504. return self.selectedShapes
  505. def boundedShiftShapes(self, shapes):
  506. # Try to move in one direction, and if it fails in another.
  507. # Give up if both fail.
  508. point = shapes[0][0]
  509. offset = QtCore.QPoint(2.0, 2.0)
  510. self.offsets = QtCore.QPoint(), QtCore.QPoint()
  511. self.prevPoint = point
  512. if not self.boundedMoveShapes(shapes, point - offset):
  513. self.boundedMoveShapes(shapes, point + offset)
  514. def paintEvent(self, event):
  515. if not self.pixmap:
  516. return super(Canvas, self).paintEvent(event)
  517. p = self._painter
  518. p.begin(self)
  519. p.setRenderHint(QtGui.QPainter.Antialiasing)
  520. p.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
  521. p.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
  522. p.scale(self.scale, self.scale)
  523. p.translate(self.offsetToCenter())
  524. p.drawPixmap(0, 0, self.pixmap)
  525. Shape.scale = self.scale
  526. for shape in self.shapes:
  527. if (shape.selected or not self._hideBackround) and self.isVisible(
  528. shape
  529. ):
  530. shape.fill = shape.selected or shape == self.hShape
  531. shape.paint(p)
  532. if self.current:
  533. self.current.paint(p)
  534. self.line.paint(p)
  535. if self.selectedShapesCopy:
  536. for s in self.selectedShapesCopy:
  537. s.paint(p)
  538. if (
  539. self.fillDrawing()
  540. and self.createMode == "polygon"
  541. and self.current is not None
  542. and len(self.current.points) >= 2
  543. ):
  544. drawing_shape = self.current.copy()
  545. drawing_shape.addPoint(self.line[1])
  546. drawing_shape.fill = True
  547. drawing_shape.paint(p)
  548. p.end()
  549. def transformPos(self, point):
  550. """Convert from widget-logical coordinates to painter-logical ones."""
  551. return point / self.scale - self.offsetToCenter()
  552. def offsetToCenter(self):
  553. s = self.scale
  554. area = super(Canvas, self).size()
  555. w, h = self.pixmap.width() * s, self.pixmap.height() * s
  556. aw, ah = area.width(), area.height()
  557. x = (aw - w) / (2 * s) if aw > w else 0
  558. y = (ah - h) / (2 * s) if ah > h else 0
  559. return QtCore.QPoint(x, y)
  560. def outOfPixmap(self, p):
  561. w, h = self.pixmap.width(), self.pixmap.height()
  562. return not (0 <= p.x() <= w - 1 and 0 <= p.y() <= h - 1)
  563. def finalise(self):
  564. assert self.current
  565. self.current.close()
  566. self.shapes.append(self.current)
  567. self.storeShapes()
  568. self.current = None
  569. self.setHiding(False)
  570. self.newShape.emit()
  571. self.update()
  572. def closeEnough(self, p1, p2):
  573. # d = distance(p1 - p2)
  574. # m = (p1-p2).manhattanLength()
  575. # print "d %.2f, m %d, %.2f" % (d, m, d - m)
  576. # divide by scale to allow more precision when zoomed in
  577. return labelme.utils.distance(p1 - p2) < (self.epsilon / self.scale)
  578. def intersectionPoint(self, p1, p2):
  579. # Cycle through each image edge in clockwise fashion,
  580. # and find the one intersecting the current line segment.
  581. # http://paulbourke.net/geometry/lineline2d/
  582. size = self.pixmap.size()
  583. points = [
  584. (0, 0),
  585. (size.width() - 1, 0),
  586. (size.width() - 1, size.height() - 1),
  587. (0, size.height() - 1),
  588. ]
  589. # x1, y1 should be in the pixmap, x2, y2 should be out of the pixmap
  590. x1 = min(max(p1.x(), 0), size.width() - 1)
  591. y1 = min(max(p1.y(), 0), size.height() - 1)
  592. x2, y2 = p2.x(), p2.y()
  593. d, i, (x, y) = min(self.intersectingEdges((x1, y1), (x2, y2), points))
  594. x3, y3 = points[i]
  595. x4, y4 = points[(i + 1) % 4]
  596. if (x, y) == (x1, y1):
  597. # Handle cases where previous point is on one of the edges.
  598. if x3 == x4:
  599. return QtCore.QPoint(x3, min(max(0, y2), max(y3, y4)))
  600. else: # y3 == y4
  601. return QtCore.QPoint(min(max(0, x2), max(x3, x4)), y3)
  602. return QtCore.QPoint(x, y)
  603. def intersectingEdges(self, point1, point2, points):
  604. """Find intersecting edges.
  605. For each edge formed by `points', yield the intersection
  606. with the line segment `(x1,y1) - (x2,y2)`, if it exists.
  607. Also return the distance of `(x2,y2)' to the middle of the
  608. edge along with its index, so that the one closest can be chosen.
  609. """
  610. (x1, y1) = point1
  611. (x2, y2) = point2
  612. for i in range(4):
  613. x3, y3 = points[i]
  614. x4, y4 = points[(i + 1) % 4]
  615. denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
  616. nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)
  617. nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
  618. if denom == 0:
  619. # This covers two cases:
  620. # nua == nub == 0: Coincident
  621. # otherwise: Parallel
  622. continue
  623. ua, ub = nua / denom, nub / denom
  624. if 0 <= ua <= 1 and 0 <= ub <= 1:
  625. x = x1 + ua * (x2 - x1)
  626. y = y1 + ua * (y2 - y1)
  627. m = QtCore.QPoint((x3 + x4) / 2, (y3 + y4) / 2)
  628. d = labelme.utils.distance(m - QtCore.QPoint(x2, y2))
  629. yield d, i, (x, y)
  630. # These two, along with a call to adjustSize are required for the
  631. # scroll area.
  632. def sizeHint(self):
  633. return self.minimumSizeHint()
  634. def minimumSizeHint(self):
  635. if self.pixmap:
  636. return self.scale * self.pixmap.size()
  637. return super(Canvas, self).minimumSizeHint()
  638. def wheelEvent(self, ev):
  639. if QT5:
  640. mods = ev.modifiers()
  641. delta = ev.angleDelta()
  642. if QtCore.Qt.ControlModifier == int(mods):
  643. # with Ctrl/Command key
  644. # zoom
  645. self.zoomRequest.emit(delta.y(), ev.pos())
  646. else:
  647. # scroll
  648. self.scrollRequest.emit(delta.x(), QtCore.Qt.Horizontal)
  649. self.scrollRequest.emit(delta.y(), QtCore.Qt.Vertical)
  650. else:
  651. if ev.orientation() == QtCore.Qt.Vertical:
  652. mods = ev.modifiers()
  653. if QtCore.Qt.ControlModifier == int(mods):
  654. # with Ctrl/Command key
  655. self.zoomRequest.emit(ev.delta(), ev.pos())
  656. else:
  657. self.scrollRequest.emit(
  658. ev.delta(),
  659. QtCore.Qt.Horizontal
  660. if (QtCore.Qt.ShiftModifier == int(mods))
  661. else QtCore.Qt.Vertical,
  662. )
  663. else:
  664. self.scrollRequest.emit(ev.delta(), QtCore.Qt.Horizontal)
  665. ev.accept()
  666. def keyPressEvent(self, ev):
  667. modifiers = ev.modifiers()
  668. key = ev.key()
  669. if key == QtCore.Qt.Key_Escape and self.current:
  670. self.current = None
  671. self.drawingPolygon.emit(False)
  672. self.update()
  673. elif key == QtCore.Qt.Key_Return and self.canCloseShape():
  674. self.finalise()
  675. elif modifiers == QtCore.Qt.AltModifier:
  676. self.snapping = False
  677. def keyReleaseEvent(self, ev):
  678. modifiers = ev.modifiers()
  679. if int(modifiers) == 0:
  680. self.snapping = True
  681. def setLastLabel(self, text, flags):
  682. assert text
  683. self.shapes[-1].label = text
  684. self.shapes[-1].flags = flags
  685. self.shapesBackups.pop()
  686. self.storeShapes()
  687. return self.shapes[-1]
  688. def undoLastLine(self):
  689. assert self.shapes
  690. self.current = self.shapes.pop()
  691. self.current.setOpen()
  692. if self.createMode in ["polygon", "linestrip"]:
  693. self.line.points = [self.current[-1], self.current[0]]
  694. elif self.createMode in ["rectangle", "line", "circle"]:
  695. self.current.points = self.current.points[0:1]
  696. elif self.createMode == "point":
  697. self.current = None
  698. self.drawingPolygon.emit(True)
  699. def undoLastPoint(self):
  700. if not self.current or self.current.isClosed():
  701. return
  702. self.current.popPoint()
  703. if len(self.current) > 0:
  704. self.line[0] = self.current[-1]
  705. else:
  706. self.current = None
  707. self.drawingPolygon.emit(False)
  708. self.update()
  709. def loadPixmap(self, pixmap, clear_shapes=True):
  710. self.pixmap = pixmap
  711. if clear_shapes:
  712. self.shapes = []
  713. self.update()
  714. def loadShapes(self, shapes, replace=True):
  715. if replace:
  716. self.shapes = list(shapes)
  717. else:
  718. self.shapes.extend(shapes)
  719. self.storeShapes()
  720. self.current = None
  721. self.hShape = None
  722. self.hVertex = None
  723. self.hEdge = None
  724. self.update()
  725. def setShapeVisible(self, shape, value):
  726. self.visible[shape] = value
  727. self.update()
  728. def overrideCursor(self, cursor):
  729. self.restoreCursor()
  730. self._cursor = cursor
  731. QtWidgets.QApplication.setOverrideCursor(cursor)
  732. def restoreCursor(self):
  733. QtWidgets.QApplication.restoreOverrideCursor()
  734. def resetState(self):
  735. self.restoreCursor()
  736. self.pixmap = None
  737. self.shapesBackups = []
  738. self.update()