|
| 1 | +""" |
| 2 | +Copyright (c) 2018-2021 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 .format_converter import ConverterReturn, BaseFormatConverter |
| 17 | +from ..config import PathField, ListField, StringField, ConfigError |
| 18 | +from ..representation import ImageProcessingAnnotation |
| 19 | +from .image_processing import LOADERS_MAPPING |
| 20 | +from ..utils import get_path |
| 21 | + |
| 22 | + |
| 23 | +class GANAnnotationConverter(BaseFormatConverter): |
| 24 | + __provider__ = 'image_generation' |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def parameters(cls): |
| 28 | + params = super().parameters() |
| 29 | + params.update({ |
| 30 | + 'data_dir': PathField(description='dataset root directory', is_directory=True), |
| 31 | + 'input_subdirectories': ListField(value_type=str, allow_empty=False, |
| 32 | + description='subdirectories with input data'), |
| 33 | + 'reference_dir': PathField( |
| 34 | + is_directory=True, |
| 35 | + description='path to directory with reference relative to dataset root' |
| 36 | + ), |
| 37 | + 'annotation_loader': StringField( |
| 38 | + optional=True, choices=LOADERS_MAPPING.keys(), default='pillow', |
| 39 | + description="Which library will be used for ground truth image reading. " |
| 40 | + "Supported: {}".format(', '.join(LOADERS_MAPPING.keys())) |
| 41 | + ) |
| 42 | + }) |
| 43 | + return params |
| 44 | + |
| 45 | + def configure(self): |
| 46 | + self.data_dir = self.get_value_from_config('data_dir') |
| 47 | + self.input_subdir = [ |
| 48 | + get_path(self.data_dir / path, is_directory=True) |
| 49 | + for path in self.get_value_from_config('input_subdirectories') |
| 50 | + ] |
| 51 | + self.reference_dir = self.get_value_from_config('reference_dir') |
| 52 | + self.annotation_loader = LOADERS_MAPPING.get(self.get_value_from_config('annotation_loader')) |
| 53 | + if not self.annotation_loader: |
| 54 | + raise ConfigError('provided not existing loader') |
| 55 | + |
| 56 | + def convert(self, check_content=False, progress_callback=None, progress_interval=100, **kwargs): |
| 57 | + annotations = [] |
| 58 | + for ref_file in self.reference_dir.glob('*'): |
| 59 | + input_files = [] |
| 60 | + for input_path in self.input_subdir: |
| 61 | + input_candidate = list(input_path.glob('{}.*'.format(ref_file.stem))) |
| 62 | + if not input_candidate: |
| 63 | + raise ConfigError('Input data for {} is not found'.format(ref_file)) |
| 64 | + input_files.append(str(input_candidate[0].relative_to(self.data_dir))) |
| 65 | + identifier = input_files if len(input_files) > 1 else input_files[0] |
| 66 | + ref_path = ref_file.name |
| 67 | + annotations.append(ImageProcessingAnnotation(identifier, ref_path, gt_loader=self.annotation_loader)) |
| 68 | + return ConverterReturn(annotations, None, None) |
0 commit comments