@@ -196,41 +196,91 @@ def do_nms_sort(dets, classes, thresh):
196196 dets [j ]["prob" ][k ] = 0
197197
198198
199+ def get_detections (im , det , thresh , names , classes ):
200+ "Draw the markings around the detected region"
201+ labelstr = []
202+ category = - 1
203+ detection = None
204+ valid = False
205+ for j in range (classes ):
206+ if det ["prob" ][j ] > thresh :
207+ if category == - 1 :
208+ category = j
209+ labelstr .append (names [j ] + " " + str (round (det ["prob" ][j ], 4 )))
210+
211+ if category > - 1 :
212+ valid = True
213+ imc , imh , imw = im .shape
214+ width = int (imh * 0.006 )
215+ offset = category * 123457 % classes
216+ red = _get_color (2 , offset , classes )
217+ green = _get_color (1 , offset , classes )
218+ blue = _get_color (0 , offset , classes )
219+ rgb = [red , green , blue ]
220+ b = det ["bbox" ]
221+ left = int ((b .x - b .w / 2.0 ) * imw )
222+ right = int ((b .x + b .w / 2.0 ) * imw )
223+ top = int ((b .y - b .h / 2.0 ) * imh )
224+ bot = int ((b .y + b .h / 2.0 ) * imh )
225+
226+ if left < 0 :
227+ left = 0
228+ if right > imw - 1 :
229+ right = imw - 1
230+ if top < 0 :
231+ top = 0
232+ if bot > imh - 1 :
233+ bot = imh - 1
234+
235+ detection = {
236+ "category" : category ,
237+ "labelstr" : labelstr ,
238+ "left" : left ,
239+ "top" : top ,
240+ "right" : right ,
241+ "bot" : bot ,
242+ "width" : width ,
243+ "rgb" : rgb ,
244+ }
245+
246+ return valid , detection
247+
248+
199249def draw_detections (font_path , im , dets , thresh , names , classes ):
200250 "Draw the markings around the detected region"
201251 for det in dets :
202- labelstr = []
203- category = - 1
204- for j in range ( classes ):
205- if det [ "prob" ][ j ] > thresh :
206- if category == - 1 :
207- category = j
208- labelstr . append ( names [ j ] + " " + str ( round ( det [ "prob" ][ j ], 4 )))
209- if category > - 1 :
210- imc , imh , imw = im . shape
211- width = int ( imh * 0.006 )
212- offset = category * 123457 % classes
213- red = _get_color ( 2 , offset , classes )
214- green = _get_color ( 1 , offset , classes )
215- blue = _get_color ( 0 , offset , classes )
216- rgb = [ red , green , blue ]
217- b = det [ "bbox" ]
218- left = int (( b . x - b . w / 2.0 ) * imw )
219- right = int (( b . x + b . w / 2.0 ) * imw )
220- top = int (( b . y - b . h / 2.0 ) * imh )
221- bot = int (( b . y + b . h / 2.0 ) * imh )
222-
223- if left < 0 :
224- left = 0
225- if right > imw - 1 :
226- right = imw - 1
227- if top < 0 :
228- top = 0
229- if bot > imh - 1 :
230- bot = imh - 1
231- _draw_box_width ( im , left , top , right , bot , width , red , green , blue )
232- label = _get_label ( font_path , "" . join ( labelstr ), rgb )
233- _draw_label ( im , top + width , left , label , rgb )
252+ valid , detection = get_detections ( im , det , thresh , names , classes )
253+ if valid :
254+ rgb = detection [ "rgb" ]
255+ label = _get_label ( font_path , "" . join ( detection [ "labelstr" ]), rgb )
256+ _draw_box_width (
257+ im ,
258+ detection [ "left" ],
259+ detection [ "top" ],
260+ detection [ "right" ],
261+ detection [ "bot" ],
262+ detection [ "width" ],
263+ rgb [ 0 ],
264+ rgb [ 1 ],
265+ rgb [ 2 ],
266+ )
267+ _draw_label ( im , detection [ "top" ] + detection [ "width" ], detection [ "left" ], label , rgb )
268+
269+
270+ def show_detections ( im , dets , thresh , names , classes ):
271+ "Print the markings and the detected region"
272+ for det in dets :
273+ valid , detection = get_detections ( im , det , thresh , names , classes )
274+ if valid :
275+ print (
276+ "class:{} left:{} right:{} top:{} bottom:{}" . format (
277+ detection [ "labelstr" ],
278+ detection [ "left" ],
279+ detection [ "top" ],
280+ detection [ "right" ],
281+ detection [ "bot" ],
282+ )
283+ )
234284
235285
236286def _get_pixel (im , x , y , c ):
0 commit comments