1
1
import cv2
2
2
3
+ font = cv2 .FONT_HERSHEY_SIMPLEX
4
+
3
5
4
6
def draw_rectangle (img ,
5
7
bbox ,
@@ -47,6 +49,8 @@ def draw_rectangle(img,
47
49
def add_label (img ,
48
50
label ,
49
51
bbox ,
52
+ size = 1 ,
53
+ thickness = 2 ,
50
54
draw_bg = True ,
51
55
text_bg_color = (255 , 255 , 255 ),
52
56
text_color = (0 , 0 , 0 ),
@@ -61,6 +65,10 @@ def add_label(img,
61
65
the text (label) to be written
62
66
bbox : list
63
67
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
64
72
draw_bg : bool, optional
65
73
if True, draws the background of the text, else just the text is written, by default True
66
74
text_bg_color : tuple, optional
@@ -76,30 +84,29 @@ def add_label(img,
76
84
the image with the label written
77
85
"""
78
86
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 )
80
88
81
89
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 ) ]
83
91
if draw_bg :
84
92
cv2 .rectangle (img , (label_bg [0 ], label_bg [1 ]),
85
93
(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 )
88
94
95
+ cv2 .putText (img , label , (bbox [0 ] + 5 , bbox [1 ] - (15 * size )), font , size , text_color , thickness )
89
96
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 ) ]
91
98
if draw_bg :
92
99
cv2 .rectangle (img , (label_bg [0 ], label_bg [1 ]),
93
100
(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 )
97
102
return img
98
103
99
104
100
105
def add_T_label (img ,
101
106
label ,
102
107
bbox ,
108
+ size = 1 ,
109
+ thickness = 2 ,
103
110
draw_bg = True ,
104
111
text_bg_color = (255 , 255 , 255 ),
105
112
text_color = (0 , 0 , 0 )):
@@ -113,6 +120,10 @@ def add_T_label(img,
113
120
the text (label) to be written
114
121
bbox : list
115
122
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
116
127
draw_bg : bool, optional
117
128
if True, draws the background of the text, else just the text is written, by default True
118
129
text_bg_color : tuple, optional
@@ -126,35 +137,36 @@ def add_T_label(img,
126
137
the image with the T label drawn/written
127
138
"""
128
139
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 )
132
141
# draw vertical line
133
142
x_center = (bbox [0 ] + bbox [2 ]) // 2
134
143
y_top = bbox [1 ] - 50
135
144
cv2 .line (img , (x_center , bbox [1 ]), (x_center , y_top ), text_bg_color , 3 )
136
145
137
146
# draw rectangle with label
138
147
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
142
151
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 ),
144
153
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 )
147
156
148
157
return img
149
158
150
159
151
160
def draw_flag_with_label (img ,
152
161
label ,
153
162
bbox ,
163
+ size = 1 ,
164
+ thickness = 2 ,
154
165
write_label = True ,
155
166
line_color = (255 , 255 , 255 ),
156
167
text_bg_color = (255 , 255 , 255 ),
157
- text_color = (0 , 0 , 0 )):
168
+ text_color = (0 , 0 , 0 ),
169
+ ):
158
170
"""draws a pole from the middle of the object that is to be labeled and adds the label to the flag
159
171
160
172
Parameters
@@ -165,6 +177,10 @@ def draw_flag_with_label(img,
165
177
label that is written inside the flag
166
178
bbox : list
167
179
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
168
184
write_label : bool, optional
169
185
if True, writes the label, otherwise, it's just a vertical line, by default True
170
186
line_color : tuple, optional
@@ -181,6 +197,7 @@ def draw_flag_with_label(img,
181
197
"""
182
198
183
199
# draw vertical line
200
+ (label_width , label_height ), baseline = cv2 .getTextSize (label , font , size , thickness )
184
201
185
202
x_center = (bbox [0 ] + bbox [2 ]) // 2
186
203
y_bottom = int ((bbox [1 ] * .75 + bbox [3 ] * .25 ))
@@ -194,17 +211,14 @@ def draw_flag_with_label(img,
194
211
# write label
195
212
196
213
if write_label :
197
- text_width = cv2 .getTextSize (label , cv2 .FONT_HERSHEY_SIMPLEX , 1 ,
198
- 2 )[0 ][0 ]
199
214
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 )
202
217
]
203
218
cv2 .rectangle (img , (label_bg [0 ], label_bg [1 ]),
204
219
(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 )
208
222
return img
209
223
210
224
0 commit comments