From d6de382c6ffce6aeba1702cd12bfb5a01bbfc81c Mon Sep 17 00:00:00 2001 From: Guozhen Ding Date: Fri, 8 Dec 2023 13:21:28 -0500 Subject: [PATCH 1/3] Enable the switch for print out segement only to accomdate #3427 --- demo/image_demo.py | 7 ++++++- mmseg/apis/inference.py | 4 +++- mmseg/visualization/local_visualizer.py | 5 ++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/demo/image_demo.py b/demo/image_demo.py index 231aacb9dd..4da6b02396 100644 --- a/demo/image_demo.py +++ b/demo/image_demo.py @@ -21,6 +21,10 @@ def main(): help='Opacity of painted segmentation map. In (0, 1] range.') parser.add_argument( '--title', default='result', help='The image identifier.') + parser.add_argument( + '--segementation-only', + action='store_true', + help='Only show the segmentation map.') args = parser.parse_args() # build the model from a config file and a checkpoint file @@ -38,7 +42,8 @@ def main(): opacity=args.opacity, draw_gt=False, show=False if args.out_file is not None else True, - out_file=args.out_file) + out_file=args.out_file, + segmentation_only=args.segementation_only) if __name__ == '__main__': diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index 57fc5d23dc..7f940f4354 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -128,6 +128,7 @@ def show_result_pyplot(model: BaseSegmentor, wait_time: float = 0, show: bool = True, withLabels: Optional[bool] = True, + segmentation_only: bool = False, save_dir=None, out_file=None): """Visualize the segmentation results on the image. @@ -170,7 +171,8 @@ def show_result_pyplot(model: BaseSegmentor, visualizer = SegLocalVisualizer( vis_backends=[dict(type='LocalVisBackend')], save_dir=save_dir, - alpha=opacity) + alpha=opacity, + segement_only=segmentation_only) visualizer.dataset_meta = dict( classes=model.dataset_meta['classes'], palette=model.dataset_meta['palette']) diff --git a/mmseg/visualization/local_visualizer.py b/mmseg/visualization/local_visualizer.py index 3096e3183b..3790549350 100644 --- a/mmseg/visualization/local_visualizer.py +++ b/mmseg/visualization/local_visualizer.py @@ -73,9 +73,12 @@ def __init__(self, palette: Optional[List] = None, dataset_name: Optional[str] = None, alpha: float = 0.8, + segement_only: bool = False, **kwargs): super().__init__(name, image, vis_backends, save_dir, **kwargs) self.alpha: float = alpha + if segement_only: + self.alpha = 1 self.set_dataset_meta(palette, classes, dataset_name) def _get_center_loc(self, mask: np.ndarray) -> np.ndarray: @@ -139,7 +142,7 @@ def _draw_sem_seg(self, for label, color in zip(labels, colors): mask[sem_seg[0] == label, :] = color - if withLabels: + if withLabels and not self.segement_only: font = cv2.FONT_HERSHEY_SIMPLEX # (0,1] to change the size of the text relative to the image scale = 0.05 From 21957efe4efb1c59204c92d7cec80a4b69bebd43 Mon Sep 17 00:00:00 2001 From: Yoko Date: Sat, 9 Dec 2023 00:17:29 -0500 Subject: [PATCH 2/3] Inseart a warning message when setting both segmentation_only and withLables to true. --- mmseg/apis/inference.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index 7f940f4354..a7c12cca20 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -168,6 +168,13 @@ def show_result_pyplot(model: BaseSegmentor, if save_dir is not None: mkdir_or_exist(save_dir) # init visualizer + if withLabels and segmentation_only: + # Issue a warning if withLabels is True and segmentation_only is True + warnings.simplefilter('once') + warnings.warn( + 'withLabels is True and segmentation_only is True, ' + 'withLabels will be set to False') + withLabels = False visualizer = SegLocalVisualizer( vis_backends=[dict(type='LocalVisBackend')], save_dir=save_dir, From fba6e3fb8098e546de58ab75150b8225f149c426 Mon Sep 17 00:00:00 2001 From: Tonyzhang2000 Date: Sat, 9 Dec 2023 18:37:43 -0500 Subject: [PATCH 3/3] Fixed a small bug and add documentation. --- docs/en/user_guides/visualization.md | 2 ++ mmseg/visualization/local_visualizer.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/docs/en/user_guides/visualization.md b/docs/en/user_guides/visualization.md index e7c3359cc9..e34cfa0ca9 100644 --- a/docs/en/user_guides/visualization.md +++ b/docs/en/user_guides/visualization.md @@ -137,6 +137,8 @@ gt_sem_seg = PixelData(**gt_sem_seg_data) data_sample = SegDataSample() data_sample.gt_sem_seg = gt_sem_seg +# If you want to show the segement data only (without lable text and background) +# you can use segement_only = True. seg_local_visualizer = SegLocalVisualizer( vis_backends=[dict(type='LocalVisBackend')], save_dir=save_dir) diff --git a/mmseg/visualization/local_visualizer.py b/mmseg/visualization/local_visualizer.py index 3790549350..6781c3dfaa 100644 --- a/mmseg/visualization/local_visualizer.py +++ b/mmseg/visualization/local_visualizer.py @@ -39,6 +39,11 @@ class SegLocalVisualizer(Visualizer): Defaults to None. alpha (int, float): The transparency of segmentation mask. Defaults to 0.8. + segement_only (bool): Only show the segmentation map, by setting this flag + to True, only the segmentation map will be shown, the background image + and the labels will not be shown. Defaults to False. + Warning: If withLabels is True and segmentation_only is True, + withLabels will be set to False. i.e. this flag will override withLabels. Examples: >>> import numpy as np @@ -80,6 +85,7 @@ def __init__(self, if segement_only: self.alpha = 1 self.set_dataset_meta(palette, classes, dataset_name) + self.segement_only = segement_only def _get_center_loc(self, mask: np.ndarray) -> np.ndarray: """Get semantic seg center coordinate.