@@ -315,12 +315,56 @@ def draw_text_on_image(image, string, position, color = (77, 255, 9)):
315315 cv2 .FONT_HERSHEY_SIMPLEX , 0.75 , color , 2 )
316316
317317
318- def visualize_objectdetection (image , boxes , classes , scores , masks , category_index ,
319- fps = None , visualize = False , det_interval = 5 , det_th = 0.5 ,
320- max_frames = 500 , cur_frame = None , model = 'realtime_object_detection' ):
318+ def exit_visualization (millis ):
321319 """
322- complicated visualization function for object detections
323- TODO: CLEAN UP
320+ returns false if openCV exit is requested
321+ """
322+ if cv2 .waitKey (millis ) & 0xFF == ord ('q' ):
323+ return False
324+ return True
325+
326+ def exit_print (cur_frame ,max_frames ):
327+ """
328+ returns false if max frames are reached
329+ """
330+ if cur_frame >= max_frames :
331+ return False
332+ return True
333+
334+ def print_detection (boxes ,scores ,classes ,category_index ,cur_frame ,max_frames = 500 ,print_interval = 100 ,print_th = 0.5 ):
335+ """
336+ prints detection result above threshold to console
337+ """
338+ for box , score , _class in zip (boxes , scores , classes ):
339+ if cur_frame % print_interval == 0 and score > print_th :
340+ label = category_index [_class ]['name' ]
341+ print ("label: {}\n score: {}\n box: {}" .format (label , score , box ))
342+
343+ def draw_single_box_on_image (image ,box ,label ):
344+ """
345+ draws single box and label on image
346+ """
347+ p1 = (box [1 ], box [0 ])
348+ p2 = (box [3 ], box [2 ])
349+ cv2 .rectangle (image , p1 , p2 , (77 ,255 ,9 ), 2 )
350+ draw_text_on_image (image ,label ,(p1 [0 ],p1 [1 ]- 10 ))
351+
352+
353+ def visualize_objectdetection (image ,
354+ boxes ,
355+ classes ,
356+ scores ,
357+ masks ,
358+ category_index ,
359+ cur_frame ,
360+ max_frames = 500 ,
361+ fps = 'N/A' ,
362+ print_interval = 100 ,
363+ print_th = 0.5 ,
364+ model_name = 'realtime_object_detection' ,
365+ visualize = True ):
366+ """
367+ visualization function for object_detection
324368 """
325369 if visualize :
326370 visualize_boxes_and_labels_on_image (
@@ -332,44 +376,31 @@ def visualize_objectdetection(image, boxes, classes, scores, masks, category_ind
332376 instance_masks = masks ,
333377 use_normalized_coordinates = True ,
334378 line_thickness = 2 )
335- if fps :
336- draw_text_on_image (image ,"fps: {}" .format (fps ), (10 ,30 ))
337- cv2 .imshow (model , image )
338- elif not visualize and cur_frame :
339- # Exit after max frames if no visualization
340- for box , score , _class in zip (boxes , scores , classes ):
341- if cur_frame % det_interval == 0 and score > det_th :
342- label = category_index [_class ]['name' ]
343- print ("label: {}\n score: {}\n box: {}" .format (label , score , box ))
344- elif fps == "console" :
345- for box , score , _class in zip (boxes , scores , classes ):
346- if score > det_th :
347- label = category_index [_class ]['name' ]
348- print ("label: {}\n score: {}\n box: {}" .format (label , score , box ))
349- # Exit Option
350- if visualize :
351- if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
352- return False
353- elif not visualize and fps :
354- if cur_frame >= max_frames :
355- return False
356- return True
357-
358- def draw_single_box_on_image (image ,box ,label ):
359- p1 = (box [1 ], box [0 ])
360- p2 = (box [3 ], box [2 ])
361- cv2 .rectangle (image , p1 , p2 , (77 ,255 ,9 ), 2 )
362- draw_text_on_image (image ,label ,(p1 [0 ],p1 [1 ]- 10 ))
363-
379+ draw_text_on_image (image ,"fps: {}" .format (fps ), (10 ,30 ))
380+ cv2 .imshow (model_name , image )
381+ vis = exit_visualization (1 )
382+ else :
383+ print_detection (boxes ,scores ,classes ,category_index ,cur_frame ,max_frames ,print_interval ,print_th )
384+ vis = exit_print (cur_frame ,max_frames )
385+ return vis
364386
365- def visualize_deeplab (image ,seg_map ,model_name ,fps ,visualize = True ):
387+ def visualize_deeplab (image ,
388+ seg_map ,
389+ cur_frame ,
390+ max_frames = 500 ,
391+ fps = 'N/A' ,
392+ print_interval = 100 ,
393+ print_th = 0.5 ,
394+ model_name = 'DeepLab' ,
395+ visualize = True ):
396+ """
397+ visualization function for deeplab
398+ """
366399 if visualize :
367400 draw_mask_on_image (image , seg_map )
368401 draw_text_on_image (image ,"fps: {}" .format (fps ),(10 ,30 ))
369402 cv2 .imshow (model_name ,image )
370- if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
371- return False
372- else :
373- return True
403+ vis = exit_visualization (1 )
374404 else :
375- return True
405+ vis = exit_print (cur_frame ,max_frames )
406+ return vis
0 commit comments