LoginSignup
2
0

Python で物体認識AIのYOLOv8を試してみた!〜応用編〜

Posted at

はじめに

今回は、物体認識のYOLOv8の応用編として、動画から物体の名称や自信度、座標位置を取得する方法をご紹介します。
YOLOv8のインストール方法や基本的な使い方は、前回の記事又は、Youtube動画をご覧ください。

環境

Windows10
Python3.11.8
GPU:NVIDIA Geforce RTX3060
VSCode

使用ライブラリー

pytorch 2.2.1+cu118
ultralytics 8.1.25
opencv-python 4.9.0.80

処理のイメージ

処理イメージ.JPG

YouTubeでの解説:

上記のインストール方法から、サンプルプログラムの実行までをYoutubeで詳しく解説していますので、ぜひ、ご覧ください。

サンプルプログラム1

YouTubeで紹介している1つ目の処理のプログラムソースです。
画像から物体を認識して、各種属性情報をprintしています。

YOLO_test1.py
from ultralytics import YOLO
from PIL import Image
import cv2

model = YOLO("yolov8x.pt")

im1 = Image.open("sample1.jpg")
results = model.predict(source=im1, save=True)  # save plotted images

for result in results:
    # Detection
    print('---boxes.xyxy---')  
    print(result.boxes.xyxy)   # box with xyxy format, (N, 4)
    print('---boxes.xywh---')
    print(result.boxes.xywh)   # box with xywh format, (N, 4)
    print('---boxes.xyxyn---')
    print(result.boxes.xyxyn)  # box with xyxy format but normalized, (N, 4)
    print('---boxes.xywhn---')
    print(result.boxes.xywhn)  # box with xywh format but normalized, (N, 4)
    print('---boxes.conf---')
    print(result.boxes.conf)   # confidence score, (N, 1)
    print('---boxes.cls---')
    print(result.boxes.cls)    # cls, (N, 1)
    
    print(result.names)

サンプルプログラム2

Youtubeで紹介している、画像ファイルから物体の名称、自信度、座標位置をcsvファイルに出力するサンプルプログラムです。

YOLO_test_csv.py
from ultralytics import YOLO
import cv2
import csv

csv_filename = 'sample1.csv'
# 出力するデータ
header = ['Class', 'Label','Scores','x1','y1','x2','y2']
# CSVファイルにデータを書き込む
file = open(csv_filename, mode='w', newline='', encoding='utf-8')
writer = csv.writer(file)
writer.writerow(header)

model = YOLO("yolov8x.pt")

# # # from PIL
im1 = Image.open("sample1.jpg")
results = model.predict(source=im1)  # save plotted images

items = results[0]

for item in items:
    cls = int(item.boxes.cls)    # cls, (N, 1)
    label = item.names[int(cls)]
    score = item.boxes.conf.cpu().numpy()[0]   # confidence score, (N, 1)
    x1,y1,x2,y2 = item.boxes.xyxy.cpu().numpy()[0]   # box with xyxy format, (N, 4)

    csv_data = [str(cls),str(label),str(score),str(x1),str(y1),str(x2),str(y2)]
    writer.writerow(csv_data)

items.show()
 # ファイルを閉じる
file.close()   

サンプルプログラム3

Youtubeで紹介している、動画ファイルから物体の名称、自信度、座標位置、トラッキングIDをcsvファイルに出力すると共に、物体をトラッキングするプログラムです。

YOLO_test_video.py
from ultralytics import YOLO
import cv2
import csv

csv_filename = 'sample1.csv'
# 出力するデータ
header = ['No','Class', 'Label','Scores','id','x1','y1','x2','y2']
# CSVファイルにデータを書き込む
file = open(csv_filename, mode='w', newline='', encoding='utf-8')
writer = csv.writer(file)
writer.writerow(header)

model = YOLO("yolov8x.pt")

# Open the video file
video_path = "./sample/sample_1.mp4"
cap = cv2.VideoCapture(video_path)

frame_cnt = 0
# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()

    if success:
        # Run YOLOv8 inference on the frame
        results = model.track(frame,persist=True,conf = 0.5,classes=[0,2,7])

        frame_cnt = frame_cnt + 1
        # Visualize the results on the frame
        annotated_frame = results[0].plot()

        items = results[0]
        for item in items:
            cls = int(item.boxes.cls)    # cls, (N, 1)
            label = item.names[int(cls)]
            score = item.boxes.conf.cpu().numpy()[0]   # confidence score, (N, 1)
            x1,y1,x2,y2 = item.boxes.xyxy.cpu().numpy()[0]   # box with xyxy format, (N, 4)

            id_value = item.boxes.id
            if id_value is None:
                track_ids = ''
            else:
                track_ids = item.boxes.id.int().cpu().tolist()[0]

            csv_data = [str(frame_cnt),str(cls),str(label),str(score),str(track_ids),str(x1),str(y1),str(x2),str(y2)]
            writer.writerow(csv_data)

        # Display the annotated frame
        cv2.imshow("YOLOv8 Inference", annotated_frame)

        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

 # ファイルを閉じる
file.close()   
 

最後に:

今回は、物体認識のYOLOv8の様々な使い方の例をご紹介しました。

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