1818from .annotation import PanopticAnnotation , SegmentInfo , get_annotation_lookup
1919
2020
21+
22+
2123def vector_to_coco_segment_info (canvas : np .ndarray ,
2224 annotation : ObjectAnnotation ,
2325 annotation_idx : int , image : CocoImage ,
2426 category_id : int ):
27+
2528 shapely = annotation .value .shapely
29+ if shapely .is_empty :
30+ return
31+
2632 xmin , ymin , xmax , ymax = shapely .bounds
2733 canvas = annotation .value .draw (height = image .height ,
2834 width = image .width ,
@@ -40,6 +46,9 @@ def mask_to_coco_segment_info(canvas: np.ndarray, annotation,
4046 color = id_to_rgb (annotation_idx )
4147 mask = annotation .value .draw (color = color )
4248 shapely = annotation .value .shapely
49+ if shapely .is_empty :
50+ return
51+
4352 xmin , ymin , xmax , ymax = shapely .bounds
4453 canvas = np .where (canvas == (0 , 0 , 0 ), mask , canvas )
4554 return SegmentInfo (id = annotation_idx ,
@@ -70,20 +79,32 @@ def process_label(label: Label,
7079 for annotation_idx , annotation in enumerate (annotations [class_name ]):
7180 categories [annotation .name ] = hash_category_name (annotation .name )
7281 if isinstance (annotation .value , Mask ):
73- segment , canvas = ( mask_to_coco_segment_info (
82+ coco_segment_info = mask_to_coco_segment_info (
7483 canvas , annotation , class_idx + 1 ,
75- categories [annotation .name ]))
84+ categories [annotation .name ])
85+
86+ if coco_segment_info is None :
87+ # Filter out empty masks
88+ continue
89+
90+ segment , canvas = coco_segment_info
7691 segments .append (segment )
7792 is_thing [annotation .name ] = 0
7893
7994 elif isinstance (annotation .value , (Polygon , Rectangle )):
80- segment , canvas = vector_to_coco_segment_info (
95+ coco_vector_info = vector_to_coco_segment_info (
8196 canvas ,
8297 annotation ,
8398 annotation_idx = (class_idx if all_stuff else annotation_idx )
8499 + 1 ,
85100 image = image ,
86101 category_id = categories [annotation .name ])
102+
103+ if coco_segment_info is None :
104+ # Filter out empty annotations
105+ continue
106+
107+ segment , canvas = coco_vector_info
87108 segments .append (segment )
88109 is_thing [annotation .name ] = 1 - int (all_stuff )
89110
0 commit comments