360 Secure SkillHub
360 安全技能中心

Segment Anything

核心功能:基于Meta的SAM模型实现图片背景移除,提取前景为透明PNG,支持单点指定前景、批量提取所有独立元素两种模式。典型场景适用于人像/产品图抠图、多元素拆分。输入为图片路径,输出单张或多张透明PNG;可选择不同精度模型,支持GPU加速,首次运行自动下载模型。

scikkk
scikkk
热度 57.68
最近更新 2026-05-17 14:32:40
分类:创作依赖:代码执行 / 读写文件应用场景:image-editing

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add sam
Metadata
name
sam-segmentation
description
Use SAM (Segment Anything Model) to remove image backgrounds and extract foreground subjects as transparent PNGs. Use when users want to remove backgrounds, cut out objects, extract foreground subjects, or perform image segmentation.
metadata
{"openclaw":{"requires":{"bins":["python3"]},"install":[{"kind":"uv","package":"pillow"},{"kind":"uv","package":"numpy"},{"kind":"uv","package":"torch"},{"kind":"uv","package":"torchvision"}]}}

SAM Background Removal

Extract foreground subjects from images using Meta's Segment Anything Model, outputting transparent PNGs.

Quick Start

python3 scripts/segment.py <input_image> <output.png>

Defaults to the image center as the foreground hint — works well for portraits and product shots where the subject is centered.

Parameters

Param Description Default
input Input image path required
output Output PNG path (single mode) or directory (--all mode) required
--model Model size: vit_b (fast) · vit_l (medium) · vit_h (best quality) vit_h
--checkpoint Local checkpoint path; auto-downloaded if omitted auto
--points Foreground hint points as x,y, multiple allowed center
--all Grid-sweep mode: extract all distinct elements off
--grid Grid density for --all; 16 means 16×16=256 probe points 16
--iou-thresh Minimum predicted IoU to accept a mask (--all) 0.88
--min-area Minimum mask area as fraction of image (--all) 0.001

Examples

# Basic background removal (auto-downloads vit_h ~2.5GB)
python3 scripts/segment.py photo.jpg output.png

# Specify hint point when subject is off-center
python3 scripts/segment.py photo.jpg output.png --points 320,240

# Multiple hints with lightweight model
python3 scripts/segment.py photo.jpg output.png --model vit_b --points 320,240 400,300

# Extract all elements (one PNG per element)
python3 scripts/segment.py photo.jpg ./elements/ --all

# Denser grid to capture small objects
python3 scripts/segment.py photo.jpg ./elements/ --all --grid 32

# Use a local checkpoint
python3 scripts/segment.py photo.jpg output.png --checkpoint /path/to/sam_vit_h_4b8939.pth

Dependencies

segment_anything is auto-installed on first run, or install manually:

pip install git+https://github.com/facebookresearch/segment-anything.git
pip install pillow numpy torch torchvision

Workflow

  1. User provides image path
  2. Ask if hint points are needed (when subject is off-center)
  3. Run script; checkpoint auto-downloads on first use to ~/.cache/sam/
  4. Output transparent-background PNG

Model Selection

Model Size Speed Quality
vit_b ~375 MB fastest good
vit_l ~1.25 GB medium better
vit_h ~2.5 GB slower best

CUDA is used automatically when a GPU is available.