目标检测模型:Ultralytics-YOLOv8

mtain 2023年09月11日 226次浏览

说明

YOLOv8是Ultralytics公司推出的基于对象检测模型的YOLO最新系列,它能够提供截至目前最先进的对象检测性能。

功能包括

  • 物体检测与跟踪
  • 实例分割
  • 图像分类
  • 姿态估计

Github:https://github.com/ultralytics/ultralytics
文档:https://docs.ultralytics.com/
pytorch:https://pytorch.org/get-started/locally/
Roboflow(AI图像识别模型训练数据标注平台):

安装过程

以centos安装cli为例

1. 安装PyTorch(可跳过)

image.png

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

2. 安装ultralytics

pip install opencv-python-headless

pip install ultralytics

3. 安装其它缺的库文件(根据报错安装)

yum install xz-devel -y
yum install python-backports-lzma -y
pip install backports.lzma


vi /usr/local/python3/lib/python3.9/lzma.py

大概27行改为
import builtins
import io
import os
try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties
import _compression


注意:有的可能需要重新编译Python3.9

4. 测试

# 训练
yolo train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01


# 目标检测
yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'

5. 代码

视频识别追踪

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO('yolov8n.pt')

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

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

    if success:
        # Run YOLOv8 tracking on the frame, persisting tracks between frames
        results = model.track(frame, persist=True)

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

        # Display the annotated frame
        cv2.imshow("YOLOv8 Tracking", 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()