YOLOV8模型使用-检测-物体追踪

这个最新的物体检测模型,很厉害的样子,还有物体追踪的功能。

有官方的Python代码,直接上手试试就好,至于理论,有想研究在看论文了╮(╯_╰)╭


简单介绍

YOLOv8 中可用的模型

YOLOv8 模型的每个类别中有五个模型用于检测、分割和分类。YOLOv8 Nano 是最快和最小的,而 YOLOv8 Extra Large (YOLOv8x) 是其中最准确但最慢的。用来实际使用的时候选权重模型。

| YOLOv8n | YOLOv8s | YOLOv8m | YOLOv8l | YOLOv8x |

其他介绍,就不用管了,上手玩一下要紧。看一下几个官方介绍图片就懂了:


这里可以看到,有物体检测识别,检测,分类,轨迹,姿态的功能,下面就上手试试。


部署-简单使用【超简单】

前提安装好Python,版本需要Python>=3.8 我的是 Python 3.11.3

视频图片识别

  1. 首先,先下载官方的代码。官网代码

  2. 执行安装与检测:【执行位置是在项目目录下】

shell 复制代码
pip install -r requirements.txt
pip install ultralytics

# 执行这个,会自动下载模型
# Downloading https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt to 'yolov8n.pt'...
# source 替换成需要检测的本地图片即可
yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'

# 也可以如下对视频进行检测
yolo task=detect mode=predict model=yolov8n.pt source=C:\Users\Administrator\Desktop\sssss-1.mp4  show=True

#实例分割
yolo task=segment  mode=predict model=yolov8n-seg.pt source=C:\Users\Administrator\Desktop\sssss-1.mp4  show=True
  1. 看看这个检测出来的效果:

  2. 是不是灰常的简单,[]( ̄▽ ̄)*

  3. 就酱紫,后面在试试其他功能。


视频流,摄像头识别

这个处理只需要把来源替换成0即可,就像这样

shell 复制代码
yolo task=detect mode=predict model=yolov8n.pt source=0 show=True

视频追踪-绘制随时间变化的轨迹【这个有意思】

可以用于视频追踪的模型是:YOLOv8n, YOLOv8n-seg and YOLOv8n-pose 【以8n举例子】

shell 复制代码
yolo track model=yolov8n.pt source=0 show=True 

这个追踪的效果就是,在识别里面多了一个ID表示固定的物体。

以下是官方代码改了一下,绘制随时间变化的轨迹

效果是这样的:

这个车流比较多感觉轨迹画的不怎么好看。

哈哈,这个卡车还识别错了 。。╮(╯▽╰)╭

不过这里可以绘制轨迹,就也可以统计这个ID物体在视频中存在的时间什么的。如果放在门店咖啡厅的摄像头里面,就可以看到顾客的停留时间。

这个轨迹变化绘制+物体追踪代码如下:

python 复制代码
# 绘制随时间变化的轨迹
from collections import defaultdict

import cv2
import numpy as np

from ultralytics import YOLO

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

# Open the video file
# video_path = "C:\\Users\\Administrator\\Desktop\\1.ts" 
video_path = 0
cap = cv2.VideoCapture(video_path)

# Store the track history
track_history = defaultdict(lambda: [])

# 用于保存图像
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# out_cat = cv2.VideoWriter("C:\\Users\\Administrator\\Desktop\\save.mp4", fourcc, 24, (352, 288), True)  # 保存位置/格式

# 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)

        # Get the boxes and track IDs
        boxes = results[0].boxes.xywh.cpu()
        if results[0].boxes.id is not None:
            track_ids = results[0].boxes.id.int().cpu().tolist()

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

        # Plot the tracks
        if results[0].boxes.id is not None:
            for box, track_id in zip(boxes, track_ids):
                x, y, w, h = box
                track = track_history[track_id]
                track.append((float(x), float(y)))  # x, y center point
                if len(track) > 30:  # retain 90 tracks for 90 frames
                    track.pop(0)

                # Draw the tracking lines
                points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
                cv2.polylines(annotated_frame, [points], isClosed=False, color=(track_id*10%255, 100, 255), thickness=2)

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


        # out_cat.write(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()

参考资料:


相关推荐
学算法的程霖5 小时前
CVPR2025 | 首个多光谱无人机单目标跟踪大规模数据集与统一框架, 数据可直接下载
人工智能·深度学习·目标检测·机器学习·计算机视觉·目标跟踪·研究生
山海不说话7 小时前
深度学习(第3章——亚像素卷积和可形变卷积)
图像处理·人工智能·pytorch·深度学习·目标检测·计算机视觉·超分辨率重建
白熊18811 小时前
【计算机视觉】论文精读《基于改进YOLOv3的火灾检测与识别》
人工智能·yolo·计算机视觉
qq_4162764211 小时前
SuperYOLO:多模态遥感图像中的超分辨率辅助目标检测之论文阅读
论文阅读·人工智能·目标检测
白熊18812 小时前
【图像生成大模型】Step-Video-T2V:下一代文本到视频生成技术
人工智能·opencv·yolo·计算机视觉·大模型·音视频
Blossom.11813 小时前
基于区块链技术的供应链溯源系统:重塑信任与透明度
服务器·网络·人工智能·目标检测·机器学习·计算机视觉·区块链
埃菲尔铁塔_CV算法13 小时前
深度学习驱动下的目标检测技术:原理、算法与应用创新(二)
深度学习·算法·目标检测
水花花花花花1 天前
Transformer 架构在目标检测中的应用:YOLO 系列模型解析
目标检测·架构·transformer
Blossom.1181 天前
从虚拟现实到混合现实:沉浸式体验的未来之路
人工智能·目标检测·机器学习·计算机视觉·语音识别·vr·mr
埃菲尔铁塔_CV算法1 天前
深度学习驱动下的目标检测技术:原理、算法与应用创新
深度学习·算法·目标检测