Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions fastlabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
Expand Down Expand Up @@ -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])
Expand Down
25 changes: 25 additions & 0 deletions fastlabel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down