Skip to content

Commit

Permalink
feat: Enhance shape label rendering
Browse files Browse the repository at this point in the history
- Added  method to  class for label rendering
- Labels are positioned at the top-left corner of the shape's bounding box
- Labels use the shape's line color for text
- Auto-adjusted font size based on shape size for better readability
- Removed background box around label text for a cleaner look
  • Loading branch information
healthonrails committed Dec 19, 2024
1 parent 32ce52d commit c6e3e6a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions annolid/gui/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import cv2
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtGui
from labelme.logger import logger
import labelme.utils
Expand Down Expand Up @@ -295,6 +296,29 @@ def paint(self, painter):
painter.drawPath(negative_vrtx_path)
painter.fillPath(negative_vrtx_path, QtGui.QColor(255, 0, 0, 255))

# Draw the label near the shape
if self.label:
self._draw_label(painter)

def _draw_label(self, painter):
# Get the bounding box of the shape
bounding_rect = self.boundingRect()

# Calculate label position (top-left corner of bounding box)
label_pos = bounding_rect.topLeft()

# Set label style
shape_color = self.line_color if self.line_color else Qt.black
painter.setPen(shape_color)

# Auto-adjust font size based on shape size
font_size = int(
max(20, min(bounding_rect.width(), bounding_rect.height()) // 5))
painter.setFont(QtGui.QFont("Arial", font_size, QtGui.QFont.Bold))

# Draw label text without background box
painter.drawText(label_pos, self.label)

def drawVertex(self, path, i):
d = self.point_size / self.scale
shape = self.point_type
Expand Down

0 comments on commit c6e3e6a

Please sign in to comment.