55from napari import Viewer
66
77from .. import util
8+ from .. import segment_instances
89from ..visualization import project_embeddings_for_visualization
9- from ..segment_instances import segment_from_embeddings
1010from ..segment_from_prompts import segment_from_points
1111from .util import (
1212 commit_segmentation_widget , create_prompt_menu , prompt_layer_to_points , toggle_label , LABEL_COLOR_CYCLE
@@ -21,20 +21,26 @@ def segment_wigdet(v: Viewer):
2121 v .layers ["current_object" ].refresh ()
2222
2323
24- # TODO enable choosing setting the segmentation method and setting other params
25- @magicgui (call_button = "Segment All Objects" )
26- def autosegment_widget (v : Viewer ):
27- # choose if we segment with/without tiling based on the image shape
28- seg = segment_from_embeddings (PREDICTOR , IMAGE_EMBEDDINGS )
24+ # TODO expose more parameters
25+ @magicgui (call_button = "Segment All Objects" , method = {"choices" : ["default" , "sam" , "embeddings" ]})
26+ def autosegment_widget (v : Viewer , method : str = "default" ):
27+ if method in ("default" , "sam" ):
28+ print ("Run automatic segmentation with SAM. This can take a few minutes ..." )
29+ image = v .layers ["raw" ].data
30+ seg = segment_instances .segment_instances_sam (SAM , image )
31+ elif method == "embeddings" :
32+ seg = segment_instances .segment_instances_from_embeddings (PREDICTOR , IMAGE_EMBEDDINGS )
33+ else :
34+ raise ValueError
2935 v .layers ["auto_segmentation" ].data = seg
3036 v .layers ["auto_segmentation" ].refresh ()
3137
3238
3339def annotator_2d (raw , embedding_path = None , show_embeddings = False , segmentation_result = None ):
3440 # for access to the predictor and the image embeddings in the widgets
35- global PREDICTOR , IMAGE_EMBEDDINGS
41+ global PREDICTOR , IMAGE_EMBEDDINGS , SAM
3642
37- PREDICTOR = util .get_sam_model ()
43+ PREDICTOR , SAM = util .get_sam_model (return_sam = True )
3844 IMAGE_EMBEDDINGS = util .precompute_image_embeddings (PREDICTOR , raw , save_path = embedding_path , ndim = 2 )
3945 util .set_precomputed (PREDICTOR , IMAGE_EMBEDDINGS )
4046
0 commit comments