فهرست منبع

Colorize shapes according to uniqLabelList

Kentaro Wada 5 سال پیش
والد
کامیت
9a191df998
3فایلهای تغییر یافته به همراه53 افزوده شده و 28 حذف شده
  1. 21 14
      examples/instance_segmentation/data_annotated/2011_000025.json
  2. 23 5
      labelme/app.py
  3. 9 9
      labelme/shape.py

+ 21 - 14
examples/instance_segmentation/data_annotated/2011_000025.json

@@ -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,
@@ -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,
@@ -165,11 +165,12 @@
           81.93617021276599,
           151.56382978723406
         ]
-      ]
+      ],
+      "group_id": null,
+      "shape_type": "polygon",
+      "flags": {}
     },
     {
-      "fill_color": null,
-      "line_color": null,
       "label": "car",
       "points": [
         [
@@ -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,
@@ -210,6 +218,5 @@
     255,
     0,
     128
-  ],
-  "imagePath": "2011_000025.jpg"
+  ]
 }

+ 23 - 5
labelme/app.py

@@ -4,6 +4,7 @@ import os.path as osp
 import re
 import webbrowser
 
+import imgviz
 from qtpy import QtCore
 from qtpy.QtCore import Qt
 from qtpy import QtGui
@@ -39,6 +40,9 @@ from labelme.widgets import ZoomWidget
 # - Zoom is too "steppy".
 
 
+LABEL_COLORMAP = imgviz.label_colormap()
+
+
 class MainWindow(QtWidgets.QMainWindow):
 
     FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = 0, 1, 2
@@ -115,7 +119,6 @@ class MainWindow(QtWidgets.QMainWindow):
             "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)
@@ -918,7 +921,6 @@ class MainWindow(QtWidgets.QMainWindow):
         self.setDirty()
         if not self.uniqLabelList.findItems(shape.label, Qt.MatchExactly):
             self.uniqLabelList.addItem(shape.label)
-            self.uniqLabelList.sortItems()
 
     def fileSearchChanged(self):
         self.importDirImages(
@@ -972,11 +974,23 @@ class MainWindow(QtWidgets.QMainWindow):
         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)
@@ -1000,7 +1014,9 @@ class MainWindow(QtWidgets.QMainWindow):
             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))
@@ -1113,6 +1129,7 @@ class MainWindow(QtWidgets.QMainWindow):
         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:
@@ -1127,7 +1144,7 @@ class MainWindow(QtWidgets.QMainWindow):
                     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)
 
@@ -1142,6 +1159,7 @@ class MainWindow(QtWidgets.QMainWindow):
         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)

+ 9 - 9
labelme/shape.py

@@ -11,12 +11,12 @@ import labelme.utils
 # - [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):
@@ -156,7 +156,7 @@ class Shape(object):
 
             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
@@ -170,9 +170,9 @@ class Shape(object):
             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: