A GUI application for image segmentation using the SAM2 (Segment Anything Model 2) model, implemented in Python using PyQt. This tool allows you to upload images, annotate them with points and lines as prompts, and generate masks using the SAM2 model.
- Upload Images: Supports common image formats (PNG, JPG, JPEG, BMP).
- Interactive Annotation:
- Add foreground points (
Ctrl + Click). - Add background points (
Shift + Click). - Draw lines as prompts.
- Add foreground points (
- Mask Generation: Generate segmentation masks using the SAM2 model.
- Real-time Mask Updates: Masks update in real-time as you add annotations.
- Undo/Redo Functionality: Supports undoing and redoing of actions.
- Multiple Model Sizes: Choose between Tiny, Small, Base+, and Large SAM2 models.
- Zooming and Panning: Use the mouse wheel to zoom in/out.
- Python 3.7 or higher
- PyTorch (with CUDA support for GPU acceleration, optional)
- PyQt5 or PyQt6
- NumPy
- Pillow (PIL)
- scikit-image
- SAM2 modules (from the
sam2package)
git clone https://github.com/waqashussain117/sam2-gui.git
cd sam2-guiIt's recommended to use a virtual environment to manage dependencies.
# Create a virtual environment named 'venv'
python -m venv venv
# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activateInstall the required Python packages using pip:
pip install -r requirements.txtIf you don't have a requirements.txt file, you can install the dependencies manually:
pip install numpy pillow scikit-image
pip install torch torchvision
pip install PyQt5 # or PyQt6 if you preferNote: If you intend to use GPU acceleration, ensure that PyTorch is installed with the appropriate CUDA version. Visit PyTorch's official website for installation instructions tailored to your system.
Depending on your preference, install PyQt5 or PyQt6:
# For PyQt5
pip install PyQt5
# For PyQt6
pip install PyQt6Download the SAM2 model checkpoints and configuration files:
- Tiny Model:
- Checkpoint:
sam2.1_hiera_tiny.pt - Config:
sam2.1_hiera_t.yaml
- Checkpoint:
- Small Model:
- Checkpoint:
sam2.1_hiera_small.pt - Config:
sam2.1_hiera_s.yaml
- Checkpoint:
- Base+ Model (Default):
- Checkpoint:
sam2.1_hiera_base_plus.pt - Config:
sam2.1_hiera_b+.yaml
- Checkpoint:
- Large Model:
- Checkpoint:
sam2.1_hiera_large.pt - Config:
sam2.1_hiera_l.yaml
- Checkpoint:
Directory Structure:
- Place the checkpoint files in the
checkpoints/directory. - Place the config files in the
sam2/configs/sam2.1/directory.
Ensure that the directory structure matches the paths used in the code. If these directories don't exist, create them.
The application depends on the sam2 package. Ensure that:
- The
sam2package is installed in your Python environment. - OR the
sam2directory (containing__init__.py,build_sam.py, etc.) is located in the same directory as your script or is accessible via your Python path.
Ensure all required packages are installed:
numpytorchPillow(PIL)PyQt5orPyQt6scikit-image
python app.py- Upload Image: Click to select and upload an image.
- Draw Line: Toggle line drawing mode to draw lines as prompts.
- Remove Point: Remove selected points from the image.
- Remove Line: Remove selected lines from the image.
- Generate Mask: Generate segmentation masks based on the current annotations.
- Remove Mask: Remove generated masks from the image.
- Undo/Redo: Undo or redo the last action.
- Clear Annotations: Remove all annotations (points, lines, masks) from the image.
- Model Type: Select the SAM2 model type (Tiny, Small, Base+, Large).
- Batch Size: Adjust the batch size for mask generation.
- Enable Multimask Output: Toggle between single and multiple mask outputs.
- Instructions: Displays helpful tips for using the application.
- Add Foreground Point: Hold
Ctrland click on the desired location in the image. - Add Background Point: Hold
Shiftand click on the desired location in the image. - Draw Line:
- Click the
Draw Linebutton to enable line drawing mode. - Click on the image to set the start point of the line.
- Click again to set the end point of the line.
- Click the
- Zoom In/Out: Use the mouse wheel to zoom in and out.
- Right-click on a Point: Opens a context menu to:
- Delete Point: Remove the point from the image.
- Toggle Point Label: Change the point from foreground to background or vice versa.
After adding annotations (points and/or lines):
- Click on the
Generate Maskbutton. - The application will process the image and display the generated mask(s) over the image.
- Masks can be removed using the
Remove Maskbutton.
- The application supports real-time mask updates as you add or modify annotations.
- This feature is automatically triggered when you add or remove points and lines.
- Model Files Not Found: Ensure that the SAM2 model checkpoint and config files are placed in the correct directories (
checkpoints/andsam2/configs/sam2.1/). - Dependencies Errors: Verify that all required Python packages are installed in your environment.
- GPU Support:
- If you have a CUDA-compatible GPU, ensure that PyTorch is installed with CUDA support.
- On macOS with M1/M2 chips, the application will attempt to use the Metal Performance Shaders (MPS) backend.
- If no GPU is available, the application will default to CPU mode, which may be slower.
- Pillow Version:
- Ensure you have a recent version of Pillow installed.
- The application uses
Image.Resampling.LANCZOSfor image resizing. - If you encounter issues, try updating Pillow:
pip install --upgrade Pillow
- SAM2: This application utilizes the Segment Anything Model 2 (SAM2) for image segmentation.
- PyQt: The GUI is built using PyQt.
This project is licensed under the MIT License - see the LICENSE file for details.