This is a small tool and Marimo notebook to segment image with SAM2.
uv run python SegmentWithSAM2.py [-h] -i INPUT_DIR -o OUTPUT_DIR [--mask_in_rgb] [--post_process] [--class_seg CLASS_SEG]
options:
-h, --help show this help message and exit
-i INPUT_DIR, --input_dir INPUT_DIR
Input directory of images to segments
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
Output directory of segmentation
--mask_in_rgb segmentation masks store in rgb file
--post_process Add post processing after segmentation prediction (opening -> closing).
--class_seg CLASS_SEG
Classes to segments (default: ['nuc', 'cyto'])
Example of usage: uv run SegmentWithSAM2.py -i ./PATCHS/GREY/ -o ./PATCHS/MASK/ --mask_in_rgb --class_seg nuclei,cytoplasm
uv run marimo edit app_marimo_sam2.py
Or to use it as an app: uv run marimo run app_marimo_sam2.py
Important
Ensure you have UV installed (using the command: uv --version ) and the Git version control software. If not, install UV and/or Git.
Important
You need to have Segment Anything Model 2 (SAM2) installed. If not, proceed with the installation of SAM2. Or check github.com/facebookresearch/sam2 for the installation.
git clone https://github.com/facebookresearch/sam2.git && cd sam2
cd checkpoints && \
./download_ckpts.sh && \
cd ..
- Clone this repository

git clone .....
- Install the UV environment
uv sync
2025 - 🐙