AiSegmentation
High-level interface for AI-powered image segmentation. The AiSegmentation class enables users to perform automated segmentation of images using AI models, including TotalSegmentator, nnU-Net, MONAI, and Cellpose-SAM. It provides methods to select and configure models, install required dependencies, check installation status, and run segmentation on one or more image volumes. Results are returned as new mask objects that can be used in further analysis or visualization. Key features: - Choose between multiple AI segmentation models (TotalSegmentator, nnU-Net, MONAI, Cellpose-SAM). - Install and verify model environments and dependencies with a single call. - Run segmentation on one or more volumes by name, with flexible parameter options. - Retrieve available tasks, default parameters, and model information for scripting convenience. - Integrate seamlessly into automated workflows and batch processing scripts.
Import
import ScriptingApi as api
# Access the class
# AiSegmentation is available via api.AiSegmentation
Methods
Properties
set_model_type
Sets the active AI segmentation model type.
Signature:
set_model_type(modelType: AiSegmentationModelType) -> None
Parameters:
| Parameter | Type | Description |
|---|---|---|
modelType | any | The AI model to use for subsequent operations. Use api.AiSegmentationModelType.TotalSegmentator, api.AiSegmentationModelType.NnUnet, api.AiSegmentationModelType.Monai, or api.AiSegmentationModelType.Cellpose. |
get_model_type
Gets the currently active AI segmentation model type.
Signature:
get_model_type() -> AiSegmentationModelType
Returns: AiSegmentationModelType — The currently selected AI model type as AiSegmentationModelType.
set_model_type_str
Sets the active AI segmentation model type using a string representation. Allows setting the model type using a string like "TotalSegmentator", "NnUnet", or "Monai".
Signature:
set_model_type_str(modelType: str) -> None
Parameters:
| Parameter | Type | Description |
|---|---|---|
modelType | any | A string specifying the model type to set. |
get_model_type_str
Gets the currently active AI segmentation model type as a string.
Signature:
get_model_type_str() -> str
Returns: str — The currently selected AI model type as a string.
get_model_name
Gets the display name of the current AI model.
Signature:
get_model_name() -> str
Returns: str — A string representing the human-readable name of the current model.
get_installation_status
Checks if the AI segmentation model is installed and ready for use.
Signature:
get_installation_status() -> bool
Returns: bool — True if the model is installed and ready, false otherwise.
set_models_dir_path
Sets the models directory path for nnU-Net and MONAI models. This path should contain the trained model files and configurations.
Signature:
set_models_dir_path(modelsDirPath: str) -> None
Parameters:
| Parameter | Type | Description |
|---|---|---|
modelsDirPath | any | The absolute path to the trained models directory. |
get_models_dir_path
Gets the currently configured trained models directory path.
Signature:
get_models_dir_path() -> str
Returns: str — The path to the trained models directory, or empty string if not set.
get_available_tasks
Gets the list of available tasks for the current model. For TotalSegmentator: returns available task names (e.g., "total", "body"). For nnU-Net: returns available dataset names. For MONAI: returns available bundle names. For Cellpose-SAM: returns the available segmentation modes ("2D", "3D").
Signature:
get_available_tasks() -> list
Returns: list — List of available task/dataset/bundle/mode names for the current model.
get_default_total_segmentator_params
Gets the default parameters for the current model type.
Signature:
get_default_total_segmentator_params() -> TotalSegmentatorParams
Returns: TotalSegmentatorParams — The TotalSegmentatorParams structure with default settings.
get_default_nn_unet_params
Gets the default parameters for nnU-Net.
Signature:
get_default_nn_unet_params() -> NnUnetParams
Returns: NnUnetParams — The NnUnetParams structure with default settings.
get_default_monai_params
Gets the default parameters for MONAI.
Signature:
get_default_monai_params() -> MonaiParams
Returns: MonaiParams — The MonaiParams structure with default settings.
get_default_cellpose_params
Gets the default parameters for Cellpose-SAM.
Signature:
get_default_cellpose_params() -> CellposeParams
Returns: CellposeParams — The CellposeParams structure with default settings.
General
install_model
Installs the current AI model and its dependencies. This operation may take several minutes as it downloads and configures the Python environment and required packages.
Signature:
install_model(showMessages: bool = True) -> bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
showMessages | any | If true, displays installation progress messages in the UI. |
Returns: bool — True if the installation succeeded, false if there was an error.
run_total_segmentator
Runs AI segmentation on specified volume objects using TotalSegmentator. The volumes must exist in the current project and be accessible by name.
Signature:
run_total_segmentator(volumeNames: list, params: TotalSegmentatorParams, multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = []) -> list
Parameters:
| Parameter | Type | Description |
|---|---|---|
volumeNames | any | List of volume object names to segment. |
params | any | TotalSegmentator-specific configuration parameters. Create using params = api.TotalSegmentatorParams(). |
multilabelOutput | any | If true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks. |
transferAllLabels | any | If true, transfers all available labels to output; otherwise, only selected labels. |
selectedLabels | any | List of specific label names to include when transferAllLabels is false. |
Returns: list — List of generated mask object names on success, empty list on failure.
run_nn_unet
Runs AI segmentation on specified volume objects using nnU-Net. The volumes must exist in the current project and be accessible by name.
Signature:
run_nn_unet(volumeNames: list, params: NnUnetParams, multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = []) -> list
Parameters:
| Parameter | Type | Description |
|---|---|---|
volumeNames | any | List of volume object names to segment. |
params | any | nnU-Net-specific configuration parameters. Create using params = api.NnUnetParams(). |
multilabelOutput | any | If true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks. |
transferAllLabels | any | If true, transfers all available labels to output; otherwise, only selected labels. |
selectedLabels | any | List of specific label names to include when transferAllLabels is false. |
Returns: list — List of generated mask object names on success, empty list on failure.
run_monai
Runs AI segmentation on specified volume objects using MONAI. The volumes must exist in the current project and be accessible by name.
Signature:
run_monai(volumeNames: list, params: MonaiParams, multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = []) -> list
Parameters:
| Parameter | Type | Description |
|---|---|---|
volumeNames | any | List of volume object names to segment. |
params | any | MONAI-specific configuration parameters. Create using params = api.MonaiParams(). |
multilabelOutput | any | If true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks. |
transferAllLabels | any | If true, transfers all available labels to output; otherwise, only selected labels. |
selectedLabels | any | List of specific label names to include when transferAllLabels is false. |
Returns: list — List of generated mask object names on success, empty list on failure.
run_cellpose
Runs AI segmentation on specified volume objects using Cellpose-SAM. Cellpose-SAM provides general-purpose 2D and 3D cell/object segmentation. In 2D mode, it segments the currently active slice and merges the result into the active mask. In 3D mode, it segments the entire volume.
Signature:
run_cellpose(volumeNames: list, params: CellposeParams, multilabelOutput: bool = True) -> list
Parameters:
| Parameter | Type | Description |
|---|---|---|
volumeNames | any | List of volume object names to segment. |
params | any | Cellpose-SAM-specific configuration parameters. Create using params = api.CellposeParams(). |
multilabelOutput | any | If true, all detected instances are packed into a single multilabel mask (each object gets a unique integer label); if false, each detected instance is returned as a separate single-label mask object. |
Returns: list — List of generated mask object names on success, empty list on failure.