Skip to content

Commit f2d02f0

Browse files
committed
Fix label positioning
1 parent 2728477 commit f2d02f0

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

bbox_visualizer/bbox_visualizer.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,52 +123,69 @@ def add_label(
123123
"""
124124

125125
(text_width, text_height), baseline = cv2.getTextSize(label, font, size, thickness)
126+
padding = 5 # Padding around text
126127

127128
if top and bbox[1] - text_height > text_height:
128-
label_bg = [
129-
bbox[0],
130-
bbox[1],
131-
bbox[0] + text_width,
132-
bbox[1] - text_height - (15 * size),
133-
]
129+
# Calculate background rectangle dimensions
130+
bg_width = text_width + 2 * padding
131+
bg_height = text_height + 2 * padding
132+
133+
# Calculate background rectangle position
134+
bg_x1 = bbox[0]
135+
bg_y1 = bbox[1] - bg_height # Removed the gap by removing (5 * size)
136+
bg_x2 = bg_x1 + bg_width
137+
bg_y2 = bg_y1 + bg_height
138+
134139
if draw_bg:
135140
cv2.rectangle(
136141
img,
137-
(label_bg[0], label_bg[1]),
138-
(label_bg[2] + 5, label_bg[3]),
142+
(bg_x1, bg_y1),
143+
(bg_x2, bg_y2),
139144
text_bg_color,
140145
-1,
141146
)
142147

148+
# Center text in background rectangle
149+
text_x = bg_x1 + (bg_width - text_width) // 2
150+
text_y = bg_y1 + (bg_height + text_height) // 2
151+
143152
cv2.putText(
144153
img,
145154
label,
146-
(bbox[0] + 5, bbox[1] - (15 * size)),
155+
(text_x, text_y),
147156
font,
148157
size,
149158
text_color,
150159
thickness,
151160
)
152161
else:
153-
label_bg = [
154-
bbox[0],
155-
bbox[1],
156-
bbox[0] + text_width,
157-
bbox[1] + text_height + (15 * size),
158-
]
159-
if draw_bg:
162+
# Calculate background rectangle dimensions
163+
bg_width = text_width + 2 * padding
164+
bg_height = text_height + 2 * padding
165+
166+
# Calculate background rectangle position
167+
bg_x1 = bbox[0]
168+
bg_y1 = bbox[1]
169+
bg_x2 = bg_x1 + bg_width
170+
bg_y2 = bg_y1 + bg_height
160171

172+
if draw_bg:
161173
cv2.rectangle(
162174
img,
163-
(label_bg[0], label_bg[1]),
164-
(label_bg[2] + 5, label_bg[3]),
175+
(bg_x1, bg_y1),
176+
(bg_x2, bg_y2),
165177
text_bg_color,
166178
-1,
167179
)
180+
181+
# Center text in background rectangle
182+
text_x = bg_x1 + (bg_width - text_width) // 2
183+
text_y = bg_y1 + (bg_height + text_height) // 2
184+
168185
cv2.putText(
169186
img,
170187
label,
171-
(bbox[0] + 5, bbox[1] + (16 * size) + (4 * thickness)),
188+
(text_x, text_y),
172189
font,
173190
size,
174191
text_color,
@@ -217,13 +234,15 @@ def add_T_label(
217234
(label_width, label_height), baseline = cv2.getTextSize(
218235
label, font, size, thickness
219236
)
237+
padding = 5 # Padding around text
238+
220239
# draw vertical line
221240
x_center = (bbox[0] + bbox[2]) // 2
222241
line_top = y_top = bbox[1] - 50
223242

224243
# draw rectangle with label
225244
y_bottom = y_top
226-
y_top = y_bottom - label_height - 5
245+
y_top = y_bottom - label_height - 2 * padding
227246

228247
if y_top < 0:
229248
logging.warning(
@@ -232,14 +251,28 @@ def add_T_label(
232251
return add_label(img, label, bbox)
233252

234253
cv2.line(img, (x_center, bbox[1]), (x_center, line_top), text_bg_color, 3)
235-
x_left = x_center - (label_width // 2) - 5
236-
x_right = x_center + (label_width // 2) + 5
254+
255+
# Calculate background rectangle dimensions
256+
bg_width = label_width + 2 * padding
257+
bg_height = label_height + 2 * padding
258+
259+
# Calculate background rectangle position
260+
bg_x1 = x_center - (bg_width // 2)
261+
bg_y1 = y_top
262+
bg_x2 = bg_x1 + bg_width
263+
bg_y2 = bg_y1 + bg_height
264+
237265
if draw_bg:
238-
cv2.rectangle(img, (x_left, y_top - 30), (x_right, y_bottom), text_bg_color, -1)
266+
cv2.rectangle(img, (bg_x1, bg_y1), (bg_x2, bg_y2), text_bg_color, -1)
267+
268+
# Center text in background rectangle
269+
text_x = bg_x1 + (bg_width - label_width) // 2
270+
text_y = bg_y1 + (bg_height + label_height) // 2
271+
239272
cv2.putText(
240273
img,
241274
label,
242-
(x_left + 5, y_bottom - (8 * size)),
275+
(text_x, text_y),
243276
font,
244277
size,
245278
text_color,

0 commit comments

Comments
 (0)