@@ -123,52 +123,69 @@ def add_label(
123
123
"""
124
124
125
125
(text_width , text_height ), baseline = cv2 .getTextSize (label , font , size , thickness )
126
+ padding = 5 # Padding around text
126
127
127
128
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
+
134
139
if draw_bg :
135
140
cv2 .rectangle (
136
141
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 ),
139
144
text_bg_color ,
140
145
- 1 ,
141
146
)
142
147
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
+
143
152
cv2 .putText (
144
153
img ,
145
154
label ,
146
- (bbox [ 0 ] + 5 , bbox [ 1 ] - ( 15 * size ) ),
155
+ (text_x , text_y ),
147
156
font ,
148
157
size ,
149
158
text_color ,
150
159
thickness ,
151
160
)
152
161
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
160
171
172
+ if draw_bg :
161
173
cv2 .rectangle (
162
174
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 ),
165
177
text_bg_color ,
166
178
- 1 ,
167
179
)
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
+
168
185
cv2 .putText (
169
186
img ,
170
187
label ,
171
- (bbox [ 0 ] + 5 , bbox [ 1 ] + ( 16 * size ) + ( 4 * thickness ) ),
188
+ (text_x , text_y ),
172
189
font ,
173
190
size ,
174
191
text_color ,
@@ -217,13 +234,15 @@ def add_T_label(
217
234
(label_width , label_height ), baseline = cv2 .getTextSize (
218
235
label , font , size , thickness
219
236
)
237
+ padding = 5 # Padding around text
238
+
220
239
# draw vertical line
221
240
x_center = (bbox [0 ] + bbox [2 ]) // 2
222
241
line_top = y_top = bbox [1 ] - 50
223
242
224
243
# draw rectangle with label
225
244
y_bottom = y_top
226
- y_top = y_bottom - label_height - 5
245
+ y_top = y_bottom - label_height - 2 * padding
227
246
228
247
if y_top < 0 :
229
248
logging .warning (
@@ -232,14 +251,28 @@ def add_T_label(
232
251
return add_label (img , label , bbox )
233
252
234
253
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
+
237
265
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
+
239
272
cv2 .putText (
240
273
img ,
241
274
label ,
242
- (x_left + 5 , y_bottom - ( 8 * size ) ),
275
+ (text_x , text_y ),
243
276
font ,
244
277
size ,
245
278
text_color ,
0 commit comments