Skip to content

Commit ba33581

Browse files
authored
Merge pull request #87 from fastlabel/develop
Merge to main
2 parents 371d20c + 03d28c5 commit ba33581

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

fastlabel/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

fastlabel/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import base64
3+
import numpy as np
4+
import geojson
35
from typing import List
46

57

@@ -41,3 +43,26 @@ def reverse_points(points: List[int]) -> List[int]:
4143
reversed_points.insert(
4244
0, points[index])
4345
return reversed_points
46+
47+
def is_clockwise(points: list) -> bool:
48+
"""
49+
points: [x1, y1, x2, y2, x3, y3, ... xn, yn]
50+
Sum over the edges, (x2 − x1)(y2 + y1).
51+
If the result is positive the curve is clockwise, if it's negative the curve is counter-clockwise.
52+
53+
The above is assumes a normal Cartesian coordinate system.
54+
HTML5 canvas, use an inverted Y-axis.
55+
Therefore If the area is negative, the curve is clockwise.
56+
"""
57+
points_splitted = [points[idx:idx + 2]
58+
for idx in range(0, len(points), 2)]
59+
polygon_geo = geojson.Polygon(points_splitted)
60+
coords = np.array(list(geojson.utils.coords(polygon_geo)))
61+
xs, ys = map(list, zip(*coords))
62+
xs.append(xs[0])
63+
ys.append(ys[0])
64+
sum_edges = sum((xs[i] - xs[i - 1]) * (ys[i] + ys[i - 1]) for i in range(1, len(points_splitted))) / 2.0
65+
66+
if sum_edges < 0:
67+
return True
68+
return False

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="fastlabel",
11-
version="0.11.8",
11+
version="0.11.9",
1212
author="eisuke-ueta",
1313
author_email="eisuke.ueta@fastlabel.ai",
1414
description="The official Python SDK for FastLabel API, the Data Platform for AI",

0 commit comments

Comments
 (0)