Skip to content

Commit

Permalink
Add config option to show polygons' labels on canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkarelov committed Feb 22, 2021
1 parent 7be04b2 commit 5da5ab8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(
Shape.vertex_fill_color = QtGui.QColor(
*self._config["shape"]["vertex_fill_color"]
)
Shape.show_labels = self._config["shape"]["show_labels"]
Shape.hvertex_fill_color = QtGui.QColor(
*self._config["shape"]["hvertex_fill_color"]
)
Expand Down
1 change: 1 addition & 0 deletions labelme/config/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ shape:
line_color: [0, 255, 0, 128]
fill_color: [0, 255, 0, 0] # transparent
vertex_fill_color: [0, 255, 0, 255]
show_labels: false
# selecting / hovering
select_line_color: [255, 255, 255, 255]
select_fill_color: [0, 255, 0, 155]
Expand Down
12 changes: 12 additions & 0 deletions labelme/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 255, 0, 155) # selected
DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255) # hovering
DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 255, 255, 255) # hovering
DEFAULT_SHOW_LABELS = False


class Shape(object):
Expand All @@ -32,6 +33,7 @@ class Shape(object):
select_fill_color = DEFAULT_SELECT_FILL_COLOR
vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
show_labels = DEFAULT_SHOW_LABELS
point_type = P_ROUND
point_size = 8
scale = 1.0
Expand Down Expand Up @@ -168,6 +170,16 @@ def paint(self, painter):
if self.isClosed():
line_path.lineTo(self.points[0])

if self.show_labels:
painter.setFont(QtGui.QFont("Arial", 25))
label_x, label_y = self.points[0].x(), self.points[0].y()
for p in self.points:
if p.x() < label_x:
label_x = p.x()
label_y = p.y()
shift_y = 20 * (2 if len(self.label) > 2 else 1)
painter.drawText(label_x - 15, label_y - shift_y, self.label)

painter.drawPath(line_path)
painter.drawPath(vrtx_path)
painter.fillPath(vrtx_path, self._vertex_fill_color)
Expand Down

0 comments on commit 5da5ab8

Please sign in to comment.