Skip to content

Divelix/samat

Repository files navigation

SAM Annotation Tool (SAMAT)

SAMAT is a hackable PyQt5 based GUI labelling tool for semantic segmentation task enhanced by an assistance feature, reminiscent of the functionality offered by the Photoshop Magic Wand tool.

Note: Magic Wand is optional tool and uses SAM masks that can be generated by python script in scripts directory of this repo.

Showcase

Animation below shows annotation speed in real-time (SAM mask used). showcase

Workflow

  • (optional) Generate SAM masks from images via given script
  • Organize your data following this structure
  • Specify path to your data in config.toml
  • Run GUI via __main__.py (prerequisites should be satisfied)
  • Annotate using brush (label is saved on sample switch)

Getting started

Prerequisites

Annotation tool itself requires only:

  • Python 3.11
  • PyQt5
  • numpy

Example setup (Ubuntu):

git clone https://github.com/Divelix/samat.git
cd samat
sudo apt update
sudo apt install python3.11-venv
pip3 install virtualenv
virtualenv venv -p python3.11
source venv/bin/activate
python -m pip install -e .
python .

(optional) In order to generate SAM masks for Magic Wand, you will need to install:

  • PyTorch to run SAM model inference
  • segment-anything itself + related libs

Example setup (assuming Miniconda/Anaconda installed):

conda create -n samat python=3.11
conda activate samat
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install opencv-python pycocotools matplotlib onnxruntime onnx

Dataset folder structure

Your data MUST follow this structure:

── my_dataset
   ├── images
   |   ├── 000001.png
   |   ├── 000002.png
   |   └── ...
   ├── labels (optional)
   |   ├── 000001.png
   |   ├── 000002.png
   |   └── ...
   ├── sam (optional)
   |   ├── 000001.png
   |   ├── 000002.png
   |   └── ...
   └── classes.json
  • images contains .png files you want to label
  • labels contains .png files with labels (will be automatically created if you have no labels yet)
  • sam contains .png files with SAM annotations (8-bit grayscale product of SAM script from scripts/ folder)
  • classes.json contains classes description that will be used for labeling

Example classes.json:

{
    "classes": [
        { "id": 1, "name": "human", "color": "#FF0000" },
        { "id": 2, "name": "car", "color": "#00FF00" },
    ]
}

where:

  • id field must coinside with number keys on keyboard, so start with 1 (not 0). Any number of classes allowed, but only first 9 have their shortcuts.
  • name field is arbitrary and used only for dispaly in GUI
  • color field specifies the color this class would be displayed in GUI and encoded in output label .png

Note: specify path to your my_dataset (or any other name) inside config.toml.

Note: image files can have arbitrary names, but should resemble labels and sam names + only .png format is suppotred.

Shortcuts

Shortcut Description
Left Mouse Button Draw with brush + fill region (in SAM mode)
Right Mouse Button Pan motion on zoomed-in image
Mouse Wheel Zoom in/out
Ctrl + Mouse Wheel Change brush size
1-9 Select class (color to draw on label layer)
E Eraser tool (transparent brush)
Space Reset zoom
C Clear label
S Switch SAM assistance mode on/off
,/. Previous/Next sample