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.
16 from functools import singledispatch
17 from typing import Union
19 from ..config import StringField
20 from ..representation import DetectionAnnotation, DetectionPrediction, TextDetectionPrediction, TextDetectionAnnotation
21 from .postprocessor import Postprocessor, BasePostprocessorConfig
24 class CastToInt(Postprocessor):
25 __provider__ = 'cast_to_int'
26 annotation_types = (DetectionAnnotation, TextDetectionAnnotation)
27 prediction_types = (DetectionPrediction, TextDetectionPrediction)
29 round_policies_func = {
31 'nearest_to_zero': np.trunc,
36 def validate_config(self):
37 class _CastToIntConfigValidator(BasePostprocessorConfig):
38 round_policy = StringField(optional=True, choices=self.round_policies_func.keys())
40 cast_to_int_config_validator = _CastToIntConfigValidator(
41 self.__provider__, on_extra_argument=_CastToIntConfigValidator.ERROR_ON_EXTRA_ARGUMENT
43 cast_to_int_config_validator.validate(self.config)
46 self.round_func = self.round_policies_func[self.config.get('round_policy', 'nearest')]
48 def process_image(self, annotation, prediction):
53 @cast.register(Union[DetectionAnnotation, DetectionPrediction])
55 entry.x_mins = self.round_func(entry.x_mins)
56 entry.x_maxs = self.round_func(entry.x_maxs)
57 entry.y_mins = self.round_func(entry.y_mins)
58 entry.y_maxs = self.round_func(entry.y_maxs)
60 @cast.register(Union[TextDetectionAnnotation, TextDetectionPrediction])
62 entry.points = self.round_func(entry.points)
65 for annotation_ in annotation:
68 for prediction_ in prediction:
71 return annotation, prediction