Skip to content

Commit

Permalink
Colorize shapes according to uniqLabelList
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jan 26, 2020
1 parent 3936c55 commit 9a191df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
35 changes: 21 additions & 14 deletions examples/instance_segmentation/data_annotated/2011_000025.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"imageData": null,
"version": "3.22.0",
"flags": {},
"shapes": [
{
"fill_color": null,
"line_color": null,
"label": "bus-1",
"label": "bus",
"points": [
[
260.936170212766,
Expand Down Expand Up @@ -114,12 +113,13 @@
338.936170212766,
25.563829787234056
]
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
{
"fill_color": null,
"line_color": null,
"label": "bus-2",
"label": "bus",
"points": [
[
88.93617021276599,
Expand Down Expand Up @@ -165,11 +165,12 @@
81.93617021276599,
151.56382978723406
]
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
{
"fill_color": null,
"line_color": null,
"label": "car",
"points": [
[
Expand All @@ -196,9 +197,16 @@
408.936170212766,
218.56382978723406
]
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"imagePath": "2011_000025.jpg",
"imageData": null,
"imageHeight": 375,
"imageWidth": 500,
"fillColor": [
255,
0,
Expand All @@ -210,6 +218,5 @@
255,
0,
128
],
"imagePath": "2011_000025.jpg"
]
}
28 changes: 23 additions & 5 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import webbrowser

import imgviz
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtGui
Expand Down Expand Up @@ -39,6 +40,9 @@
# - Zoom is too "steppy".


LABEL_COLORMAP = imgviz.label_colormap()


class MainWindow(QtWidgets.QMainWindow):

FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = 0, 1, 2
Expand Down Expand Up @@ -115,7 +119,6 @@ def __init__(
"Press 'Esc' to deselect."))
if self._config['labels']:
self.uniqLabelList.addItems(self._config['labels'])
self.uniqLabelList.sortItems()
self.label_dock = QtWidgets.QDockWidget(self.tr(u'Label List'), self)
self.label_dock.setObjectName(u'Label List')
self.label_dock.setWidget(self.uniqLabelList)
Expand Down Expand Up @@ -918,7 +921,6 @@ def editLabel(self, item=False):
self.setDirty()
if not self.uniqLabelList.findItems(shape.label, Qt.MatchExactly):
self.uniqLabelList.addItem(shape.label)
self.uniqLabelList.sortItems()

def fileSearchChanged(self):
self.importDirImages(
Expand Down Expand Up @@ -972,11 +974,23 @@ def addLabel(self, shape):
self.labelList.addItem(item)
if not self.uniqLabelList.findItems(shape.label, Qt.MatchExactly):
self.uniqLabelList.addItem(shape.label)
self.uniqLabelList.sortItems()
self.labelDialog.addLabelHistory(shape.label)
for action in self.actions.onShapesPresent:
action.setEnabled(True)

r, g, b = self._get_rgb_by_label(shape.label)
shape.line_color = QtGui.QColor(r, g, b)
shape.vertex_fill_color = QtGui.QColor(r, g, b)
shape.hvertex_fill_color = QtGui.QColor(255, 255, 255)
shape.fill_color = QtGui.QColor(r, g, b, 127)
shape.select_line_color = QtGui.QColor(255, 255, 255)
shape.select_fill_color = QtGui.QColor(r, g, b, 155)

def _get_rgb_by_label(self, label):
item = self.uniqLabelList.findItems(label, Qt.MatchExactly)[0]
label_id = self.uniqLabelList.indexFromItem(item).row() - 1
return LABEL_COLORMAP[label_id % len(LABEL_COLORMAP)]

def remLabels(self, shapes):
for shape in shapes:
item = self.labelList.get_item_from_shape(shape)
Expand All @@ -1000,7 +1014,9 @@ def loadLabels(self, shapes):
group_id = shape.get('group_id')

shape = Shape(
label=label, shape_type=shape_type, group_id=group_id
label=label,
shape_type=shape_type,
group_id=group_id,
)
for x, y in points:
shape.addPoint(QtCore.QPointF(x, y))
Expand Down Expand Up @@ -1113,6 +1129,7 @@ def newShape(self):
items = self.uniqLabelList.selectedItems()
text = None
flags = {}
group_id = None
if items:
text = items[0].text()
if self._config['display_label_popup'] or not text:
Expand All @@ -1127,7 +1144,7 @@ def newShape(self):
instance_text = previous_label
if instance_text != '':
text = instance_text
text, flags = self.labelDialog.popUp(text)
text, flags, group_id = self.labelDialog.popUp(text)
if text is None:
self.labelDialog.edit.setText(previous_label)

Expand All @@ -1142,6 +1159,7 @@ def newShape(self):
if text:
self.labelList.clearSelection()
shape = self.canvas.setLastLabel(text, flags)
shape.group_id = group_id
self.addLabel(shape)
self.actions.editMode.setEnabled(True)
self.actions.undoLastPoint.setEnabled(False)
Expand Down
18 changes: 9 additions & 9 deletions labelme/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
# - [opt] Store paths instead of creating new ones at each paint.


DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128)
DEFAULT_FILL_COLOR = QtGui.QColor(255, 0, 0, 128)
DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255)
DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 128, 255, 155)
DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255)
DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 0, 0)
DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128) # before hovering
DEFAULT_FILL_COLOR = QtGui.QColor(255, 0, 0, 128) # hovering
DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255) # selected
DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 128, 255, 155) # selected
DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255) # hovering
DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 0, 0, 255) # hovering


class Shape(object):
Expand Down Expand Up @@ -156,7 +156,7 @@ def paint(self, painter):

painter.drawPath(line_path)
painter.drawPath(vrtx_path)
painter.fillPath(vrtx_path, self.vertex_fill_color)
painter.fillPath(vrtx_path, self._vertex_fill_color)
if self.fill:
color = self.select_fill_color \
if self.selected else self.fill_color
Expand All @@ -170,9 +170,9 @@ def drawVertex(self, path, i):
size, shape = self._highlightSettings[self._highlightMode]
d *= size
if self._highlightIndex is not None:
self.vertex_fill_color = self.hvertex_fill_color
self._vertex_fill_color = self.hvertex_fill_color
else:
self.vertex_fill_color = Shape.vertex_fill_color
self._vertex_fill_color = self.vertex_fill_color
if shape == self.P_SQUARE:
path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
elif shape == self.P_ROUND:
Expand Down

0 comments on commit 9a191df

Please sign in to comment.