2 Copyright (C) 2018-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 argparse import ArgumentParser
18 from ..config import ConfigValidator, StringField, PathField
19 from ..dependency import ClassProvider
20 from ..utils import format_key
23 class BaseFormatConverterConfig(ConfigValidator):
24 converter = StringField()
27 class BaseFormatConverter(ClassProvider):
28 __provider_type__ = 'converter'
30 _config_validator_type = BaseFormatConverterConfig
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
39 def __init__(self, config=None):
42 self.validate_config()
45 def convert(self, *args, **kwargs):
47 Converts specific annotation format to the ResultRepresentation specific for current dataset/task.
50 annotation: list of ResultRepresentations.
51 meta: meta-data map for the current dataset.
53 raise NotImplementedError
57 return cls.__provider__
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.
69 required = not field.optional
71 format_key(field_name), required=required, type=field.type
76 def validate_config(self):
77 self.config_validator.validate(self.config)
83 class FileBasedAnnotationConverterConfig(BaseFormatConverterConfig):
84 annotation_file = PathField()
87 class FileBasedAnnotationConverter(BaseFormatConverter):
88 _config_validator_type = FileBasedAnnotationConverterConfig
91 self.annotation_file = self.config['annotation_file']
93 def convert(self, *args, **kwargs):
97 class DirectoryBasedAnnotationConverterConfig(BaseFormatConverterConfig):
98 data_dir = PathField(is_directory=True)
101 class DirectoryBasedAnnotationConverter(BaseFormatConverter):
102 _config_validator_type = DirectoryBasedAnnotationConverterConfig
105 self.data_dir = self.config['data_dir']
107 def convert(self, *args, **kwargs):