コンテンツへスキップ

Ultralytics YOLOv8 🚀を使ったインスタンスのセグメンテーションとトラッキング

インスタンス・セグメンテーションとは?

Ultralytics YOLOv8インスタンスセグメンテーションは、画像内の個々の物体を識別し、輪郭を描くことで、空間分布を詳細に理解する。セマンティック・セグメンテーションとは異なり、インスタンス・セグメンテーションでは、各オブジェクトに一意のラベルを付け、正確に区切る。

Ultralytics 、2種類のインスタンス・セグメンテーション・トラッキングが利用できる:

  • クラス・オブジェクトによるインスタンスのセグメンテーション:各クラスオブジェクトには、視覚的に明確に分離するために固有の色が割り当てられています。

  • オブジェクトトラックによるインスタンスセグメンテーション:すべてのトラックは明確な色で表現され、識別と追跡が容易になります。



見るんだ: オブジェクト・トラッキングを用いたインスタンス・セグメンテーションUltralytics YOLOv8

サンプル

インスタンスのセグメンテーション インスタンス・セグメンテーション+オブジェクト・トラッキング
Ultralytics インスタンスのセグメンテーション Ultralytics オブジェクト追跡によるインスタンス分割
Ultralytics インスタンスのセグメンテーション Ultralytics オブジェクト・トラッキングによるインスタンス・セグメンテーション 🔥 🔥 .

インスタンスのセグメンテーションとトラッキング

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8n-seg.pt")  # segmentation model
names = model.model.names
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    results = model.predict(im0)
    annotator = Annotator(im0, line_width=2)

    if results[0].masks is not None:
        clss = results[0].boxes.cls.cpu().tolist()
        masks = results[0].masks.xy
        for mask, cls in zip(masks, clss):
            color = colors(int(cls), True)
            txt_color = annotator.get_txt_color(color)
            annotator.seg_bbox(mask=mask, mask_color=color, label=names[int(cls)], txt_color=txt_color)

    out.write(im0)
    cv2.imshow("instance-segmentation", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()
from collections import defaultdict

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

track_history = defaultdict(lambda: [])

model = YOLO("yolov8n-seg.pt")  # segmentation model
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation-object-tracking.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    annotator = Annotator(im0, line_width=2)

    results = model.track(im0, persist=True)

    if results[0].boxes.id is not None and results[0].masks is not None:
        masks = results[0].masks.xy
        track_ids = results[0].boxes.id.int().cpu().tolist()

        for mask, track_id in zip(masks, track_ids):
            color = colors(int(track_id), True)
            txt_color = annotator.get_txt_color(color)
            annotator.seg_bbox(mask=mask, mask_color=color, label=str(track_id), txt_color=txt_color)

    out.write(im0)
    cv2.imshow("instance-segmentation-object-tracking", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

seg_bbox 論争

名称 タイプ デフォルト 説明
mask array None セグメンテーション・マスク座標
mask_color RGB (255, 0, 255) すべてのセグメント・ボックスのマスク・カラー
label str None セグメント化されたオブジェクトのラベル
txt_color RGB None セグメント化され追跡されたオブジェクトのラベルの色

お問い合わせは、Ultralytics Issue Sectionまたは下記のディスカッション・セクションまでお気軽にお寄せください。

よくあるご質問

Ultralytics YOLOv8 を使ってインスタンスのセグメンテーションを行うには?

Ultralytics YOLOv8 を使ってインスタンスのセグメンテーションを行うには、YOLOv8 のセグメンテーションバージョンでYOLO モデルを初期化し、それを使ってビデオフレームを処理する。以下に、簡略化したコード例を示す:

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8n-seg.pt")  # segmentation model
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        break

    results = model.predict(im0)
    annotator = Annotator(im0, line_width=2)

    if results[0].masks is not None:
        clss = results[0].boxes.cls.cpu().tolist()
        masks = results[0].masks.xy
        for mask, cls in zip(masks, clss):
            annotator.seg_bbox(mask=mask, mask_color=colors(int(cls), True), det_label=model.model.names[int(cls)])

    out.write(im0)
    cv2.imshow("instance-segmentation", im0)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

インスタンスのセグメンテーションの詳細については、Ultralytics YOLOv8 ガイドを参照してください。

Ultralytics YOLOv8 におけるインスタンスセグメンテーションとオブジェクトトラッキングの違いは?

インスタンスセグメンテーションは、画像内の個々のオブジェクトを識別し、アウトライン化し、各オブジェクトにユニークなラベルとマスクを与える。オブジェクトトラッキングは、ビデオフレーム間でオブジェクトに一貫性のあるラベルを割り当てることで、これを拡張し、同じオブジェクトの継続的なトラッキングを容易にします。Ultralytics YOLOv8 ドキュメントで、その違いを詳しくご覧ください。

例えばセグメンテーションやトラッキングに、マスクR-CNNやファスターR-CNNのような他のモデルではなく、Ultralytics YOLOv8 。

Ultralytics YOLOv8 YOLOv8 は HUB とシームレスに統合されており、ユーザーはモデル、データセット、トレーニングパイプラインを効率的に管理することができます。 の利点については、Ultralytics YOLOv8 Ultralytics ブログをご覧ください。

Ultralytics YOLOv8 を使ってオブジェクト・トラッキングを実装するには?

オブジェクト・トラッキングを実装するには model.track メソッドを使用して、各オブジェクトのIDがフレーム間で一貫して割り当てられるようにします。以下は簡単な例です:

from collections import defaultdict

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

track_history = defaultdict(lambda: [])

model = YOLO("yolov8n-seg.pt")  # segmentation model
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation-object-tracking.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        break

    annotator = Annotator(im0, line_width=2)
    results = model.track(im0, persist=True)

    if results[0].boxes.id is not None and results[0].masks is not None:
        masks = results[0].masks.xy
        track_ids = results[0].boxes.id.int().cpu().tolist()

        for mask, track_id in zip(masks, track_ids):
            annotator.seg_bbox(mask=mask, mask_color=colors(track_id, True), track_label=str(track_id))

    out.write(im0)
    cv2.imshow("instance-segmentation-object-tracking", im0)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

詳しくは、インスタンスのセグメンテーションとトラッキングのセクションをご覧ください。

Ultralytics 、例えばセグメンテーションやトラッキングなどのYOLOv8 モデルのトレーニングに適したデータセットはありますか?

はい。Ultralytics は、セグメンテーションやトラッキングのデータセットなど、YOLOv8 モデルのトレーニングに適したデータセットをいくつか提供しています。データセットの例、構造、使用方法は、Ultralytics Datasetsdocumentationに記載されている。



作成 2023-12-18 更新 2024-07-14
著者RizwanMunawar(2),glenn-jocher(10),IvorZhu331(1)

コメント