17
17
from ..config import PathField , BoolField
18
18
from ..representation import DetectionAnnotation , SegmentationAnnotation
19
19
from ..representation .segmentation_representation import GTMaskLoader
20
- from ..utils import get_path , read_txt , read_xml , check_file_existence , read_json
20
+ from ..utils import get_path , read_txt , read_xml , check_file_existence , read_json , string_to_tuple
21
21
from .format_converter import BaseFormatConverter , ConverterReturn , verify_label_map
22
22
23
23
_SYG_CLASSES_DETECTION = (
@@ -91,6 +91,7 @@ def syg_prepare_detection_labels(dataset_meta, has_background=True):
91
91
92
92
return reversed_label_map
93
93
94
+
94
95
class PascalVOCSegmentationConverter (BaseFormatConverter ):
95
96
__provider__ = 'voc_segmentation'
96
97
annotation_types = (SegmentationAnnotation , )
@@ -111,7 +112,8 @@ def parameters(cls):
111
112
),
112
113
'dataset_meta_file' : PathField (
113
114
description = 'path to json file with dataset meta (e.g. label_map, color_encoding)' , optional = True
114
- )
115
+ ),
116
+ 'labelmap_file' : PathField (description = 'labelmap.txt in Datumaro format' , optional = True )
115
117
})
116
118
117
119
return configuration_parameters
@@ -121,6 +123,9 @@ def configure(self):
121
123
self .image_dir = self .get_value_from_config ('images_dir' )
122
124
dataset_meta_file = self .get_value_from_config ('dataset_meta_file' )
123
125
self .dataset_meta = {} if not dataset_meta_file else read_json (dataset_meta_file )
126
+ labelmap_file = self .get_value_from_config ('labelmap_file' )
127
+ if labelmap_file is not None :
128
+ self .dataset_meta .update (self .read_labelmap (labelmap_file ))
124
129
if not self .image_dir :
125
130
self .image_dir = get_path (self .image_set_file .parents [- 2 ] / 'JPEGImages' , is_directory = True )
126
131
@@ -156,6 +161,21 @@ def convert(self, check_content=False, progress_callback=None, progress_interval
156
161
157
162
return ConverterReturn (annotations , meta , content_check_errors )
158
163
164
+ @staticmethod
165
+ def read_labelmap (input_file ):
166
+ label_map = {}
167
+ segmentation_colors = []
168
+ idx = 0
169
+ for line in read_txt (input_file ):
170
+ if line .startswith ('#' ):
171
+ continue
172
+ data = line .split (':' )
173
+ label , color = data [:2 ]
174
+ label_map [idx ] = label
175
+ segmentation_colors .append (string_to_tuple (color ))
176
+ idx += 1
177
+ return {'label_map' : label_map , 'segmentation_colors' : segmentation_colors }
178
+
159
179
160
180
class PascalVOCDetectionConverter (BaseFormatConverter ):
161
181
__provider__ = 'voc_detection'
0 commit comments