Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / preprocessor / preprocessing_executor.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 from ..config import ConfigValidator, StringField
18 from ..preprocessor.preprocessors import Preprocessor
19
20
21 class PreprocessingExecutor:
22     def __init__(self, processors=None, dataset_name='custom', dataset_meta=None):
23         self.processors = []
24         self.dataset_meta = dataset_meta
25
26         if not processors:
27             return
28
29         identifier = 'type'
30         for processor in processors:
31             preprocessor_config = PreprocessorConfig(
32                 "{}.preprocessors".format(dataset_name), on_extra_argument=ConfigValidator.IGNORE_ON_EXTRA_ARGUMENT
33             )
34
35             type_ = processor.get(identifier)
36             preprocessor_config.validate(processor, type_)
37             preprocessor = Preprocessor.provide(processor[identifier], config=processor, name=type_)
38
39             self.processors.append(preprocessor)
40
41     def process(self, images, batch_annotation=None):
42         for i, _ in enumerate(images):
43             for processor in self.processors:
44                 images[i] = processor(
45                     image=images[i], annotation_meta=batch_annotation[i].metadata if batch_annotation else None
46                 )
47
48         return images
49
50
51 class PreprocessorConfig(ConfigValidator):
52     type = StringField(choices=Preprocessor.providers)