Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / brats.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 from pathlib import Path
17
18 from ..representation import BrainTumorSegmentationAnnotation
19 from ..utils import get_path
20 from ..config import StringField
21 from .format_converter import BaseFormatConverter, DirectoryBasedAnnotationConverterConfig
22
23
24 class BratsConverterConfig(DirectoryBasedAnnotationConverterConfig):
25     image_folder = StringField(optional=True)
26     mask_folder = StringField(optional=True)
27
28
29 class BratsConverter(BaseFormatConverter):
30     __provider__ = 'brats'
31
32     _config_validator_type = BratsConverterConfig
33
34     def configure(self):
35         self.data_dir = self.config['data_dir']
36         self.image_folder = self.config.get('image_folder', 'imagesTr')
37         self.mask_folder = self.config.get('mask_folder', 'labelsTr')
38
39     def convert(self):
40         mask_folder = Path(self.mask_folder)
41         image_folder = Path(self.image_folder)
42         image_dir = get_path(self.data_dir / image_folder, is_directory=True)
43
44         annotations = []
45         for file_in_dir in image_dir.iterdir():
46             annotation = BrainTumorSegmentationAnnotation(
47                 str(image_folder / file_in_dir.parts[-1]),
48                 str(mask_folder / file_in_dir.parts[-1]),
49             )
50
51             annotations.append(annotation)
52
53         return annotations, None