A newer version of the Gradio SDK is available:
6.2.0
metadata
title: NeuroSAM 3
emoji: π₯
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 6.1.0
app_file: app.py
pinned: true
license: apache-2.0
NeuroSAM 3: Medical Image Segmentation
A comprehensive medical image segmentation application using SAM 3 (Segment Anything Model 3) for DICOM file analysis with advanced features.
β¨ Features
Core Features
- π§ SAM 3 Integration: Uses the latest Segment Anything Model 3 for medical image segmentation
- π DICOM Support: Process CT and MRI DICOM files
- πΌοΈ Image Formats: Supports DICOM (.dcm), PNG, and JPG files
- π― Text Prompts: Describe what you want to segment (e.g., "brain", "tumor", "skull")
- βοΈ Windowing Strategies: Optimized windowing presets for CT images
Interactive Features
- π Interactive Slice Viewer: Scroll through multiple slices with auto-play
- π Subject Detection: Automatically groups images by patient/subject ID
- βΆοΈ Auto-play: Automatically animate through slices
- π Ground Truth Comparison: Compare SAM 3 results with expert annotations (Dice score, IoU)
Enhanced Features (NEW!)
- β¨ Enhanced Single Image: Brightness/contrast controls, colormap options, transparency adjustment
- β¨ Enhanced Batch Processing: Process multiple images with ZIP download
- π― Point/Box Prompts: Interactive point-click and bounding box-based segmentation
- π ROI Statistics: Detailed statistics (area, intensity, centroid, bounding box)
- π₯ NIFTI Export: Export segmentation masks to NIFTI format for medical imaging software
- πΎ Annotation Save/Load: Save and reload annotations for later use
- π Multi-Mask Output: Generate multiple mask candidates with confidence scores
Usage
Single Image Processing
- Upload a DICOM (.dcm), PNG, or JPG file
- Enter a text prompt describing what to segment
- Select the imaging modality (CT or MRI)
- Choose the windowing strategy (for CT images)
- Click "Segment Structure" to process
Interactive Slice Viewer
- Upload multiple slices/images from the same subject
- Click "π Detect Subjects" to auto-group by patient ID
- Select a subject from the dropdown
- Click "Process All Slices"
- Use the slider, navigation buttons, or βΆοΈ Auto-play to scroll through slices
Enhanced Single Image (with Image Adjustments)
- Upload an image
- Adjust brightness and contrast sliders
- Choose colormap and transparency for mask overlay
- Click "Segment with Progress"
- Download result using the download button
Point/Box Prompts
- Upload an image
- Point Prompt: Enter X, Y coordinates and click "Segment at Point"
- Box Prompt: Enter bounding box coordinates (X1, Y1, X2, Y2) and click "Segment in Box"
ROI Statistics & Export
- Process an image using "Process & Calculate Statistics"
- View detailed ROI statistics (area, intensity, centroid)
- Export to NIFTI format for use in medical imaging software
- Save annotation for later use
- Load previously saved annotations
Multi-Mask Output
- Upload an image and enter prompt
- Choose number of masks to generate (1-5)
- Click "Generate Multiple Masks"
- Compare mask candidates with confidence scores
Compare with Ground Truth
- Upload a medical image and its ground truth mask
- Enter segmentation prompt
- Click "Compare Segmentation" to see metrics (Dice score, IoU)
Requirements
- Python 3.8+
- PyTorch
- Gradio
- PyDICOM
- Transformers (SAM 3 requires latest version from GitHub)
- nibabel (for NIFTI export)
- scipy (for ROI statistics)
Installation
To install the required dependencies:
pip install -r requirements.txt
Note: SAM 3 is only available in the development version of transformers. The requirements.txt file automatically installs it from the GitHub repository.
Model
This app uses the SAM 3 model from Facebook/Meta. You need:
- A Hugging Face account
- Access token with read permissions for the SAM 3 model (get it from https://huggingface.co/settings/tokens)
- Set
HF_TOKENenvironment variable in Space settings
Public Datasets
- UniqueData/dicom-brain-dataset (Hugging Face) - MRI Brain scans
- The Cancer Imaging Archive (TCIA) - Various medical imaging
- Imaging Data Commons (IDC) - Large collection of DICOM files
- Kaggle Brain MRI datasets - PNG/JPG format images
Tabs Overview
| Tab | Description |
|---|---|
| Single Image | Basic single image segmentation |
| Interactive Slice Viewer | Browse through multiple slices with auto-play |
| Gallery View | View all segmentations in a grid |
| Compare with Ground Truth | Evaluate with Dice/IoU metrics |
| β¨ Enhanced Single Image | Advanced controls with download |
| β¨ Enhanced Batch Processing | Batch processing with ZIP export |
| π― Point/Box Prompts | Interactive region selection |
| π ROI Statistics & Export | Statistics + NIFTI/annotation export |
| π Multi-Mask Output | Multiple mask candidates |
| π¬ Automatic Mask Generator | AMG - segment entire image without prompts |
| π§ Advanced Transforms | ResizeLongestSide + CLAHE enhancement |
| π Edge-Based Segmentation | Sobel edge detection (no AI required) |
SAM-Medical-Imaging Utilities
Inspired by amine0110/SAM-Medical-Imaging, we've integrated several utilities:
- Automatic Mask Generator (AMG): Automatically segment all regions in an image using multiple prompts
- ResizeLongestSide Transform: Optimal image resizing that maintains aspect ratio
- CLAHE-like Enhancement: Contrast enhancement for low-visibility images
- Edge-Based Segmentation: Classical computer vision approach using Sobel gradients
License
Apache 2.0