源码位置:https://github.com/facebookresearch/segment-anything

一、下载

pip install git+https://github.com/facebookresearch/segment-anything.git

或者 直接下载,解压到当前文件夹,并把解压出的文件夹名字改成segment-anything

二、安装

1、进入segment-anything文件夹

cd segment-anything

2、安装

pip install -e .

3、安装其他依赖

pip install opencv-python pycocotools matplotlib onnxruntime onnx

4、下载模型文件到segment-anything文件夹内

default or vit_h:

https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth

vit_l:

https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth

vit_b:

https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth

5、下载数据集

https://ai.facebook.com/datasets/segment-anything-downloads/

或者用自己的图片数据集

三、运行

整体文件结构

python scripts/amg.py --checkpoint <path/to/checkpoint> --model-type <model_type> --input <image_or_folder> --output <path/to/output>

例如:在D:\PythonProject\segment-anything\test\input文件夹下放入图片,使用vit_b模型sam_vit_b_01ec64.pth,输出到D:\PythonProject\segment-anything\test\output文件夹内

python scripts/amg.py --checkpoint sam_vit_b_01ec64.pth --model-type vit_b --input D:\PythonProject\segment-anything\test\input --output D:\PythonProject\segment-anything\test\output

输入:

输出: