Skip to content

Commit b75ab58

Browse files
Implement CLI for interactive instance segmentation
1 parent 432ecc2 commit b75ab58

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

micro_sam/sam_annotator/interactive_instance_segmentation.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from magicgui import magicgui
55

66
from .annotator_2d import _get_shape
7+
from .util import _initialize_parser
78
from ..import util
89
from ..import segment_instances
910
from ..visualization import project_embeddings_for_visualization
@@ -71,3 +72,20 @@ def interactive_instance_segmentation(
7172
v.window.add_dock_widget(autosegment_widget)
7273

7374
napari.run()
75+
76+
77+
def main():
78+
parser = _initialize_parser(
79+
description="Run the automatic instance segmentation in interactive mode"
80+
"to determine the optimal segmentation parameters.",
81+
with_segmentation_result=False, with_show_embeddings=False,
82+
)
83+
parser.add_argument(
84+
"-c", "--checkpoint", default=None,
85+
help="Path to alternative checkpoint instead of the standard model."
86+
)
87+
args = parser.parse_args()
88+
raw = util.load_image_data(args.input, ndim=2, key=args.key)
89+
interactive_instance_segmentation(
90+
raw, args.embedding_path, args.model_type, args.tile_shape, args.halo, args.checkpoint
91+
)

micro_sam/sam_annotator/util.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def toggle_label(prompts):
323323
prompts.refresh_colors()
324324

325325

326-
def _initialize_parser(description, with_segmentation_result=True):
326+
def _initialize_parser(description, with_segmentation_result=True, with_show_embeddings=True):
327327
parser = argparse.ArgumentParser(description=description)
328328

329329
parser.add_argument(
@@ -336,6 +336,7 @@ def _initialize_parser(description, with_segmentation_result=True):
336336
help="The key for opening data with elf.io.open_file. This is the internal path for a hdf5 or zarr container, "
337337
"for a image series it is a wild-card, e.g. '*.png' and for mrc it is 'data'."
338338
)
339+
339340
parser.add_argument(
340341
"-e", "--embedding_path",
341342
help="The filepath for saving/loading the pre-computed image embeddings. "
@@ -356,10 +357,11 @@ def _initialize_parser(description, with_segmentation_result=True):
356357
help="The key for opening the segmentation data. Same rules as for 'key' apply."
357358
)
358359

359-
parser.add_argument(
360-
"--show_embeddings", action="store_true",
361-
help="Visualize the embeddings computed by SegmentAnything. This can be helpful for debugging."
362-
)
360+
if with_show_embeddings:
361+
parser.add_argument(
362+
"--show_embeddings", action="store_true",
363+
help="Visualize the embeddings computed by SegmentAnything. This can be helpful for debugging."
364+
)
363365
parser.add_argument(
364366
"--model_type", default="vit_h", help="The segment anything model that will be used, one of vit_h,l,b."
365367
)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"micro_sam.annotator_3d = micro_sam.sam_annotator.annotator_3d:main",
1818
"micro_sam.annotator_tracking = micro_sam.sam_annotator.annotator_tracking:main",
1919
"micro_sam.image_series_annotator = micro_sam.sam_annotator.image_series_annotator:main",
20+
"micro_sam.interactive_instance_segmentation = micro_sam.sam_annotator.interactive_instance_segmentation:main",
2021
"micro_sam.precompute_embeddings = micro_sam.util:main",
2122
]
2223
}

0 commit comments

Comments
 (0)