Traditional SAM is great but a little slow
SAM (Segment Anything) is a model that allows you to segment any object in any image into instances .
You can segment anything by specifying points.
However, previous models took about 1 second to capture each image.
Here’s a really fast model
FastSAM is a version that is about 10 times faster .
This opens up the possibility of using it for various tasks such as real-time segmentation.
What’s more, you can freely cut out objects specified by text, boxes, or points.
Easy to use
And it’s easy to use. Below are the steps.
install
git clone https://github.com/CASIA-IVA-Lab/FastSAM.git # clone Fast-SAM
cd FastSAM
pip install -r requirements.txt
pip install git+https://github.com/openai/CLIP.git # install CLIP
Segment all instances
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg
black hair girl
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --text_prompt "the black hair girl"
Segment object specified by box coordinates
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --box_prompt "[[290,200,300,500]]"
Segment object specified by points
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --point_prompt "[[600,360],[900,360]]" --point_label "[1,1]"
Use from Python code
from fastsam import FastSAM, FastSAMPrompt
model = FastSAM('FastSAM-x.pt')
IMAGE_PATH = './images/girls.jpg'
DEVICE = "cuda:0"
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)
# everything prompt
# ann = prompt_process.everything_prompt()
# bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
# ann = prompt_process.box_prompt(bbox=[100, 200, 300, 700])
# text prompt
ann = prompt_process.text_prompt(text='skirt')
# point prompt
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
# ann = prompt_process.point_prompt(points=[[200, 360]], pointlabel=[1])
prompt_process.plot(annotations=ann,output_path='./output/students.jpg',)
That’s amazing.
🐣
I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.
rockyshikoku@gmail.com
I am creating applications using machine learning and AR technology.
I send machine learning / AR related information.