従来のSAMはすごいが少し遅い
SAM(Segment Anything)はどんな画像のどんなオブジェクトもインスタンスにセグメントできるモデルです。
ポイントを指定することによりあらゆるものをセグメントできます。
しかし従来のモデルは画像一枚に1秒ほどかかりました。
やばい速いモデルきた
FastSAMは、その約10倍高速なバージョンです。
これにより、リアルタイムセグメントなど様々なタスクに使える可能性が広がりました。
しかも、テキストやボックスやポイントで指定したオブジェクトを自在に切り抜けます。
使い方かんたん
しかも使い方かんたんです。以下手順。
インストール
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
全部のインスタンスをセグメント
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg
テキストで指定したオブジェクトをセグメント
指定テキスト:黒い髪の女の子
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --text_prompt "the black hair girl"
ボックス座標で指定したオブジェクトをセグメント
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --box_prompt "[[290,200,300,500]]"
python Inference.py --model_path FastSAM-x.pt --img_path ./images/students.jpg --point_prompt "[[600,360],[900,360]]" --point_label "[1,1]"
Pythonコードから使う
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',)
すごいよね。
🐣
フリーランスエンジニアです。
AIについて色々記事を書いていますのでよかったらプロフィールを見てみてください。
もし以下のようなご要望をお持ちでしたらお気軽にご相談ください。
AIサービスを開発したい、ビジネスにAIを組み込んで効率化したい、AIを使ったスマホアプリを開発したい、
ARを使ったアプリケーションを作りたい、スマホアプリを作りたいけどどこに相談したらいいかわからない…
いずれも中間コストを省いたリーズナブルな価格でお請けできます。
お仕事のご相談はこちらまで
rockyshikoku@gmail.com
機械学習やAR技術を使ったアプリケーションを作っています。
機械学習/AR関連の情報を発信しています。