Skip to content

Commit 663bc6b

Browse files
nefeliparshoumikchow
authored andcommitted
In the parameters I have put default values for the size (=1) and thickness (=2) of the label. If the user wants to change these values, when the function he wants is called, he will put the values he wants after the bbox, first for the size and then for the thickness.
1 parent adee8e7 commit 663bc6b

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

bbox_visualizer/bbox_visualizer.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import cv2
22

3+
font = cv2.FONT_HERSHEY_SIMPLEX
4+
35

46
def draw_rectangle(img,
57
bbox,
@@ -47,6 +49,8 @@ def draw_rectangle(img,
4749
def add_label(img,
4850
label,
4951
bbox,
52+
size=1,
53+
thickness=2,
5054
draw_bg=True,
5155
text_bg_color=(255, 255, 255),
5256
text_color=(0, 0, 0),
@@ -61,6 +65,10 @@ def add_label(img,
6165
the text (label) to be written
6266
bbox : list
6367
a list containing x_min, y_min, x_max and y_max of the rectangle positions
68+
size : int, optional
69+
size of the label, by default 1
70+
thickness : int, optional
71+
thickness of the label, by default 2
6472
draw_bg : bool, optional
6573
if True, draws the background of the text, else just the text is written, by default True
6674
text_bg_color : tuple, optional
@@ -76,30 +84,29 @@ def add_label(img,
7684
the image with the label written
7785
"""
7886

79-
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][0]
87+
(label_width, label_height), baseline = cv2.getTextSize(label, font, size, thickness)
8088

8189
if top:
82-
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] - 30]
90+
label_bg = [bbox[0], bbox[1], bbox[0] + label_width, bbox[1] - label_height - (15 * size)]
8391
if draw_bg:
8492
cv2.rectangle(img, (label_bg[0], label_bg[1]),
8593
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
86-
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - 5),
87-
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
8894

95+
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - (15 * size)), font, size, text_color, thickness)
8996
else:
90-
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] + 30]
97+
label_bg = [bbox[0], bbox[1], bbox[0] + label_width, bbox[1] + label_height + (15 * size)]
9198
if draw_bg:
9299
cv2.rectangle(img, (label_bg[0], label_bg[1]),
93100
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
94-
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - 5 + 30),
95-
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
96-
101+
cv2.putText(img, label, (bbox[0] + 5, bbox[1] + (16 * size) + (4 * thickness)), font, size, text_color, thickness)
97102
return img
98103

99104

100105
def add_T_label(img,
101106
label,
102107
bbox,
108+
size=1,
109+
thickness=2,
103110
draw_bg=True,
104111
text_bg_color=(255, 255, 255),
105112
text_color=(0, 0, 0)):
@@ -113,6 +120,10 @@ def add_T_label(img,
113120
the text (label) to be written
114121
bbox : list
115122
a list containing x_min, y_min, x_max and y_max of the rectangle positions
123+
size : int, optional
124+
size of the label, by default 1
125+
thickness : int, optional
126+
thickness of the label, by default 2
116127
draw_bg : bool, optional
117128
if True, draws the background of the text, else just the text is written, by default True
118129
text_bg_color : tuple, optional
@@ -126,35 +137,36 @@ def add_T_label(img,
126137
the image with the T label drawn/written
127138
"""
128139

129-
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][0]
130-
text_height = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][1]
131-
140+
(label_width, label_height), baseline = cv2.getTextSize(label, font, size, thickness)
132141
# draw vertical line
133142
x_center = (bbox[0] + bbox[2]) // 2
134143
y_top = bbox[1] - 50
135144
cv2.line(img, (x_center, bbox[1]), (x_center, y_top), text_bg_color, 3)
136145

137146
# draw rectangle with label
138147
y_bottom = y_top
139-
y_top = y_bottom - text_height - 5
140-
x_left = x_center - (text_width // 2) - 5
141-
x_right = x_center + (text_width // 2) + 5
148+
y_top = y_bottom - label_height - 5
149+
x_left = x_center - (label_width // 2) - 5
150+
x_right = x_center + (label_width // 2) + 5
142151
if draw_bg:
143-
cv2.rectangle(img, (x_left, y_top - 3), (x_right, y_bottom),
152+
cv2.rectangle(img, (x_left, y_top - 30), (x_right, y_bottom),
144153
text_bg_color, -1)
145-
cv2.putText(img, label, (x_left + 5, y_bottom - 7),
146-
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
154+
cv2.putText(img, label, (x_left + 5, y_bottom - (8 * size)),
155+
font, size, text_color, thickness)
147156

148157
return img
149158

150159

151160
def draw_flag_with_label(img,
152161
label,
153162
bbox,
163+
size=1,
164+
thickness=2,
154165
write_label=True,
155166
line_color=(255, 255, 255),
156167
text_bg_color=(255, 255, 255),
157-
text_color=(0, 0, 0)):
168+
text_color=(0, 0, 0),
169+
):
158170
"""draws a pole from the middle of the object that is to be labeled and adds the label to the flag
159171
160172
Parameters
@@ -165,6 +177,10 @@ def draw_flag_with_label(img,
165177
label that is written inside the flag
166178
bbox : list
167179
a list containing x_min, y_min, x_max and y_max of the rectangle positions
180+
size : int, optional
181+
size of the label, by default 1
182+
thickness : int, optional
183+
thickness of the label, by default 2
168184
write_label : bool, optional
169185
if True, writes the label, otherwise, it's just a vertical line, by default True
170186
line_color : tuple, optional
@@ -181,6 +197,7 @@ def draw_flag_with_label(img,
181197
"""
182198

183199
# draw vertical line
200+
(label_width, label_height), baseline = cv2.getTextSize(label, font, size, thickness)
184201

185202
x_center = (bbox[0] + bbox[2]) // 2
186203
y_bottom = int((bbox[1] * .75 + bbox[3] * .25))
@@ -194,17 +211,14 @@ def draw_flag_with_label(img,
194211
# write label
195212

196213
if write_label:
197-
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1,
198-
2)[0][0]
199214
label_bg = [
200-
start_point[0], start_point[1], start_point[0] + text_width,
201-
start_point[1] + 30
215+
start_point[0], start_point[1], start_point[0] + label_width,
216+
start_point[1] - label_height - (10 * size)
202217
]
203218
cv2.rectangle(img, (label_bg[0], label_bg[1]),
204219
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
205-
cv2.putText(img, label, (start_point[0] + 5, start_point[1] - 5 + 30),
206-
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
207-
220+
cv2.putText(img, label, (start_point[0] + 7, start_point[1] - (13 * size) + 20),
221+
font, size, text_color, thickness)
208222
return img
209223

210224

0 commit comments

Comments
 (0)