From 6468e229875631998dfde9947bfbdeb43b8deedd Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Thu, 2 Feb 2023 19:44:19 +0900 Subject: [PATCH 1/2] fix mask image logic --- fastlabel/__init__.py | 19 ++++++++++++++----- fastlabel/utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/fastlabel/__init__.py b/fastlabel/__init__.py index 4b7794a..4416eb5 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.sorted_segmentation_points(points) + ) else: + reverse_points = utils.reverse_points(points) + sorted_points = utils.sorted_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.sorted_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..4fc3060 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 sorted_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] From ad489067e330999a6da59a2cacdcb4335ef7b511 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Thu, 2 Feb 2023 20:16:39 +0900 Subject: [PATCH 2/2] rename --- fastlabel/__init__.py | 6 +++--- fastlabel/utils.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fastlabel/__init__.py b/fastlabel/__init__.py index 4416eb5..f071e16 100644 --- a/fastlabel/__init__.py +++ b/fastlabel/__init__.py @@ -2309,11 +2309,11 @@ def __export_index_color_image( cv_draw_points = [] if utils.is_clockwise(points): cv_draw_points = self.__get_cv_draw_points( - utils.sorted_segmentation_points(points) + utils.sort_segmentation_points(points) ) else: reverse_points = utils.reverse_points(points) - sorted_points = utils.sorted_segmentation_points( + sorted_points = utils.sort_segmentation_points( reverse_points ) cv_draw_points = self.__get_cv_draw_points( @@ -2330,7 +2330,7 @@ def __export_index_color_image( # Reverse hollow points for opencv because these points are # counterclockwise reverse_points = utils.reverse_points(points) - sorted_points = utils.sorted_segmentation_points( + sorted_points = utils.sort_segmentation_points( reverse_points ) cv_draw_points = self.__get_cv_draw_points(sorted_points) diff --git a/fastlabel/utils.py b/fastlabel/utils.py index 4fc3060..ca8d209 100644 --- a/fastlabel/utils.py +++ b/fastlabel/utils.py @@ -85,7 +85,7 @@ def reverse_points(points: List[int]) -> List[int]: return reversed_points -def sorted_segmentation_points(points: List[int]) -> List[int]: +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]