@@ -61,7 +61,7 @@ def __len__(self):
61
61
def categories (self ):
62
62
return self ._parent .categories ()
63
63
64
- def __init__ (self , path , task ):
64
+ def __init__ (self , path , task , merge_instance_polygons = False ):
65
65
super ().__init__ ()
66
66
67
67
rootpath = path .rsplit (CocoPath .ANNOTATIONS_DIR , maxsplit = 1 )[0 ]
@@ -80,6 +80,8 @@ def __init__(self, path, task):
80
80
81
81
self ._load_categories ()
82
82
83
+ self ._merge_instance_polygons = merge_instance_polygons
84
+
83
85
@staticmethod
84
86
def _make_subset_loader (path ):
85
87
# COCO API has an 'unclosed file' warning
@@ -212,20 +214,22 @@ def _parse_annotation(self, ann, ann_type, parsed_annotations,
212
214
segmentation = ann .get ('segmentation' )
213
215
if segmentation is not None :
214
216
group = ann_id
217
+ rle = None
215
218
216
219
if isinstance (segmentation , list ):
217
- # polygon -- a single object might consist of multiple parts
220
+ # polygon - a single object can consist of multiple parts
218
221
for polygon_points in segmentation :
219
222
parsed_annotations .append (PolygonObject (
220
223
points = polygon_points , label = label_id ,
221
- group = group
224
+ id = ann_id , group = group , attributes = attributes
222
225
))
223
226
224
- # we merge all parts into one mask RLE code
225
- img_h = image_info ['height' ]
226
- img_w = image_info ['width' ]
227
- rles = mask_utils .frPyObjects (segmentation , img_h , img_w )
228
- rle = mask_utils .merge (rles )
227
+ if self ._merge_instance_polygons :
228
+ # merge all parts into a single mask RLE
229
+ img_h = image_info ['height' ]
230
+ img_w = image_info ['width' ]
231
+ rles = mask_utils .frPyObjects (segmentation , img_h , img_w )
232
+ rle = mask_utils .merge (rles )
229
233
elif isinstance (segmentation ['counts' ], list ):
230
234
# uncompressed RLE
231
235
img_h , img_w = segmentation ['size' ]
@@ -234,9 +238,10 @@ def _parse_annotation(self, ann, ann_type, parsed_annotations,
234
238
# compressed RLE
235
239
rle = segmentation
236
240
237
- parsed_annotations .append (RleMask (rle = rle , label = label_id ,
238
- group = group
239
- ))
241
+ if rle is not None :
242
+ parsed_annotations .append (RleMask (rle = rle , label = label_id ,
243
+ id = ann_id , group = group , attributes = attributes
244
+ ))
240
245
241
246
parsed_annotations .append (
242
247
BboxObject (x , y , w , h , label = label_id ,
@@ -277,21 +282,22 @@ def _parse_annotation(self, ann, ann_type, parsed_annotations,
277
282
return parsed_annotations
278
283
279
284
class CocoImageInfoExtractor (CocoExtractor ):
280
- def __init__ (self , path ):
281
- super ().__init__ (path , task = CocoAnnotationType .image_info )
285
+ def __init__ (self , path , ** kwargs ):
286
+ super ().__init__ (path , task = CocoAnnotationType .image_info , ** kwargs )
282
287
283
288
class CocoCaptionsExtractor (CocoExtractor ):
284
- def __init__ (self , path ):
285
- super ().__init__ (path , task = CocoAnnotationType .captions )
289
+ def __init__ (self , path , ** kwargs ):
290
+ super ().__init__ (path , task = CocoAnnotationType .captions , ** kwargs )
286
291
287
292
class CocoInstancesExtractor (CocoExtractor ):
288
- def __init__ (self , path ):
289
- super ().__init__ (path , task = CocoAnnotationType .instances )
293
+ def __init__ (self , path , ** kwargs ):
294
+ super ().__init__ (path , task = CocoAnnotationType .instances , ** kwargs )
290
295
291
296
class CocoPersonKeypointsExtractor (CocoExtractor ):
292
- def __init__ (self , path ):
293
- super ().__init__ (path , task = CocoAnnotationType .person_keypoints )
297
+ def __init__ (self , path , ** kwargs ):
298
+ super ().__init__ (path , task = CocoAnnotationType .person_keypoints ,
299
+ ** kwargs )
294
300
295
301
class CocoLabelsExtractor (CocoExtractor ):
296
- def __init__ (self , path ):
297
- super ().__init__ (path , task = CocoAnnotationType .labels )
302
+ def __init__ (self , path , ** kwargs ):
303
+ super ().__init__ (path , task = CocoAnnotationType .labels , ** kwargs )
0 commit comments