Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def loadLabels(self, shapes):
s = []
for label, points, line_color, fill_color, shape_type, flags in shapes:
shape = Shape(label=label, shape_type=shape_type)
for x, y in points:
shape.addPoint(QtCore.QPointF(x, y))
shape.close()
if line_color:
shape.line_color = QtGui.QColor(*line_color)
if fill_color:
shape.fill_color = QtGui.QColor(*fill_color)
default_flags = {}
if self._config['label_flags']:
for pattern, keys in self._config['label_flags'].items():
if re.match(pattern, label):
for key in keys:
default_flags[key] = False
self.settings = QtCore.QSettings('labelme', 'labelme')
# FIXME: QSettings.value can return None on PyQt4
self.recentFiles = self.settings.value('recentFiles', []) or []
size = self.settings.value('window/size', QtCore.QSize(600, 500))
position = self.settings.value('window/position', QtCore.QPoint(0, 0))
self.resize(size)
self.move(position)
# or simply:
# self.restoreGeometry(settings['window/geometry']
self.restoreState(
self.settings.value('window/state', QtCore.QByteArray()))
self.lineColor = QtGui.QColor(
self.settings.value('line/color', Shape.line_color))
self.fillColor = QtGui.QColor(
self.settings.value('fill/color', Shape.fill_color))
Shape.line_color = self.lineColor
Shape.fill_color = self.fillColor
# Populate the File menu dynamically.
self.updateFileMenu()
# Since loading the file may take some time,
# make sure it runs in the background.
if self.filename is not None:
self.queueEvent(functools.partial(self.loadFile, self.filename))
# Callbacks:
self.zoomWidget.valueChanged.connect(self.paintCanvas)
self.populateModeActions()
self.epsilon = kwargs.pop('epsilon', 10.0)
super(Canvas, self).__init__(*args, **kwargs)
# Initialise local state.
self.mode = self.EDIT
self.shapes = []
self.shapesBackups = []
self.current = None
self.selectedShapes = [] # save the selected shapes here
self.selectedShapesCopy = []
self.lineColor = QtGui.QColor(0, 0, 255)
# self.line represents:
# - createMode == 'polygon': edge from last point to current
# - createMode == 'rectangle': diagonal line of the rectangle
# - createMode == 'line': the line
# - createMode == 'point': the point
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
# Menus:
# 0: right-click without selection and dragging of shapes
# XXX: Could be completely declarative.
# Restore application settings.
self.settings = QtCore.QSettings('labelme', 'labelme')
# FIXME: QSettings.value can return None on PyQt4
self.recentFiles = self.settings.value('recentFiles', []) or []
size = self.settings.value('window/size', QtCore.QSize(600, 500))
position = self.settings.value('window/position', QtCore.QPoint(0, 0))
self.resize(size)
self.move(position)
# or simply:
# self.restoreGeometry(settings['window/geometry']
self.restoreState(
self.settings.value('window/state', QtCore.QByteArray()))
self.lineColor = QtGui.QColor(
self.settings.value('line/color', Shape.line_color))
self.fillColor = QtGui.QColor(
self.settings.value('fill/color', Shape.fill_color))
Shape.line_color = self.lineColor
Shape.fill_color = self.fillColor
# Populate the File menu dynamically.
self.updateFileMenu()
# Since loading the file may take some time,
# make sure it runs in the background.
if self.filename is not None:
self.queueEvent(functools.partial(self.loadFile, self.filename))
# Callbacks:
self.zoomWidget.valueChanged.connect(self.paintCanvas)
self.populateModeActions()
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.selectedShapesCopy:
for s in self.selectedShapesCopy:
s.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])