2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

YOLOv8.1 Google Colabで学習し、Jetson NanoとPythonで物体検出する(2024/02)

Last updated at Posted at 2024-02-03

最新版をご覧ください。

  • Jetson Nano 4GB
  • micro SDXC 64GB
  • logicool C270N
  • Ubuntu 20.04

Google Colab上でYOLOv8.1.9を用いて学習する

Pythonのバージョンを確認します(2024/02/03:3.10.12)

!python3 -V

Google Driveをマウントします

from google.colab import drive
drive.mount('/content/drive')

YOLOv8.1用のディレクトリを作成します

%cd "/content/drive/MyDrive/"
%mkdir Python3.10_YOLOv8.1
%cd Python3.10_YOLOv8.1

YOLOv8.1をインストールします

!pip3 install ultralytics

YOLOv8の動作確認をします

!yolo predict model=yolov8n.pt source='https://ultralytics.com/images/zidane.jpg'
from IPython.display import Image, clear_output
Image(filename='runs/detect/predict/zidane.jpg', width=600)

Python3.10_YOLOv8.1直下にdata.yamlをアップロードします

data.yaml
train: ../train/images
val: ../valid/images

# number of classes
nc: 4

# class names
names: ['pcb', 'battery', 'nut', 'spacer']

Python3.10_YOLOv8.1直下にtrain、validフォルダをアップロードし、学習を行います

!yolo detect train data=data.yaml model=yolov8n.pt epochs=300 imgsz=640 batch=8

マイドライブ > Python3.10_YOLOv8.1 > runs > detect > train > weights に重みファイル best.ptが保存されます。

Jetson Nanoで YOLOv8を動かす

Jetson Nano with Ubuntu 20.04 OS image(2024/02/03:20.04 3b)を入手します

※username:jetson、password:jetson

キーボード、タイム ゾーンの設定

Settings
-> Region & Language -> Input Sources:Other -> Japanese
-> Data & Time -> Time Zone:JST(Tokyo, Japan)

モジュールの追加・更新

pip3 install psutil==5.9.5

YOLOv8.1.9をダウンロードします

  • 「pypi.org」から「ultralytics-8.1.9-py3-none-any.whl」をHome直下にダウンロードします。

YOLOv8.1.9をインストールします。

pip3 install --no-deps ultralytics-8.1.9-py3-none-any.whl

YOLOv8の動作確認

yolo predict model=yolov8n.pt source="https://ultralytics.com/images/zidane.jpg"

検出結果は「Home/runs/detect/predict」内に保存されます(zidane.jpg)。

  • Google Colabで学習した重みファイルを使用して認識を実行
yolo task=detect mode=predict model=best.pt source=0 show=True imgsz=256

Jetson Nano:1 frame:66.3ms(15.0fps)

Pythonでプログラムを作成する

プログラム

JetsonYolo.py
import cv2
import numpy as np
from ultralytics import YOLO

classes = ['pcb', 'battery', 'nut', 'spacer']
colors = list(np.random.rand(4, 3)*255)
model = YOLO('best.pt')

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

cv2.namedWindow('win', cv2.WINDOW_AUTOSIZE)


while True:
    ret, frame = cap.read()

    results = model(frame, conf=0.5, iou=0.5)

    result = results[0]
    
    for box in result.boxes:
        label = classes[int(box.cls)]
        score = round(float(box.conf), 2)
        color = colors[int(box.cls)]
        print(label, score)

        r = box.xyxy.tolist()
        cv2.rectangle(frame, (int(r[0][0]), int(r[0][1])),
                        (int(r[0][2]), int(r[0][3])), color, 2)
        cv2.putText(frame, f'{label} ({str(score)})', (int(r[0][0]), int(r[0][1])),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2, 4)

    cv2.imshow('win', frame)

    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

実行方法

python3 JetsonYolo.py

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?