Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / format_converter.py
1 """
2 Copyright (C) 2018-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 from argparse import ArgumentParser
17
18 from ..config import ConfigValidator, StringField, PathField
19 from ..dependency import ClassProvider
20 from ..utils import format_key
21
22
23 class BaseFormatConverterConfig(ConfigValidator):
24     converter = StringField()
25
26
27 class BaseFormatConverter(ClassProvider):
28     __provider_type__ = 'converter'
29
30     _config_validator_type = BaseFormatConverterConfig
31
32     @property
33     def config_validator(self):
34         return self._config_validator_type(
35             '{}_converter_config'.format(self.get_name()),
36             on_extra_argument=self._config_validator_type.ERROR_ON_EXTRA_ARGUMENT
37         )
38
39     def __init__(self, config=None):
40         self.config = config
41         if config:
42             self.validate_config()
43             self.configure()
44
45     def convert(self, *args, **kwargs):
46         """
47         Converts specific annotation format to the ResultRepresentation specific for current dataset/task.
48
49         Returns:
50             annotation: list of ResultRepresentations.
51             meta: meta-data map for the current dataset.
52         """
53         raise NotImplementedError
54
55     @classmethod
56     def get_name(cls):
57         return cls.__provider__
58
59     def get_argparser(self):
60         parser = ArgumentParser(add_help=False)
61         config_validator = self.config_validator
62         fields = config_validator.fields
63         for field_name, field in fields.items():
64             if field_name == 'converter':
65                 # it is base argument. Main argparser already use it to get argparser from specific converter.
66                 # Converter argparser should contain only converter specific arguments.
67                 continue
68
69             required = not field.optional
70             parser.add_argument(
71                 format_key(field_name), required=required, type=field.type
72             )
73
74         return parser
75
76     def validate_config(self):
77         self.config_validator.validate(self.config)
78
79     def configure(self):
80         pass
81
82
83 class FileBasedAnnotationConverterConfig(BaseFormatConverterConfig):
84     annotation_file = PathField()
85
86
87 class FileBasedAnnotationConverter(BaseFormatConverter):
88     _config_validator_type = FileBasedAnnotationConverterConfig
89
90     def configure(self):
91         self.annotation_file = self.config['annotation_file']
92
93     def convert(self, *args, **kwargs):
94         pass
95
96
97 class DirectoryBasedAnnotationConverterConfig(BaseFormatConverterConfig):
98     data_dir = PathField(is_directory=True)
99
100
101 class DirectoryBasedAnnotationConverter(BaseFormatConverter):
102     _config_validator_type = DirectoryBasedAnnotationConverterConfig
103
104     def configure(self):
105         self.data_dir = self.config['data_dir']
106
107     def convert(self, *args, **kwargs):
108         pass