Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 5d6557f

Browse files
authored
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
1 parent 0a581ef commit 5d6557f

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

labelImg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ def resizeEvent(self, event):
11411141
def paintCanvas(self):
11421142
assert not self.image.isNull(), "cannot paint null image"
11431143
self.canvas.scale = 0.01 * self.zoomWidget.value()
1144+
self.canvas.labelFontSize = int(0.02 * max(self.image.width(), self.image.height()))
11441145
self.canvas.adjustSize()
11451146
self.canvas.update()
11461147

libs/canvas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(self, *args, **kwargs):
4747
self.prevPoint = QPointF()
4848
self.offsets = QPointF(), QPointF()
4949
self.scale = 1.0
50+
self.labelFontSize = 8
5051
self.pixmap = QPixmap()
5152
self.visible = {}
5253
self._hideBackround = False
@@ -478,6 +479,7 @@ def paintEvent(self, event):
478479

479480
p.drawPixmap(0, 0, self.pixmap)
480481
Shape.scale = self.scale
482+
Shape.labelFontSize = self.labelFontSize
481483
for shape in self.shapes:
482484
if (shape.selected or not self._hideBackround) and self.isVisible(shape):
483485
shape.fill = shape.selected or shape == self.hShape

libs/shape.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
1919
DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
2020
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
21-
MIN_Y_LABEL = 10
2221

2322

2423
class Shape(object):
@@ -37,6 +36,7 @@ class Shape(object):
3736
point_type = P_ROUND
3837
point_size = 8
3938
scale = 1.0
39+
labelFontSize = 8
4040

4141
def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False):
4242
self.label = label
@@ -115,18 +115,19 @@ def paint(self, painter):
115115
if self.paintLabel:
116116
min_x = sys.maxsize
117117
min_y = sys.maxsize
118+
min_y_label = int(1.25 * self.labelFontSize)
118119
for point in self.points:
119120
min_x = min(min_x, point.x())
120121
min_y = min(min_y, point.y())
121122
if min_x != sys.maxsize and min_y != sys.maxsize:
122123
font = QFont()
123-
font.setPointSize(8)
124+
font.setPointSize(self.labelFontSize)
124125
font.setBold(True)
125126
painter.setFont(font)
126127
if(self.label == None):
127128
self.label = ""
128-
if(min_y < MIN_Y_LABEL):
129-
min_y += MIN_Y_LABEL
129+
if(min_y < min_y_label):
130+
min_y += min_y_label
130131
painter.drawText(min_x, min_y, self.label)
131132

132133
if self.fill:

0 commit comments

Comments
 (0)