From e51bfce9a72d0a7eb9e7265a3d4be60f74bf04d7 Mon Sep 17 00:00:00 2001 From: Hakan Karaoguz Date: Tue, 16 Jul 2019 13:52:57 +0200 Subject: [PATCH] Added combobox to the main view for displaying unique labels individually --- combobox.py | 25 +++++++++++++++++++++++++ labelImg.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 combobox.py diff --git a/combobox.py b/combobox.py new file mode 100644 index 00000000..bb6f3214 --- /dev/null +++ b/combobox.py @@ -0,0 +1,25 @@ +import sys +from PyQt5.QtCore import * +from PyQt5.QtGui import * +from PyQt5.QtWidgets import * + + +class ComboBox(QWidget): + def __init__(self, parent=None, items=[]): + super(ComboBox, self).__init__(parent) + + layout = QHBoxLayout() + self.cb = QComboBox() + self.items = items + self.cb.addItems(self.items) + + self.cb.currentIndexChanged.connect(parent.comboSelectionChanged) + + layout.addWidget(self.cb) + self.setLayout(layout) + + def update_items(self, items): + self.items = items + + self.cb.clear() + self.cb.addItems(self.items) \ No newline at end of file diff --git a/labelImg.py b/labelImg.py index 23a6cd1d..5a95a6cd 100755 --- a/labelImg.py +++ b/labelImg.py @@ -26,6 +26,7 @@ except ImportError: from PyQt4.QtGui import * from PyQt4.QtCore import * +from combobox import ComboBox from libs.resources import * from libs.constants import * from libs.utils import * @@ -136,6 +137,10 @@ class MainWindow(QMainWindow, WindowMixin): listLayout.addWidget(self.diffcButton) listLayout.addWidget(useDefaultLabelContainer) + # Create and add combobox for showing unique labels in group + self.comboBox = ComboBox(self) + listLayout.addWidget(self.comboBox) + # Create and add a widget for showing current label items self.labelList = QListWidget() labelListContainer = QWidget() @@ -147,6 +152,8 @@ class MainWindow(QMainWindow, WindowMixin): self.labelList.itemChanged.connect(self.labelItemChanged) listLayout.addWidget(self.labelList) + + self.dock = QDockWidget(getStr('boxLabelText'), self) self.dock.setObjectName(getStr('labels')) self.dock.setWidget(labelListContainer) @@ -573,6 +580,7 @@ class MainWindow(QMainWindow, WindowMixin): self.labelFile = None self.canvas.resetState() self.labelCoordinates.clear() + self.comboBox.cb.clear() def currentItem(self): items = self.labelList.selectedItems() @@ -670,6 +678,7 @@ class MainWindow(QMainWindow, WindowMixin): item.setText(text) item.setBackground(generateColorByText(text)) self.setDirty() + self.updateComboBox() # Tzutalin 20160906 : Add file list and dock to move faster def fileitemDoubleClicked(self, item=None): @@ -733,6 +742,7 @@ class MainWindow(QMainWindow, WindowMixin): self.labelList.addItem(item) for action in self.actions.onShapesPresent: action.setEnabled(True) + self.updateComboBox() def remLabel(self, shape): if shape is None: @@ -742,6 +752,7 @@ class MainWindow(QMainWindow, WindowMixin): self.labelList.takeItem(self.labelList.row(item)) del self.shapesToItems[shape] del self.itemsToShapes[item] + self.updateComboBox() def loadLabels(self, shapes): s = [] @@ -770,9 +781,20 @@ class MainWindow(QMainWindow, WindowMixin): shape.fill_color = generateColorByText(label) self.addLabel(shape) - + self.updateComboBox() self.canvas.loadShapes(s) + def updateComboBox(self): + # Get the unique labels and add them to the Combobox. + itemsTextList = [str(self.labelList.item(i).text()) for i in range(self.labelList.count())] + + uniqueTextList = list(set(itemsTextList)) + # Add a null row for showing all the labels + uniqueTextList.append("") + uniqueTextList.sort() + + self.comboBox.update_items(uniqueTextList) + def saveLabels(self, annotationFilePath): annotationFilePath = ustr(annotationFilePath) if self.labelFile is None: @@ -813,6 +835,16 @@ class MainWindow(QMainWindow, WindowMixin): self.addLabel(self.canvas.copySelectedShape()) # fix copy and delete self.shapeSelectionChanged(True) + + def comboSelectionChanged(self, index): + text = self.comboBox.cb.itemText(index) + for i in range(self.labelList.count()): + if text == "": + self.labelList.item(i).setCheckState(2) + elif text != self.labelList.item(i).text(): + self.labelList.item(i).setCheckState(0) + else: + self.labelList.item(i).setCheckState(2) def labelSelectionChanged(self): item = self.currentItem()