Skip to content

Automatically generate masks using Segment Anything 2.1 and Florence 2 and make manual adjustments using a UI

License

Notifications You must be signed in to change notification settings

Tophness/Segment-Anything-Mask-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Segment Anything 2.1 / Florence 2 Automasker

This Python script allows for interactive image masking using either AI-based segmentation with Segment Anything 2.1 or manual painting/lasso selection in a UI.

Features

  • AI Mode (Segment Anything 2.1)
    • Hovering over objects previews a mask using the tiny model.
    • Clicking applies a mask using the large model.
  • AI Mode (Florence 2 + Segment Anything 2.1)
    • Provide a prompt along with input and output folders to automatically mask every image in the folder
  • Manual Masking Mode
    • Lasso Selection (Default Mode)
      • Press L to toggle lasso mode.
      • Left click: Lasso a white mask.
      • Middle click: Lasso a grey mask.
      • Right click: Erase mask.
    • Paintbrush Mode
      • Left click: Paint a white mask.
      • Middle click: Paint a grey mask.
      • Right click: Erase mask.
  • Zooming
    • Use the mouse wheel to zoom in and out.
  • Saving
    • Press S to save mask
  • Reset
    • Press R to reset masks
  • Fullscreen
    • Press esc to fullscreen
  • Navigation
    • Press left and right keys to navigate between files.
  • Toggle AI Mode
    • Press tab to toggle AI mode (off by default)

Installation

Ensure you have Git and Python installed, then run the following commands:

git clone https://github.com/Tophness/Segment-Anything-Mask-Generator
cd Segment-Anything-Mask-Generator
python -m venv venv
call venv\Scripts\activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
pip install -r requirements.txt

Usage

Run the script with:

python main.py

You can also provide input and output folders to automatically launch in manual / SAM 2.1 mask editing mode using those folders, choose a file in the folders to start from and use cpu if you don't want to use gpu:

python main.py --input_folder "folderpath" --output_folder "folderpath" --start_file "start_file.png" --cpu

Screenshots

Selecting a hand in AI mode

seg1

Drawing a white mask in lasso mode

seg2

Removing part of a mask in lasso mode

seg3

Drawing a grey mask in paintbrush mode

seg4

License

This project is released under the MIT License.

About

Automatically generate masks using Segment Anything 2.1 and Florence 2 and make manual adjustments using a UI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages