Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added label specific content #917

Closed
wants to merge 13 commits into from
Closed

Added label specific content #917

wants to merge 13 commits into from

Conversation

xulihang
Copy link

I need to annotate barcodes in images. Barcodes contain text content but I didn't find a place to input this kind of value. So I added a content property to shape and a textEdit widget in the label dialog.

image

@wkentaro
Copy link
Owner

It looks like you changed too many files in this PR.
And this text field may not be necessary for all people so should be hidden by default.
Please work on these.

@xulihang
Copy link
Author

xulihang commented Oct 1, 2021

I created the pull request when I only made the first commit. Sorry that I didn't know the following commits will also be included in this pull request.

@archwolf118
Copy link

we should need label "content" info in json, for example, ocr text instence has "contents" character.
Thank you!

@wosiu
Copy link
Contributor

wosiu commented May 12, 2022

Indeed it would be super cool to have this feature. I've got the same usecase - keeping text for training OCR later in the pipeline. But I also udnerstand this PR includes non-related changes and is "no-go" with the current state.

I was trying to apply the first commit only (the patch below) from this PR to the current main branch, but I guess it's slightly drifted already :(

But I can try to continue the work on this, if it makes sense @wkentaro for you?
From what I've checked it would be very similar to the change in this PR, which is:

From 2cfeb89d1c894b837d27c54efb7bf87a89240e12 Mon Sep 17 00:00:00 2001
From: xulihang <xulihanghai@163.com>
Date: Mon, 13 Sep 2021 14:53:23 +0800
Subject: [PATCH] Added label specific content

---
 labelme/app.py                  | 10 ++++++++--
 labelme/label_file.py           |  2 ++
 labelme/shape.py                |  2 ++
 labelme/widgets/label_dialog.py | 27 +++++++++++++++++++++++----
 4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/labelme/app.py b/labelme/app.py
index 2e5ec2e..24163d0 100644
--- a/labelme/app.py
+++ b/labelme/app.py
@@ -1034,10 +1034,11 @@ class MainWindow(QtWidgets.QMainWindow):
         shape = item.shape()
         if shape is None:
             return
-        text, flags, group_id = self.labelDialog.popUp(
+        text, flags, group_id, content = self.labelDialog.popUp(
             text=shape.label,
             flags=shape.flags,
             group_id=shape.group_id,
+            content=shape.content
         )
         if text is None:
             return
@@ -1052,6 +1053,7 @@ class MainWindow(QtWidgets.QMainWindow):
         shape.label = text
         shape.flags = flags
         shape.group_id = group_id
+        shape.content = content
         if shape.group_id is None:
             item.setText(shape.label)
         else:
@@ -1168,6 +1170,7 @@ class MainWindow(QtWidgets.QMainWindow):
             points = shape["points"]
             shape_type = shape["shape_type"]
             flags = shape["flags"]
+            content = shape["content"]
             group_id = shape["group_id"]
             other_data = shape["other_data"]
 
@@ -1179,6 +1182,7 @@ class MainWindow(QtWidgets.QMainWindow):
                 label=label,
                 shape_type=shape_type,
                 group_id=group_id,
+                content=content,
             )
             for x, y in points:
                 shape.addPoint(QtCore.QPointF(x, y))
@@ -1215,6 +1219,7 @@ class MainWindow(QtWidgets.QMainWindow):
                     label=s.label.encode("utf-8") if PY2 else s.label,
                     points=[(p.x(), p.y()) for p in s.points],
                     group_id=s.group_id,
+                    content=s.content,
                     shape_type=s.shape_type,
                     flags=s.flags,
                 )
@@ -1302,7 +1307,7 @@ class MainWindow(QtWidgets.QMainWindow):
         group_id = None
         if self._config["display_label_popup"] or not text:
             previous_text = self.labelDialog.edit.text()
-            text, flags, group_id = self.labelDialog.popUp(text)
+            text, flags, group_id, content = self.labelDialog.popUp(text)
             if not text:
                 self.labelDialog.edit.setText(previous_text)
 
@@ -1318,6 +1323,7 @@ class MainWindow(QtWidgets.QMainWindow):
             self.labelList.clearSelection()
             shape = self.canvas.setLastLabel(text, flags)
             shape.group_id = group_id
+            shape.content = content
             self.addLabel(shape)
             self.actions.editMode.setEnabled(True)
             self.actions.undoLastPoint.setEnabled(False)
diff --git a/labelme/label_file.py b/labelme/label_file.py
index b493ed0..3875210 100644
--- a/labelme/label_file.py
+++ b/labelme/label_file.py
@@ -83,6 +83,7 @@ class LabelFile(object):
             "group_id",
             "shape_type",
             "flags",
+            "content"
         ]
         try:
             with open(filename, "r") as f:
@@ -124,6 +125,7 @@ class LabelFile(object):
                     points=s["points"],
                     shape_type=s.get("shape_type", "polygon"),
                     flags=s.get("flags", {}),
+                    content=s.get("content"),
                     group_id=s.get("group_id"),
                     other_data={
                         k: v for k, v in s.items() if k not in shape_keys
diff --git a/labelme/shape.py b/labelme/shape.py
index 7ee0e1b..8d66ab3 100644
--- a/labelme/shape.py
+++ b/labelme/shape.py
@@ -51,6 +51,7 @@ class Shape(object):
         shape_type=None,
         flags=None,
         group_id=None,
+        content=None
     ):
         self.label = label
         self.group_id = group_id
@@ -59,6 +60,7 @@ class Shape(object):
         self.selected = False
         self.shape_type = shape_type
         self.flags = flags
+        self.content = content
         self.other_data = {}
 
         self._highlightIndex = None
diff --git a/labelme/widgets/label_dialog.py b/labelme/widgets/label_dialog.py
index 4a0eaec..fccd439 100644
--- a/labelme/widgets/label_dialog.py
+++ b/labelme/widgets/label_dialog.py
@@ -42,7 +42,7 @@ class LabelDialog(QtWidgets.QDialog):
         if fit_to_content is None:
             fit_to_content = {"row": False, "column": True}
         self._fit_to_content = fit_to_content
-
+        
         super(LabelDialog, self).__init__(parent)
         self.edit = LabelQLineEdit()
         self.edit.setPlaceholderText(text)
@@ -103,7 +103,13 @@ class LabelDialog(QtWidgets.QDialog):
         self.resetFlags()
         layout.addItem(self.flagsLayout)
         self.edit.textChanged.connect(self.updateFlags)
+        #text edit
+        self.textEdit = QtWidgets.QTextEdit()
+        self.textEdit.setPlaceholderText('Label content')
+        layout.addWidget(self.textEdit)
+        self.resize(300,200)
         self.setLayout(layout)
+
         # completion
         completer = QtWidgets.QCompleter()
         if not QT5 and completion != "startswith":
@@ -199,8 +205,17 @@ class LabelDialog(QtWidgets.QDialog):
         if group_id:
             return int(group_id)
         return None
+        
+    def getContent(self):
+        content = self.textEdit.toPlainText()
+        if content:
+            return content
+        return None
+        
+    def setContent(self, content):
+        self.textEdit.setPlainText(content)
 
-    def popUp(self, text=None, move=True, flags=None, group_id=None):
+    def popUp(self, text=None, move=True, flags=None, group_id=None, content=None):
         if self._fit_to_content["row"]:
             self.labelList.setMinimumHeight(
                 self.labelList.sizeHintForRow(0) * self.labelList.count() + 2
@@ -212,6 +227,10 @@ class LabelDialog(QtWidgets.QDialog):
         # if text is None, the previous label in self.edit is kept
         if text is None:
             text = self.edit.text()
+        # if content is None, make the self.textEdit empty
+        if content is None:
+            content=""
+        self.setContent(content)
         if flags:
             self.setFlags(flags)
         else:
@@ -233,6 +252,6 @@ class LabelDialog(QtWidgets.QDialog):
         if move:
             self.move(QtGui.QCursor.pos())
         if self.exec_():
-            return self.edit.text(), self.getFlags(), self.getGroupId()
+            return self.edit.text(), self.getFlags(), self.getGroupId(), self.getContent()
         else:
-            return None, None, None
+            return None, None, None, None
-- 
2.33.1

@xulihang
Copy link
Author

Closed because of the new pull request: #1017

@xulihang xulihang closed this May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants