🗂

YOLOv8 を試すための環境をつくる

2024/03/27に公開

ultralytics のインストール

https://github.com/ultralytics/ultralytics にしたがって,pip3 でインストールする.

$ pip3 install ultralytics

ImportError: libGL.so.1: cannot open shared object file: No such file or directory の解決

yolov8n を使って推論させようとしたら,以下のエラーが出て推論できなかった.

Traceback (most recent call last):
  File "/workspaces/yolov8-nvidia-docker/src/predict.py", line 1, in <module>
    from ultralytics import YOLO
  File "/home/vscode/.local/lib/python3.10/site-packages/ultralytics/__init__.py", line 5, in <module>
    from ultralytics.data.explorer.explorer import Explorer
  File "/home/vscode/.local/lib/python3.10/site-packages/ultralytics/data/__init__.py", line 3, in <module>
    from .base import BaseDataset
  File "/home/vscode/.local/lib/python3.10/site-packages/ultralytics/data/base.py", line 12, in <module>
    import cv2
  File "/home/vscode/.local/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/vscode/.local/lib/python3.10/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/opt/conda/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

https://github.com/ultralytics/ultralytics/issues/1270 に書いてあるように,pip3 install opencv-python-headless を実行すれば解決できる.

推論

公式ページにあるように,以下コードを実行できます.

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # pretrained YOLOv8n model

# Run batched inference on a list of images
results = model(['../img.jpeg'])  # return a list of Results objects

# Process results list
for result in results:
    boxes = result.boxes  # Boxes object for bounding box outputs
    masks = result.masks  # Masks object for segmentation masks outputs
    keypoints = result.keypoints  # Keypoints object for pose outputs
    probs = result.probs  # Probs object for classification outputs
    result.show()  # display to screen
    result.save(filename='result.jpg')  # save to disk

Discussion