@@ -1142,24 +1142,39 @@ def __export_index_color_image(self, task: list, output_dir: str, pallete: List[
11421142 image = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
11431143
11441144 index = 1
1145+ # In case segmentation, to avoid hollowed points overwrite other segmentation in them, segmentation rendering process is different from other annotation type
1146+ seg_mask_images = []
11451147 for annotation in task ["annotations" ]:
11461148 color = index if is_instance_segmentation else classes .index (
11471149 annotation ["value" ]) + 1
11481150 if annotation ["type" ] == AnnotationType .segmentation .value :
1151+ # Create each annotation's masks and merge them finally
1152+ seg_mask_ground = Image .new (
1153+ "RGB" , (task ["width" ], task ["height" ]), 0 )
1154+ seg_mask_image = np .array (seg_mask_ground )
1155+ seg_mask_image = cv2 .cvtColor (seg_mask_image , cv2 .COLOR_BGR2GRAY )
1156+
11491157 for region in annotation ["points" ]:
11501158 count = 0
11511159 for points in region :
11521160 if count == 0 :
1153- cv_draw_points = self .__get_cv_draw_points (points )
1161+ cv_draw_points = []
1162+ if utils .is_clockwise (points ):
1163+ cv_draw_points = self .__get_cv_draw_points (
1164+ points )
1165+ else :
1166+ cv_draw_points = self .__get_cv_draw_points (
1167+ utils .reverse_points (points ))
11541168 cv2 .fillPoly (
1155- image , [cv_draw_points ], color , lineType = cv2 .LINE_8 , shift = 0 )
1169+ seg_mask_image , [cv_draw_points ], color , lineType = cv2 .LINE_8 , shift = 0 )
11561170 else :
11571171 # Reverse hollow points for opencv because this points are counter clockwise
11581172 cv_draw_points = self .__get_cv_draw_points (
11591173 utils .reverse_points (points ))
11601174 cv2 .fillPoly (
1161- image , [cv_draw_points ], 0 , lineType = cv2 .LINE_8 , shift = 0 )
1175+ seg_mask_image , [cv_draw_points ], 0 , lineType = cv2 .LINE_8 , shift = 0 )
11621176 count += 1
1177+ seg_mask_images .append (seg_mask_image )
11631178 elif annotation ["type" ] == AnnotationType .polygon .value :
11641179 cv_draw_points = self .__get_cv_draw_points (
11651180 annotation ["points" ])
@@ -1174,6 +1189,10 @@ def __export_index_color_image(self, task: list, output_dir: str, pallete: List[
11741189 continue
11751190 index += 1
11761191
1192+ # For segmentation, merge each mask images
1193+ for seg_mask_image in seg_mask_images :
1194+ image = image | seg_mask_image
1195+
11771196 image_path = os .path .join (
11781197 output_dir , utils .get_basename (task ["name" ]) + ".png" )
11791198 os .makedirs (os .path .dirname (image_path ), exist_ok = True )
0 commit comments