123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- from qtpy import QtCore
- from qtpy import QtGui
- from qtpy import QtWidgets
- from labelme import QT5
- from labelme.shape import Shape
- import labelme.utils
- CURSOR_DEFAULT = QtCore.Qt.ArrowCursor
- CURSOR_POINT = QtCore.Qt.PointingHandCursor
- CURSOR_DRAW = QtCore.Qt.CrossCursor
- CURSOR_MOVE = QtCore.Qt.ClosedHandCursor
- CURSOR_GRAB = QtCore.Qt.OpenHandCursor
- class Canvas(QtWidgets.QWidget):
- zoomRequest = QtCore.Signal(int, QtCore.QPoint)
- scrollRequest = QtCore.Signal(int, int)
- newShape = QtCore.Signal()
- selectionChanged = QtCore.Signal(bool)
- shapeMoved = QtCore.Signal()
- drawingPolygon = QtCore.Signal(bool)
- edgeSelected = QtCore.Signal(bool)
- CREATE, EDIT = 0, 1
-
- _createMode = 'polygon'
- _fill_drawing = False
- def __init__(self, *args, **kwargs):
- self.epsilon = kwargs.pop('epsilon', 10.0)
- super(Canvas, self).__init__(*args, **kwargs)
-
- self.mode = self.EDIT
- self.shapes = []
- self.shapesBackups = []
- self.current = None
- self.selectedShape = None
- self.selectedShapeCopy = None
- self.lineColor = QtGui.QColor(0, 0, 255)
-
-
-
-
-
- self.line = Shape(line_color=self.lineColor)
- self.prevPoint = QtCore.QPoint()
- self.prevMovePoint = QtCore.QPoint()
- self.offsets = QtCore.QPoint(), QtCore.QPoint()
- self.scale = 1.0
- self.pixmap = QtGui.QPixmap()
- self.visible = {}
- self._hideBackround = False
- self.hideBackround = False
- self.hShape = None
- self.hVertex = None
- self.hEdge = None
- self.movingShape = False
- self._painter = QtGui.QPainter()
- self._cursor = CURSOR_DEFAULT
-
- self.menus = (QtWidgets.QMenu(), QtWidgets.QMenu())
-
- self.setMouseTracking(True)
- self.setFocusPolicy(QtCore.Qt.WheelFocus)
- def fillDrawing(self):
- return self._fill_drawing
- def setFillDrawing(self, value):
- self._fill_drawing = value
- @property
- def createMode(self):
- return self._createMode
- @createMode.setter
- def createMode(self, value):
- if value not in ['polygon', 'rectangle', 'circle',
- 'line', 'point', 'linestrip']:
- raise ValueError('Unsupported createMode: %s' % value)
- self._createMode = value
- def storeShapes(self):
- shapesBackup = []
- for shape in self.shapes:
- shapesBackup.append(shape.copy())
- if len(self.shapesBackups) >= 10:
- self.shapesBackups = self.shapesBackups[-9:]
- self.shapesBackups.append(shapesBackup)
- @property
- def isShapeRestorable(self):
- if len(self.shapesBackups) < 2:
- return False
- return True
- def restoreShape(self):
- if not self.isShapeRestorable:
- return
- self.shapesBackups.pop()
- shapesBackup = self.shapesBackups.pop()
- self.shapes = shapesBackup
- self.storeShapes()
- self.repaint()
- def enterEvent(self, ev):
- self.overrideCursor(self._cursor)
- def leaveEvent(self, ev):
- self.restoreCursor()
- def focusOutEvent(self, ev):
- self.restoreCursor()
- def isVisible(self, shape):
- return self.visible.get(shape, True)
- def drawing(self):
- return self.mode == self.CREATE
- def editing(self):
- return self.mode == self.EDIT
- def setEditing(self, value=True):
- self.mode = self.EDIT if value else self.CREATE
- if not value:
- self.unHighlight()
- self.deSelectShape()
- def unHighlight(self):
- if self.hShape:
- self.hShape.highlightClear()
- self.hVertex = self.hShape = None
- def selectedVertex(self):
- return self.hVertex is not None
- def mouseMoveEvent(self, ev):
- """Update line with last point and current coordinates."""
- try:
- if QT5:
- pos = self.transformPos(ev.localPos())
- else:
- pos = self.transformPos(ev.posF())
- except AttributeError:
- return
- self.prevMovePoint = pos
- self.restoreCursor()
-
- if self.drawing():
- self.line.shape_type = self.createMode
- self.overrideCursor(CURSOR_DRAW)
- if not self.current:
- return
- color = self.lineColor
- if self.outOfPixmap(pos):
-
-
- pos = self.intersectionPoint(self.current[-1], pos)
- elif len(self.current) > 1 and self.createMode == 'polygon' and\
- self.closeEnough(pos, self.current[0]):
-
-
- pos = self.current[0]
- color = self.current.line_color
- self.overrideCursor(CURSOR_POINT)
- self.current.highlightVertex(0, Shape.NEAR_VERTEX)
- if self.createMode in ['polygon', 'linestrip']:
- self.line[0] = self.current[-1]
- self.line[1] = pos
- elif self.createMode == 'rectangle':
- self.line.points = [self.current[0], pos]
- self.line.close()
- elif self.createMode == 'circle':
- self.line.points = [self.current[0], pos]
- self.line.shape_type = "circle"
- elif self.createMode == 'line':
- self.line.points = [self.current[0], pos]
- self.line.close()
- elif self.createMode == 'point':
- self.line.points = [self.current[0]]
- self.line.close()
- self.line.line_color = color
- self.repaint()
- self.current.highlightClear()
- return
-
- if QtCore.Qt.RightButton & ev.buttons():
- if self.selectedShapeCopy and self.prevPoint:
- self.overrideCursor(CURSOR_MOVE)
- self.boundedMoveShape(self.selectedShapeCopy, pos)
- self.repaint()
- elif self.selectedShape:
- self.selectedShapeCopy = self.selectedShape.copy()
- self.repaint()
- return
-
- self.movingShape = False
- if QtCore.Qt.LeftButton & ev.buttons():
- if self.selectedVertex():
- self.boundedMoveVertex(pos)
- self.repaint()
- self.movingShape = True
- elif self.selectedShape and self.prevPoint:
- self.overrideCursor(CURSOR_MOVE)
- self.boundedMoveShape(self.selectedShape, pos)
- self.repaint()
- self.movingShape = True
- return
-
-
-
-
- self.setToolTip("Image")
- for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
-
-
- index = shape.nearestVertex(pos, self.epsilon / self.scale)
- index_edge = shape.nearestEdge(pos, self.epsilon / self.scale)
- if index is not None:
- if self.selectedVertex():
- self.hShape.highlightClear()
- self.hVertex = index
- self.hShape = shape
- self.hEdge = index_edge
- shape.highlightVertex(index, shape.MOVE_VERTEX)
- self.overrideCursor(CURSOR_POINT)
- self.setToolTip("Click & drag to move point")
- self.setStatusTip(self.toolTip())
- self.update()
- break
- elif shape.containsPoint(pos):
- if self.selectedVertex():
- self.hShape.highlightClear()
- self.hVertex = None
- self.hShape = shape
- self.hEdge = index_edge
- self.setToolTip(
- "Click & drag to move shape '%s'" % shape.label)
- self.setStatusTip(self.toolTip())
- self.overrideCursor(CURSOR_GRAB)
- self.update()
- break
- else:
- if self.hShape:
- self.hShape.highlightClear()
- self.update()
- self.hVertex, self.hShape, self.hEdge = None, None, None
- self.edgeSelected.emit(self.hEdge is not None)
- def addPointToEdge(self):
- if (self.hShape is None and
- self.hEdge is None and
- self.prevMovePoint is None):
- return
- shape = self.hShape
- index = self.hEdge
- point = self.prevMovePoint
- shape.insertPoint(index, point)
- shape.highlightVertex(index, shape.MOVE_VERTEX)
- self.hShape = shape
- self.hVertex = index
- self.hEdge = None
- def mousePressEvent(self, ev):
- if QT5:
- pos = self.transformPos(ev.localPos())
- else:
- pos = self.transformPos(ev.posF())
- if ev.button() == QtCore.Qt.LeftButton:
- if self.drawing():
- if self.current:
-
- if self.createMode == 'polygon':
- self.current.addPoint(self.line[1])
- self.line[0] = self.current[-1]
- if self.current.isClosed():
- self.finalise()
- elif self.createMode in ['rectangle', 'circle', 'line']:
- assert len(self.current.points) == 1
- self.current.points = self.line.points
- self.finalise()
- elif self.createMode == 'linestrip':
- self.current.addPoint(self.line[1])
- self.line[0] = self.current[-1]
- if int(ev.modifiers()) == QtCore.Qt.ControlModifier:
- self.finalise()
- elif not self.outOfPixmap(pos):
-
- self.current = Shape(shape_type=self.createMode)
- self.current.addPoint(pos)
- if self.createMode == 'point':
- self.finalise()
- else:
- if self.createMode == 'circle':
- self.current.shape_type = 'circle'
- self.line.points = [pos, pos]
- self.setHiding()
- self.drawingPolygon.emit(True)
- self.update()
- else:
- self.selectShapePoint(pos)
- self.prevPoint = pos
- self.repaint()
- elif ev.button() == QtCore.Qt.RightButton and self.editing():
- self.selectShapePoint(pos)
- self.prevPoint = pos
- self.repaint()
- def mouseReleaseEvent(self, ev):
- if ev.button() == QtCore.Qt.RightButton:
- menu = self.menus[bool(self.selectedShapeCopy)]
- self.restoreCursor()
- if not menu.exec_(self.mapToGlobal(ev.pos()))\
- and self.selectedShapeCopy:
-
- self.selectedShapeCopy = None
- self.repaint()
- elif ev.button() == QtCore.Qt.LeftButton and self.selectedShape:
- self.overrideCursor(CURSOR_GRAB)
- if self.movingShape:
- self.storeShapes()
- self.shapeMoved.emit()
- def endMove(self, copy=False):
- assert self.selectedShape and self.selectedShapeCopy
- shape = self.selectedShapeCopy
-
-
- if copy:
- self.shapes.append(shape)
- self.selectedShape.selected = False
- self.selectedShape = shape
- self.repaint()
- else:
- shape.label = self.selectedShape.label
- self.deleteSelected()
- self.shapes.append(shape)
- self.storeShapes()
- self.selectedShapeCopy = None
- def hideBackroundShapes(self, value):
- self.hideBackround = value
- if self.selectedShape:
-
-
- self.setHiding(True)
- self.repaint()
- def setHiding(self, enable=True):
- self._hideBackround = self.hideBackround if enable else False
- def canCloseShape(self):
- return self.drawing() and self.current and len(self.current) > 2
- def mouseDoubleClickEvent(self, ev):
-
-
- if self.canCloseShape() and len(self.current) > 3:
- self.current.popPoint()
- self.finalise()
- def selectShape(self, shape):
- self.deSelectShape()
- shape.selected = True
- self.selectedShape = shape
- self.setHiding()
- self.selectionChanged.emit(True)
- self.update()
- def selectShapePoint(self, point):
- """Select the first shape created which contains this point."""
- self.deSelectShape()
- if self.selectedVertex():
- index, shape = self.hVertex, self.hShape
- shape.highlightVertex(index, shape.MOVE_VERTEX)
- return
- for shape in reversed(self.shapes):
- if self.isVisible(shape) and shape.containsPoint(point):
- shape.selected = True
- self.selectedShape = shape
- self.calculateOffsets(shape, point)
- self.setHiding()
- self.selectionChanged.emit(True)
- return
- def calculateOffsets(self, shape, point):
- rect = shape.boundingRect()
- x1 = rect.x() - point.x()
- y1 = rect.y() - point.y()
- x2 = (rect.x() + rect.width() - 1) - point.x()
- y2 = (rect.y() + rect.height() - 1) - point.y()
- self.offsets = QtCore.QPoint(x1, y1), QtCore.QPoint(x2, y2)
- def boundedMoveVertex(self, pos):
- index, shape = self.hVertex, self.hShape
- point = shape[index]
- if self.outOfPixmap(pos):
- pos = self.intersectionPoint(point, pos)
- shape.moveVertexBy(index, pos - point)
- def boundedMoveShape(self, shape, pos):
- if self.outOfPixmap(pos):
- return False
- o1 = pos + self.offsets[0]
- if self.outOfPixmap(o1):
- pos -= QtCore.QPoint(min(0, o1.x()), min(0, o1.y()))
- o2 = pos + self.offsets[1]
- if self.outOfPixmap(o2):
- pos += QtCore.QPoint(min(0, self.pixmap.width() - o2.x()),
- min(0, self.pixmap.height() - o2.y()))
-
-
-
-
-
- dp = pos - self.prevPoint
- if dp:
- shape.moveBy(dp)
- self.prevPoint = pos
- return True
- return False
- def deSelectShape(self):
- if self.selectedShape:
- self.selectedShape.selected = False
- self.selectedShape = None
- self.setHiding(False)
- self.selectionChanged.emit(False)
- self.update()
- def deleteSelected(self):
- if self.selectedShape:
- shape = self.selectedShape
- self.shapes.remove(self.selectedShape)
- self.storeShapes()
- self.selectedShape = None
- self.update()
- return shape
- def copySelectedShape(self):
- if self.selectedShape:
- shape = self.selectedShape.copy()
- self.deSelectShape()
- self.shapes.append(shape)
- self.storeShapes()
- shape.selected = True
- self.selectedShape = shape
- self.boundedShiftShape(shape)
- return shape
- def boundedShiftShape(self, shape):
-
-
- point = shape[0]
- offset = QtCore.QPoint(2.0, 2.0)
- self.calculateOffsets(shape, point)
- self.prevPoint = point
- if not self.boundedMoveShape(shape, point - offset):
- self.boundedMoveShape(shape, point + offset)
- def paintEvent(self, event):
- if not self.pixmap:
- return super(Canvas, self).paintEvent(event)
- p = self._painter
- p.begin(self)
- p.setRenderHint(QtGui.QPainter.Antialiasing)
- p.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
- p.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
- p.scale(self.scale, self.scale)
- p.translate(self.offsetToCenter())
- p.drawPixmap(0, 0, self.pixmap)
- Shape.scale = self.scale
- for shape in self.shapes:
- if (shape.selected or not self._hideBackround) and \
- self.isVisible(shape):
- shape.fill = shape.selected or shape == self.hShape
- shape.paint(p)
- if self.current:
- self.current.paint(p)
- self.line.paint(p)
- if self.selectedShapeCopy:
- self.selectedShapeCopy.paint(p)
- if (self.fillDrawing() and self.createMode == 'polygon' and
- self.current is not None and len(self.current.points) >= 2):
- drawing_shape = self.current.copy()
- drawing_shape.addPoint(self.line[1])
- drawing_shape.fill = True
- drawing_shape.fill_color.setAlpha(64)
- drawing_shape.paint(p)
- p.end()
- def transformPos(self, point):
- """Convert from widget-logical coordinates to painter-logical ones."""
- return point / self.scale - self.offsetToCenter()
- def offsetToCenter(self):
- s = self.scale
- area = super(Canvas, self).size()
- w, h = self.pixmap.width() * s, self.pixmap.height() * s
- aw, ah = area.width(), area.height()
- x = (aw - w) / (2 * s) if aw > w else 0
- y = (ah - h) / (2 * s) if ah > h else 0
- return QtCore.QPoint(x, y)
- def outOfPixmap(self, p):
- w, h = self.pixmap.width(), self.pixmap.height()
- return not (0 <= p.x() < w and 0 <= p.y() < h)
- def finalise(self):
- assert self.current
- self.current.close()
- self.shapes.append(self.current)
- self.storeShapes()
- self.current = None
- self.setHiding(False)
- self.newShape.emit()
- self.update()
- def closeEnough(self, p1, p2):
-
-
-
-
- return labelme.utils.distance(p1 - p2) < (self.epsilon / self.scale)
- def intersectionPoint(self, p1, p2):
-
-
-
- size = self.pixmap.size()
- points = [(0, 0),
- (size.width() - 1, 0),
- (size.width() - 1, size.height() - 1),
- (0, size.height() - 1)]
- x1, y1 = p1.x(), p1.y()
- x2, y2 = p2.x(), p2.y()
- d, i, (x, y) = min(self.intersectingEdges((x1, y1), (x2, y2), points))
- x3, y3 = points[i]
- x4, y4 = points[(i + 1) % 4]
- if (x, y) == (x1, y1):
-
- if x3 == x4:
- return QtCore.QPoint(x3, min(max(0, y2), max(y3, y4)))
- else:
- return QtCore.QPoint(min(max(0, x2), max(x3, x4)), y3)
- return QtCore.QPoint(x, y)
- def intersectingEdges(self, point1, point2, points):
- """Find intersecting edges.
- For each edge formed by `points', yield the intersection
- with the line segment `(x1,y1) - (x2,y2)`, if it exists.
- Also return the distance of `(x2,y2)' to the middle of the
- edge along with its index, so that the one closest can be chosen.
- """
- (x1, y1) = point1
- (x2, y2) = point2
- for i in range(4):
- x3, y3 = points[i]
- x4, y4 = points[(i + 1) % 4]
- denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
- nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)
- nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
- if denom == 0:
-
-
-
- continue
- ua, ub = nua / denom, nub / denom
- if 0 <= ua <= 1 and 0 <= ub <= 1:
- x = x1 + ua * (x2 - x1)
- y = y1 + ua * (y2 - y1)
- m = QtCore.QPoint((x3 + x4) / 2, (y3 + y4) / 2)
- d = labelme.utils.distance(m - QtCore.QPoint(x2, y2))
- yield d, i, (x, y)
-
-
- def sizeHint(self):
- return self.minimumSizeHint()
- def minimumSizeHint(self):
- if self.pixmap:
- return self.scale * self.pixmap.size()
- return super(Canvas, self).minimumSizeHint()
- def wheelEvent(self, ev):
- if QT5:
- mods = ev.modifiers()
- delta = ev.angleDelta()
- if QtCore.Qt.ControlModifier == int(mods):
-
-
- self.zoomRequest.emit(delta.y(), ev.pos())
- else:
-
- self.scrollRequest.emit(delta.x(), QtCore.Qt.Horizontal)
- self.scrollRequest.emit(delta.y(), QtCore.Qt.Vertical)
- else:
- if ev.orientation() == QtCore.Qt.Vertical:
- mods = ev.modifiers()
- if QtCore.Qt.ControlModifier == int(mods):
-
- self.zoomRequest.emit(ev.delta(), ev.pos())
- else:
- self.scrollRequest.emit(
- ev.delta(),
- QtCore.Qt.Horizontal
- if (QtCore.Qt.ShiftModifier == int(mods))
- else QtCore.Qt.Vertical)
- else:
- self.scrollRequest.emit(ev.delta(), QtCore.Qt.Horizontal)
- ev.accept()
- def keyPressEvent(self, ev):
- key = ev.key()
- if key == QtCore.Qt.Key_Escape and self.current:
- self.current = None
- self.drawingPolygon.emit(False)
- self.update()
- elif key == QtCore.Qt.Key_Return and self.canCloseShape():
- self.finalise()
- def setLastLabel(self, text, flags):
- assert text
- self.shapes[-1].label = text
- self.shapes[-1].flags = flags
- self.shapesBackups.pop()
- self.storeShapes()
- return self.shapes[-1]
- def undoLastLine(self):
- assert self.shapes
- self.current = self.shapes.pop()
- self.current.setOpen()
- if self.createMode in ['polygon', 'linestrip']:
- self.line.points = [self.current[-1], self.current[0]]
- elif self.createMode in ['rectangle', 'line', 'circle']:
- self.current.points = self.current.points[0:1]
- elif self.createMode == 'point':
- self.current = None
- self.drawingPolygon.emit(True)
- def undoLastPoint(self):
- if not self.current or self.current.isClosed():
- return
- self.current.popPoint()
- if len(self.current) > 0:
- self.line[0] = self.current[-1]
- else:
- self.current = None
- self.drawingPolygon.emit(False)
- self.repaint()
- def loadPixmap(self, pixmap):
- self.pixmap = pixmap
- self.shapes = []
- self.repaint()
- def loadShapes(self, shapes, replace=True):
- if replace:
- self.shapes = list(shapes)
- else:
- self.shapes.extend(shapes)
- self.storeShapes()
- self.current = None
- self.repaint()
- def setShapeVisible(self, shape, value):
- self.visible[shape] = value
- self.repaint()
- def overrideCursor(self, cursor):
- self.restoreCursor()
- self._cursor = cursor
- QtWidgets.QApplication.setOverrideCursor(cursor)
- def restoreCursor(self):
- QtWidgets.QApplication.restoreOverrideCursor()
- def resetState(self):
- self.restoreCursor()
- self.pixmap = None
- self.shapesBackups = []
- self.update()
|