2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 from .postprocessor import Postprocessor
20 from ..representation import SegmentationAnnotation, SegmentationPrediction
23 class EncodeSegMask(Postprocessor):
25 Encode segmentation label image as segmentation mask.
28 __provider__ = 'encode_segmentation_mask'
30 annotation_types = (SegmentationAnnotation, )
31 prediction_types = (SegmentationPrediction, )
33 def process_image(self, annotation, prediction):
34 segmentation_colors = self.meta.get("segmentation_colors")
36 if not segmentation_colors:
37 raise ValueError("No 'segmentation_colors' in dataset metadata.")
39 for annotation_ in annotation:
40 mask = annotation_.mask.astype(int)
41 encoded_mask = np.zeros((mask.shape[0], mask.shape[1]), dtype=np.int16)
42 for label, color in enumerate(segmentation_colors):
43 encoded_mask[np.where(np.all(mask == color, axis=-1))[:2]] = label
44 annotation_.mask = encoded_mask
46 return annotation, prediction