diff --git a/fastlabel/__init__.py b/fastlabel/__init__.py index 4b7794a..f071e16 100644 --- a/fastlabel/__init__.py +++ b/fastlabel/__init__.py @@ -2308,10 +2308,16 @@ def __export_index_color_image( if count == 0: cv_draw_points = [] if utils.is_clockwise(points): - cv_draw_points = self.__get_cv_draw_points(points) + cv_draw_points = self.__get_cv_draw_points( + utils.sort_segmentation_points(points) + ) else: + reverse_points = utils.reverse_points(points) + sorted_points = utils.sort_segmentation_points( + reverse_points + ) cv_draw_points = self.__get_cv_draw_points( - utils.reverse_points(points) + sorted_points ) cv2.fillPoly( seg_mask_image, @@ -2323,9 +2329,11 @@ def __export_index_color_image( else: # Reverse hollow points for opencv because these points are # counterclockwise - cv_draw_points = self.__get_cv_draw_points( - utils.reverse_points(points) + reverse_points = utils.reverse_points(points) + sorted_points = utils.sort_segmentation_points( + reverse_points ) + cv_draw_points = self.__get_cv_draw_points(sorted_points) cv2.fillPoly( seg_mask_image, [cv_draw_points], @@ -2362,7 +2370,8 @@ def __export_index_color_image( def __get_cv_draw_points(self, points: List[int]) -> List[int]: """ - Convert points to pillow draw points. Diagonal points are not supported. + Convert points to pillow draw points. Diagonal points are not supported + Annotation clockwise draw. """ x_points = [] x_points.append(points[0]) diff --git a/fastlabel/utils.py b/fastlabel/utils.py index 3ba7d45..ca8d209 100644 --- a/fastlabel/utils.py +++ b/fastlabel/utils.py @@ -85,6 +85,31 @@ def reverse_points(points: List[int]) -> List[int]: return reversed_points +def sort_segmentation_points(points: List[int]) -> List[int]: + """ + e.g.) + [1, 2, 1, 1, 2, 1, 2, 2, 1, 2] => [1, 1, 2, 1, 2, 2, 1, 2, 1, 1] + """ + points_array = np.array(points).reshape((-1, 2))[1:] + base_point_index = 0 + points_list = points_array.tolist() + for index, val in enumerate(points_list): + if index == 0: + continue + if ( + val[1] <= points_list[base_point_index][1] and val[0] <= points_list[base_point_index][0] + ): + base_point_index = index + new_points_array = np.vstack( + [ + points_array[base_point_index:], + points_array[:base_point_index], + np.array([points_array[base_point_index]]), + ] + ) + return new_points_array.ravel().tolist() + + def is_clockwise(points: list) -> bool: """ points: [x1, y1, x2, y2, x3, y3, ... xn, yn]