From 5d6557f9947386da17a22bc527e78b65275eeea2 Mon Sep 17 00:00:00 2001 From: chenghsiung Date: Sat, 2 Jan 2021 03:54:27 +0800 Subject: [PATCH] Adaptively adjust label size (#678) * Adjust label font size based on image size * Adjust the upper boundary of the painted label * Set font size based on both image width & height --- labelImg.py | 1 + libs/canvas.py | 2 ++ libs/shape.py | 9 +++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/labelImg.py b/labelImg.py index 70c4cb7d..0742422e 100755 --- a/labelImg.py +++ b/labelImg.py @@ -1141,6 +1141,7 @@ class MainWindow(QMainWindow, WindowMixin): def paintCanvas(self): assert not self.image.isNull(), "cannot paint null image" self.canvas.scale = 0.01 * self.zoomWidget.value() + self.canvas.labelFontSize = int(0.02 * max(self.image.width(), self.image.height())) self.canvas.adjustSize() self.canvas.update() diff --git a/libs/canvas.py b/libs/canvas.py index 4b106f56..23c5b823 100644 --- a/libs/canvas.py +++ b/libs/canvas.py @@ -47,6 +47,7 @@ class Canvas(QWidget): self.prevPoint = QPointF() self.offsets = QPointF(), QPointF() self.scale = 1.0 + self.labelFontSize = 8 self.pixmap = QPixmap() self.visible = {} self._hideBackround = False @@ -478,6 +479,7 @@ class Canvas(QWidget): p.drawPixmap(0, 0, self.pixmap) Shape.scale = self.scale + Shape.labelFontSize = self.labelFontSize for shape in self.shapes: if (shape.selected or not self._hideBackround) and self.isVisible(shape): shape.fill = shape.selected or shape == self.hShape diff --git a/libs/shape.py b/libs/shape.py index c72a68f7..466e046d 100644 --- a/libs/shape.py +++ b/libs/shape.py @@ -18,7 +18,6 @@ DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255) DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155) DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255) DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0) -MIN_Y_LABEL = 10 class Shape(object): @@ -37,6 +36,7 @@ class Shape(object): point_type = P_ROUND point_size = 8 scale = 1.0 + labelFontSize = 8 def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False): self.label = label @@ -115,18 +115,19 @@ class Shape(object): if self.paintLabel: min_x = sys.maxsize min_y = sys.maxsize + min_y_label = int(1.25 * self.labelFontSize) for point in self.points: min_x = min(min_x, point.x()) min_y = min(min_y, point.y()) if min_x != sys.maxsize and min_y != sys.maxsize: font = QFont() - font.setPointSize(8) + font.setPointSize(self.labelFontSize) font.setBold(True) painter.setFont(font) if(self.label == None): self.label = "" - if(min_y < MIN_Y_LABEL): - min_y += MIN_Y_LABEL + if(min_y < min_y_label): + min_y += min_y_label painter.drawText(min_x, min_y, self.label) if self.fill: