Skip to content

Commit d104d5e

Browse files
fix mask parse logic
1 parent 7846165 commit d104d5e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

fastlabel/utils/mask_image_util.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ def mask_to_segmentation(
4444
points = [
4545
__convert_fastlabel_segmentation(segmentation)
4646
for segmentation in segmentation_list
47+
if len(segmentation) > 0
4748
]
4849
return points
4950

5051

5152
def __detect_segmentation_list(
5253
hierarchy: np.ndarray, contours: Sequence[Union[np.ndarray, Mat]]
5354
) -> list[Any]:
55+
5456
outer_polygon_hierarchy_indexes = list(
5557
filter(lambda x: hierarchy[0][x][3] == -1, range(hierarchy[0].shape[0]))
5658
)
@@ -126,10 +128,15 @@ def __convert_fastlabel_segmentation(segmentation: list) -> list[Any]:
126128

127129

128130
# TODO: maybe return points number is not correct. (ex. only 2 points)
129-
def __convert_fastlabel_points(polygon_array: np.ndarray) -> list[int]:
131+
def __convert_fastlabel_points(cv2_polygon_array: np.ndarray) -> list[int]:
130132
# Notices
131133
# polygon_array → anticlockwise coordinates
132134
# return value → clockwise coordinates
135+
polygon_array = (
136+
np.append(cv2_polygon_array, [cv2_polygon_array[0]], axis=0)
137+
if not np.array_equal(cv2_polygon_array[0], cv2_polygon_array[-1])
138+
else cv2_polygon_array
139+
)
133140
x_array = polygon_array[:, 0]
134141
y_array = polygon_array[:, 1]
135142
# Rectangle with height of 1px
@@ -289,4 +296,10 @@ def __convert_fastlabel_points(polygon_array: np.ndarray) -> list[int]:
289296
) and (x_next_value == x_current_value and y_next_value > y_current_value):
290297
new_array.append([x_current_value + 1, y_current_value])
291298
continue
292-
return np.flipud(np.array(new_array)).flatten().tolist()
299+
new_point_array = np.flipud(np.array(new_array)).flatten().tolist()
300+
if not (
301+
(new_point_array[0] == new_point_array[-2])
302+
and (new_point_array[1] == new_point_array[-1])
303+
):
304+
new_point_array.extend([new_point_array[0], new_point_array[1]])
305+
return new_point_array

0 commit comments

Comments
 (0)