Skip to content

Commit 68846b8

Browse files
Merge pull request #146 from fastlabel/143_add_method_export
add method export segmentation
2 parents 20f843f + d435e45 commit 68846b8

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

fastlabel/__init__.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,7 @@ def export_instance_segmentation(
24682468
tasks: list,
24692469
output_dir: str = os.path.join("output", "instance_segmentation"),
24702470
pallete: List[int] = const.COLOR_PALETTE,
2471+
start_index: int = 1,
24712472
) -> None:
24722473
"""
24732474
Convert tasks to index color instance segmentation (PNG files).
@@ -2478,6 +2479,10 @@ def export_instance_segmentation(
24782479
tasks is a list of tasks (Required).
24792480
output_dir is output directory(default: output/instance_segmentation)(Optional).
24802481
pallete is color palette of index color. Ex: [255, 0, 0, ...] (Optional).
2482+
start_index is the first index of color index corresponding to color pallete.
2483+
The expected values for start_index are either 0 or 1.
2484+
When start_index is 0, all pixels are assumed to have annotations
2485+
because they become the same color as the background(Optional).
24812486
"""
24822487
tasks = converters.to_pixel_coordinates(tasks)
24832488
for task in tasks:
@@ -2486,13 +2491,16 @@ def export_instance_segmentation(
24862491
output_dir=output_dir,
24872492
pallete=pallete,
24882493
is_instance_segmentation=True,
2494+
start_index=start_index,
24892495
)
24902496

24912497
def export_semantic_segmentation(
24922498
self,
24932499
tasks: list,
24942500
output_dir: str = os.path.join("output", "semantic_segmentation"),
24952501
pallete: List[int] = const.COLOR_PALETTE,
2502+
classes: List = [],
2503+
start_index: int = 1,
24962504
) -> None:
24972505
"""
24982506
Convert tasks to index color semantic segmentation (PNG files).
@@ -2502,13 +2510,19 @@ def export_semantic_segmentation(
25022510
tasks is a list of tasks (Required).
25032511
output_dir is output directory(default: output/semantic_segmentation)(Optional).
25042512
pallete is color palette of index color. Ex: [255, 0, 0, ...] (Optional).
2505-
"""
2506-
classes = []
2507-
for task in tasks:
2508-
for annotation in task["annotations"]:
2509-
classes.append(annotation["value"])
2510-
classes = list(set(classes))
2511-
classes.sort()
2513+
classes is a list of annotation values.
2514+
This list defines the value order that corresponds to the color index of the annotation.(Optional).
2515+
start_index is the first index of color index corresponding to color pallete.
2516+
The expected values for start_index are either 0 or 1.
2517+
When start_index is 0, all pixels are assumed to have annotations
2518+
because they become the same color as the background(Optional).
2519+
"""
2520+
if len(classes) == 0:
2521+
for task in tasks:
2522+
for annotation in task["annotations"]:
2523+
classes.append(annotation["value"])
2524+
classes = list(set(classes))
2525+
classes.sort()
25122526

25132527
tasks = converters.to_pixel_coordinates(tasks)
25142528
for task in tasks:
@@ -2518,6 +2532,7 @@ def export_semantic_segmentation(
25182532
pallete=pallete,
25192533
is_instance_segmentation=False,
25202534
classes=classes,
2535+
start_index=start_index,
25212536
)
25222537

25232538
def __export_index_color_image(
@@ -2527,12 +2542,13 @@ def __export_index_color_image(
25272542
pallete: List[int],
25282543
is_instance_segmentation: bool = True,
25292544
classes: list = [],
2545+
start_index: int = 1,
25302546
) -> None:
25312547
image = Image.new("RGB", (task["width"], task["height"]), 0)
25322548
image = np.array(image)
25332549
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
25342550

2535-
index = 1
2551+
index = start_index
25362552
# In case segmentation, to avoid hollowed points overwrite other segmentation
25372553
# in them, segmentation rendering process is different from
25382554
# other annotation type
@@ -2541,7 +2557,7 @@ def __export_index_color_image(
25412557
color = (
25422558
index
25432559
if is_instance_segmentation
2544-
else classes.index(annotation["value"]) + 1
2560+
else classes.index(annotation["value"]) + start_index
25452561
)
25462562
if annotation["type"] == AnnotationType.segmentation.value:
25472563
# Create each annotation's masks and merge them finally

0 commit comments

Comments
 (0)