@@ -22,7 +22,11 @@ def vector_to_coco_segment_info(canvas: np.ndarray,
2222 annotation : ObjectAnnotation ,
2323 annotation_idx : int , image : CocoImage ,
2424 category_id : int ):
25+
2526 shapely = annotation .value .shapely
27+ if shapely .is_empty :
28+ return
29+
2630 xmin , ymin , xmax , ymax = shapely .bounds
2731 canvas = annotation .value .draw (height = image .height ,
2832 width = image .width ,
@@ -40,6 +44,9 @@ def mask_to_coco_segment_info(canvas: np.ndarray, annotation,
4044 color = id_to_rgb (annotation_idx )
4145 mask = annotation .value .draw (color = color )
4246 shapely = annotation .value .shapely
47+ if shapely .is_empty :
48+ return
49+
4350 xmin , ymin , xmax , ymax = shapely .bounds
4451 canvas = np .where (canvas == (0 , 0 , 0 ), mask , canvas )
4552 return SegmentInfo (id = annotation_idx ,
@@ -70,20 +77,32 @@ def process_label(label: Label,
7077 for annotation_idx , annotation in enumerate (annotations [class_name ]):
7178 categories [annotation .name ] = hash_category_name (annotation .name )
7279 if isinstance (annotation .value , Mask ):
73- segment , canvas = ( mask_to_coco_segment_info (
80+ coco_segment_info = mask_to_coco_segment_info (
7481 canvas , annotation , class_idx + 1 ,
75- categories [annotation .name ]))
82+ categories [annotation .name ])
83+
84+ if coco_segment_info is None :
85+ # Filter out empty masks
86+ continue
87+
88+ segment , canvas = coco_segment_info
7689 segments .append (segment )
7790 is_thing [annotation .name ] = 0
7891
7992 elif isinstance (annotation .value , (Polygon , Rectangle )):
80- segment , canvas = vector_to_coco_segment_info (
93+ coco_vector_info = vector_to_coco_segment_info (
8194 canvas ,
8295 annotation ,
8396 annotation_idx = (class_idx if all_stuff else annotation_idx )
8497 + 1 ,
8598 image = image ,
8699 category_id = categories [annotation .name ])
100+
101+ if coco_segment_info is None :
102+ # Filter out empty annotations
103+ continue
104+
105+ segment , canvas = coco_vector_info
87106 segments .append (segment )
88107 is_thing [annotation .name ] = 1 - int (all_stuff )
89108
0 commit comments