LoginSignup
0
0

「人」「猫」「顔」「靴」テキストで切り抜く対象を指定できるlang-segment-anything

Posted at

セグメントはここまで進化した

セグメントの歴史

切り抜きたいものの画像を集めてトレーニング

画像内の目立つものを切り抜く

ポイントやボックスで囲んだものを切り抜く

言葉で指定して切り抜く(←イマココ)

laptop

smart phone

cat

使い方

インストール

pip install -U git+https://github.com/luca-medeiros/lang-segment-anything.git

実行

from PIL import Image
from lang_sam import LangSAM

model = LangSAM()

image_path = "image.jpg"
image_pil = Image.open(image_path).convert("RGB")
text_prompt = "laptop"
masks, boxes, phrases, logits = model.predict(image_pil, text_prompt)

視覚化

import cv2
import numpy as np
import torch
from PIL import Image
from torchvision.utils import draw_bounding_boxes
from torchvision.utils import draw_segmentation_masks

def draw_image(image, masks, boxes, labels, alpha=0.4):
    image = torch.from_numpy(image).permute(2, 0, 1)
    if len(boxes) > 0:
        image = draw_bounding_boxes(image, boxes, colors=['red'] * len(boxes), labels=labels, width=2)
    if len(masks) > 0:
        image = draw_segmentation_masks(image, masks=masks, colors=['blue'] * len(masks), alpha=alpha)
    return image.numpy().transpose(1, 2, 0)

bdbfaa91-47dd-42f8-bb60-97ae01aa6571.jpeg

smart phone

3fee4154-411c-463a-855c-5d9361f68af5.jpeg

cat

output (1).jpg

🐣


フリーランスエンジニアです。
AIについて色々記事を書いていますのでよかったらプロフィールを見てみてください。

もし以下のようなご要望をお持ちでしたらお気軽にご相談ください。
AIサービスを開発したい、ビジネスにAIを組み込んで効率化したい、AIを使ったスマホアプリを開発したい、
ARを使ったアプリケーションを作りたい、スマホアプリを作りたいけどどこに相談したらいいかわからない…

いずれも中間コストを省いたリーズナブルな価格でお請けできます。

お仕事のご相談はこちらまで
rockyshikoku@gmail.com

機械学習やAR技術を使ったアプリケーションを作っています。
機械学習/AR関連の情報を発信しています。

X
Medium
GitHub

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0