Skip to content

Commit

Permalink
Added label specific content
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Sep 13, 2021
1 parent 1a425b2 commit 2cfeb89
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
10 changes: 8 additions & 2 deletions labelme/app.py
Expand Up @@ -1034,10 +1034,11 @@ def editLabel(self, item=None):
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
Expand All @@ -1052,6 +1053,7 @@ def editLabel(self, item=None):
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:
Expand Down Expand Up @@ -1168,6 +1170,7 @@ def loadLabels(self, shapes):
points = shape["points"]
shape_type = shape["shape_type"]
flags = shape["flags"]
content = shape["content"]
group_id = shape["group_id"]
other_data = shape["other_data"]

Expand All @@ -1179,6 +1182,7 @@ def loadLabels(self, shapes):
label=label,
shape_type=shape_type,
group_id=group_id,
content=content,
)
for x, y in points:
shape.addPoint(QtCore.QPointF(x, y))
Expand Down Expand Up @@ -1215,6 +1219,7 @@ def format_shape(s):
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,
)
Expand Down Expand Up @@ -1302,7 +1307,7 @@ def newShape(self):
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)

Expand All @@ -1318,6 +1323,7 @@ def newShape(self):
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)
Expand Down
2 changes: 2 additions & 0 deletions labelme/label_file.py
Expand Up @@ -83,6 +83,7 @@ def load(self, filename):
"group_id",
"shape_type",
"flags",
"content"
]
try:
with open(filename, "r") as f:
Expand Down Expand Up @@ -124,6 +125,7 @@ def load(self, filename):
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
Expand Down
2 changes: 2 additions & 0 deletions labelme/shape.py
Expand Up @@ -51,6 +51,7 @@ def __init__(
shape_type=None,
flags=None,
group_id=None,
content=None
):
self.label = label
self.group_id = group_id
Expand All @@ -59,6 +60,7 @@ def __init__(
self.selected = False
self.shape_type = shape_type
self.flags = flags
self.content = content
self.other_data = {}

self._highlightIndex = None
Expand Down
27 changes: 23 additions & 4 deletions labelme/widgets/label_dialog.py
Expand Up @@ -42,7 +42,7 @@ def __init__(
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)
Expand Down Expand Up @@ -103,7 +103,13 @@ def __init__(
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":
Expand Down Expand Up @@ -199,8 +205,17 @@ def getGroupId(self):
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
Expand All @@ -212,6 +227,10 @@ def popUp(self, text=None, move=True, flags=None, group_id=None):
# 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:
Expand All @@ -233,6 +252,6 @@ def popUp(self, text=None, move=True, flags=None, group_id=None):
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

0 comments on commit 2cfeb89

Please sign in to comment.