11import warnings
22
3+ import matplotlib .pyplot as plt
34import mmcv
45import numpy as np
56import pycocotools .mask as maskUtils
@@ -105,6 +106,7 @@ def show_result(img,
105106 class_names ,
106107 score_thr = 0.3 ,
107108 wait_time = 0 ,
109+ show = True ,
108110 out_file = None ):
109111 """Visualize the detection results on the image.
110112
@@ -115,11 +117,17 @@ def show_result(img,
115117 class_names (list[str] or tuple[str]): A list of class names.
116118 score_thr (float): The threshold to visualize the bboxes and masks.
117119 wait_time (int): Value of waitKey param.
120+ show (bool, optional): Whether to show the image with opencv or not.
118121 out_file (str, optional): If specified, the visualization result will
119122 be written to the out file instead of shown in a window.
123+
124+ Returns:
125+ np.ndarray or None: If neither `show` nor `out_file` is specified, the
126+ visualized image is returned, otherwise None is returned.
120127 """
121128 assert isinstance (class_names , (tuple , list ))
122129 img = mmcv .imread (img )
130+ img = img .copy ()
123131 if isinstance (result , tuple ):
124132 bbox_result , segm_result = result
125133 else :
@@ -140,11 +148,36 @@ def show_result(img,
140148 ]
141149 labels = np .concatenate (labels )
142150 mmcv .imshow_det_bboxes (
143- img . copy () ,
151+ img ,
144152 bboxes ,
145153 labels ,
146154 class_names = class_names ,
147155 score_thr = score_thr ,
148- show = out_file is None ,
156+ show = show ,
149157 wait_time = wait_time ,
150158 out_file = out_file )
159+ if not (show or out_file ):
160+ return img
161+
162+
163+ def show_result_pyplot (img ,
164+ result ,
165+ class_names ,
166+ score_thr = 0.3 ,
167+ fig_size = (15 , 10 )):
168+ """Visualize the detection results on the image.
169+
170+ Args:
171+ img (str or np.ndarray): Image filename or loaded image.
172+ result (tuple[list] or list): The detection result, can be either
173+ (bbox, segm) or just bbox.
174+ class_names (list[str] or tuple[str]): A list of class names.
175+ score_thr (float): The threshold to visualize the bboxes and masks.
176+ fig_size (tuple): Figure size of the pyplot figure.
177+ out_file (str, optional): If specified, the visualization result will
178+ be written to the out file instead of shown in a window.
179+ """
180+ img = show_result (
181+ img , result , class_names , score_thr = score_thr , show = False )
182+ plt .figure (figsize = fig_size )
183+ plt .imshow (mmcv .bgr2rgb (img ))
0 commit comments