A general-purpose image annotation tool based on Segment Anything Model (SAM)
Project description
SAM Annotator
A nice and simple tool for semi-automatic image annotation based on Meta AI's Segment Anything Model (SAM).
Features
- Semi-Automatic Segmentation: Use SAM (v1 or v2) to generate high-quality masks with minimal interaction
- Multiple Annotation Methods: Box-based and point-based annotation methods
- 1000+ Classes Support: Manage up to 1000+ classes with scrollable UI and full keyboard navigation
- Class Management: Assign classes to segmented objects with intuitive keyboard shortcuts
- Auto-Advance Workflow: Automatically move to the next image after saving annotations (toggle with 'u')
- Export Options: Export annotations to COCO, YOLO, or Pascal VOC formats
- Undo/Redo: Robust command system for undoing and redoing annotations
- Visualization: Real-time visualization of annotations with adjustable opacity
- Dual SAM Support: Works with both SAM1 (segment-anything) and SAM2 (ultralytics)
Tool Overview
Installation
Prerequisites
- Python 3.8+
- PyTorch 1.10+
- OpenCV
- NumPy
- CUDA-compatible GPU (recommended for optimal performance)
Installation via pip
# Install the stable version from PyPI
pip install sam-annotator
# Verify installation
sam_annotator --version
Setup from source
-
Clone the repository:
git clone https://github.com/pavodi-nm/sam_annotator.git cd sam_annotator
-
Install dependencies:
pip install -r requirements.txt
Or install directly:
pip install -e .
-
Download SAM model weights (optional):
- For SAM version 1: Download SAM ViT-H Model
- For SAM version 2: Download SAM 2 Model
Usage
Running the Application
python main.py --sam_version [sam1|sam2] --model_type [vit_h|vit_l|vit_b|tiny|small|base|large|tiny_v2|small_v2|base_v2|large_v2] --category_path <path_to_category> --classes_csv <path_to_classes_csv> [--checkpoint <path_to_model_checkpoint>]
Example:
python main.py --sam_version sam1 --model_type vit_h --category_path test_data/s2 --classes_csv class_map_sam.csv
Or if installed via pip:
sam_annotator --sam_version sam1 --model_type vit_h --category_path /path/to/categories --classes_csv /path/to/classes.csv
For a complete list of command-line arguments and configuration options, see the Configuration Options documentation.
Directory Structure
Create a directory structure for your dataset:
category_path/
├── images/ # Place your images here (mandatory)
├── labels/ # Annotation files will be saved here (optional)
├── masks/ # Visualization of masks will be saved here (optional)
├── metadata/ # Metadata about annotations (optional)
└── exports/ # Exported annotations in various formats (optional)
For more details on how annotations are stored and loaded, see the Loading and Saving Annotations documentation.
Class Definition
Create a CSV file with class definitions:
class_name
background
person
car
...
Annotation Methods
Box-Based Annotation (default)
- Left-click and drag to draw a bounding box around an object
- Release to generate a segmentation mask
- Press 'a' to add the annotation with the current class
- Press 's' to save all the annotations
- Press 'c' to clear all the annotations
Point-Based Annotation
- Press 'w' to switch to point mode
- Left-click to add foreground points (green)
- Press 'a' to add the annotation with the current class
- Press 's' to save all the annotations
- Press 'c' to clear all the annotations
Visualization Tool
SAM Annotator includes a powerful visualization tool for reviewing annotations. This tool allows you to browse through your annotated images, view segmentation masks, and export statistics about your dataset.
Launching the Visualization Tool
To launch the visualization tool, use the --visualization flag:
python main.py --category_path <path_to_category> --visualization [--classes_csv <path_to_classes_csv>] [--export_stats]
Example:
python main.py --category_path test_data/s2 --visualization --classes_csv class_map_sam.csv
Or if installed via pip:
sam_annotator --category_path /path/to/categories --visualization
The tool will automatically look for the classes CSV file in the dataset directory if not specified.
Visualization Features
- Browse Images: Navigate through your dataset using the Previous/Next buttons or keyboard shortcuts
- View Masks: See segmentation masks and their overlays on the original images
- Object Details: View class assignments and object properties
- Statistics: Analyze class distribution and annotation density with visual charts
- Export: Save visualizations of annotated images for documentation
Visualization Controls
| Control | Action |
|---|---|
| Previous/Next buttons | Navigate between images |
| Left/Right arrow keys | Navigate between images |
| 's' key | Save visualization |
| 'm' key | Mark as faulty |
| Opacity slider | Adjust mask transparency |
| Display mode | Switch between overlay, side-by-side, and mask-only views |
When using the --export_stats flag, the tool will generate comprehensive statistics about your dataset, including class distribution, objects per image, and more.
Keyboard Shortcuts
SAM Annotator provides a comprehensive set of keyboard shortcuts to improve your workflow efficiency:
| Key | Action |
|---|---|
| a | Add current mask as annotation |
| s | Save annotations |
| n | Next image |
| p | Previous image |
| j | Jump to specific image number |
| u | Toggle auto-advance after save |
| w | Toggle between annotation modes |
| x | Clear current selection |
| c | Clear all annotations |
| z | Undo last action |
| y | Redo last undone action |
| m | Toggle mask visibility |
| b | Toggle box visibility |
| l | Toggle label visibility |
| r | Toggle annotation review panel |
| e | Enter export mode |
| q | Quit application |
For a complete list of keyboard shortcuts and detailed usage examples, see the Keyboard Shortcuts Documentation
Documentation
- Getting Started
- Keyboard Shortcuts
- Loading and Saving Annotations
- Annotation Formats
- Configuration Options
- Memory Management
- Implementation Details
- API Reference
Windows and Controls
Main Window
- Displays the current image with annotations
- Shows status information and current class
- Includes indicators for current mode and annotation count
Class Window
- Displays available classes for selection
- Click on a class to select it for the next annotation
Review Panel
- Shows list of current annotations
- Allows selection, deletion, and class change of annotations
View Controls
- Adjust mask opacity
- Toggle visibility of masks, boxes, and labels
Exporting Annotations
Press the corresponding key to export annotations in your preferred format:
- 'e' - Export to COCO format
- 'y' - Export to YOLO format
- 'v' - Export to Pascal VOC format
Exports will be saved in the exports/ directory within your category path.
For more details on supported export formats and their structure, see the Annotation Formats documentation.
Advanced Usage
For more details on the implementation and advanced usage, see the Implementation Details documentation.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
When using this software, please cite or acknowledge:
SAM Annotator by Pavodi NDOYI MANIAMFU (FingerVision and University of Tsukuba - Adaptive Information Processing Lab - Japan)
https://github.com/pavodi-nm/sam_annotator
Acknowledgments
- Segment Anything Model (SAM) by Meta AI Research
- OpenCV for image processing
- PyTorch for deep learning
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sam_annotator-0.1.1.tar.gz.
File metadata
- Download URL: sam_annotator-0.1.1.tar.gz
- Upload date:
- Size: 238.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
215456c5fecec72a5c14e1cedebd58e15931e577bdcce9004b8d4462c47a3979
|
|
| MD5 |
0954babb1e2d265590fa7c55250c48ee
|
|
| BLAKE2b-256 |
8f144e4db2c22839fd71ed84599cd1d5cc455b7faaba44077d9c134bb356506d
|
Provenance
The following attestation bundles were made for sam_annotator-0.1.1.tar.gz:
Publisher:
publish.yml on pavodi-NM/sam_annotator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sam_annotator-0.1.1.tar.gz -
Subject digest:
215456c5fecec72a5c14e1cedebd58e15931e577bdcce9004b8d4462c47a3979 - Sigstore transparency entry: 908461263
- Sigstore integration time:
-
Permalink:
pavodi-NM/sam_annotator@5c599c6128559e97da24d3d0b65a7d1685a526ab -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pavodi-NM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5c599c6128559e97da24d3d0b65a7d1685a526ab -
Trigger Event:
push
-
Statement type:
File details
Details for the file sam_annotator-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sam_annotator-0.1.1-py3-none-any.whl
- Upload date:
- Size: 245.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c74fa3965610e5bc90adc8c3155daeea6e8cb92f0d3bbdf2b7a2d1c1a37ec92
|
|
| MD5 |
b470a4ab72b1c70cf7916b694d7aff05
|
|
| BLAKE2b-256 |
2f9f449d34feffff17a773b3cb14788601d99c56a7cb02b37a125855fcc17344
|
Provenance
The following attestation bundles were made for sam_annotator-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on pavodi-NM/sam_annotator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sam_annotator-0.1.1-py3-none-any.whl -
Subject digest:
3c74fa3965610e5bc90adc8c3155daeea6e8cb92f0d3bbdf2b7a2d1c1a37ec92 - Sigstore transparency entry: 908461268
- Sigstore integration time:
-
Permalink:
pavodi-NM/sam_annotator@5c599c6128559e97da24d3d0b65a7d1685a526ab -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pavodi-NM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5c599c6128559e97da24d3d0b65a7d1685a526ab -
Trigger Event:
push
-
Statement type: