1
2

Pythonで物体検出をかんたんにつかう(Yolov8)

Last updated at Posted at 2024-01-30

まずはインストール

pip install ultralytics

推論

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')
# model = YOLO('path/to/best.pt')  # load a custom model

results = model('your_image.jpg',save=True)

f9aca3f2-a364-4de7-a986-b4e84b649140.jpeg

これで結果が得られる。

import torch
torch.set_printoptions(sci_mode=False)

for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs
    print(boxes)

ultralytics.engine.results.Boxes object with attributes:

cls: tensor([0.], device='cuda:0')
conf: tensor([0.9016], device='cuda:0')
data: tensor([[ 0.4800, 195.0557, 423.2474, 681.3470, 0.9016, 0.0000]], device='cuda:0')
id: None
is_track: False
orig_shape: (683, 512)
shape: torch.Size([1, 6])
xywh: tensor([[211.8637, 438.2014, 422.7674, 486.2914]], device='cuda:0')
xywhn: tensor([[0.4138, 0.6416, 0.8257, 0.7120]], device='cuda:0')
xyxy: tensor([[ 0.4800, 195.0557, 423.2474, 681.3470]], device='cuda:0')
xyxyn: tensor([[ 0.0009, 0.2856, 0.8267, 0.9976]], device='cuda:0')

複数のオブジェクトを検出した時の結果は以下のようになる。

b0035d8a-a72c-433d-8115-145b235845bb.jpeg

ultralytics.engine.results.Boxes object with attributes:

cls: tensor([ 0., 0., 0., 0., 24.], device='cuda:0')
conf: tensor([0.8816, 0.8743, 0.8731, 0.8357, 0.4269], device='cuda:0')
data: tensor([[ 166.5161, 412.5081, 260.5587, 641.0244, 0.8816, 0.0000],
[ 267.1896, 400.3481, 359.5561, 651.5280, 0.8743, 0.0000],
[ 74.9264, 409.4066, 186.5217, 642.6123, 0.8731, 0.0000],
[ 353.8848, 404.5221, 463.7809, 644.9059, 0.8357, 0.0000],
[ 410.5692, 518.9191, 457.3019, 586.6266, 0.4269, 24.0000]], device='cuda:0')
id: None
is_track: False
orig_shape: (663, 512)
shape: torch.Size([5, 6])
xywh: tensor([[213.5374, 526.7662, 94.0426, 228.5162],
[313.3728, 525.9380, 92.3665, 251.1798],
[130.7240, 526.0095, 111.5953, 233.2057],
[408.8328, 524.7140, 109.8961, 240.3838],
[433.9355, 552.7728, 46.7327, 67.7075]], device='cuda:0')
xywhn: tensor([[0.4171, 0.7945, 0.1837, 0.3447],
[0.6121, 0.7933, 0.1804, 0.3789],
[0.2553, 0.7934, 0.2180, 0.3517],
[0.7985, 0.7914, 0.2146, 0.3626],
[0.8475, 0.8337, 0.0913, 0.1021]], device='cuda:0')
xyxy: tensor([[166.5161, 412.5081, 260.5587, 641.0244],
[267.1896, 400.3481, 359.5561, 651.5280],
[ 74.9264, 409.4066, 186.5217, 642.6123],
[353.8848, 404.5221, 463.7809, 644.9059],
[410.5692, 518.9191, 457.3019, 586.6266]], device='cuda:0')
xyxyn: tensor([[0.3252, 0.6222, 0.5089, 0.9669],
[0.5219, 0.6038, 0.7023, 0.9827],
[0.1463, 0.6175, 0.3643, 0.9692],
[0.6912, 0.6101, 0.9058, 0.9727],
[0.8019, 0.7827, 0.8932, 0.8848]], device='cuda:0')

resultsは、複数の入力を想定した結果のリストであり、
その1要素(result)が1枚の画像に対応する結果である。
その中のboxesが複数のオブジェクトの結果を格納した配列。

結果---画像----box
    |      |--box
    |      |--box
    |      |--box
    |
    |--画像----box
    |      |--box
    |
    |--画像

つまり、入力は複数でも可能である。画像パスのリストも渡せる。

サンプルコード

Open In Colab

🐣


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

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

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

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

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

X
Medium
GitHub

1
2
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
1
2