首页 > 其他分享 >【保姆级教程】YOLOv8_Track多目标跟踪,快速运行

【保姆级教程】YOLOv8_Track多目标跟踪,快速运行

时间:2024-03-24 14:33:36浏览次数:22  
标签:教程 Track frame cap cv2 results YOLOv8 track video

一、YOLOV8环境准备

1.1 下载安装最新的YOLOv8代码

 仓库地址: https://github.com/ultralytics/ultralytics

1.2 配置环境

  pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

二、下载测试视频,预训练权重

测试视频
链接:https://pan.baidu.com/s/1xqu4aRxoOGlVLILKLSReqg
提取码:7g9r
–来自百度网盘超级会员V5的分享
预训练权重
在YOLOv8 github上下载预训练权重:yolov8n.pt,ultralytics\ultralytics\路径下,新建weights文件夹,预训练权重放入其中。
在这里插入图片描述

三、v8追踪

from collections import defaultdict
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import cv2
import numpy as np

from ultralytics import YOLO

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

# Open the video file
video_path = "video/car.mp4"
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
size = (width, height)

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

# 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
        if results[0].boxes.id != None:
            boxes = results[0].boxes.xywh.cpu()
            track_ids = results[0].boxes.id.int().cpu().tolist()

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

            # Plot the tracks
            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=(0, 0, 255), thickness=2)

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

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

<iframe allowfullscreen="true" data-mediaembed="csdn" frameborder="0" id="8CdvZ9OT-1711117204170" src="https://live.csdn.net/v/embed/373094"></iframe>

v8 跟踪

标签:教程,Track,frame,cap,cv2,results,YOLOv8,track,video
From: https://blog.csdn.net/m0_51579041/article/details/136952810

相关文章

  • MATLAB 2024a安装包下载及安装教程
    下载链接:https://docs.qq.com/doc/DUllzRVRSZ2VZcWxO1.选中下载的安装包,右键选择解压到"MATLABR2024a"文件夹2.双击打开“setup”文件夹3.找到并选中“setup.exe”鼠标右键选择以管理员身份运行4.点击“是”,点击“下一步”5.复制"21471-07182-41807-00726-......
  • 26版SPSS操作教程(初级第七章)
    前言#由于导师最近布置了学习SPSS这款软件的任务,因此想来平台和大家一起交流下学习经验,这期推送内容接上一次第六章的学习笔记,希望能得到一些指正和帮助~#第七章一些学习笔记SPSS中集中趋势的描述指标(位置统计量)有算术均数(ArithmeticMean)、中位数(Median)、截尾均数(TrimmedM......
  • YoloV8改进策略:Block改进|ECA-Net:用于深度卷积神经网络的高效通道注意力|ECA+压缩膨胀
    摘要arxiv.org/pdf/1910.03151.pdf最近,通道注意机制已被证明在改善深度卷积神经网络(CNN)的性能方面具有巨大潜力。然而,大多数现有方法致力于开发更复杂的注意模块以实现更好的性能,这不可避免地会增加模型的复杂性。为了克服性能和复杂性折衷之间的矛盾,本文提出了一种有效......
  • SQL-Labs靶场“34-35”关通关教程
    君衍.一、34关POST单引号宽字节注入1、源码分析2、联合查询注入3、updatexml报错注入4、floor报错注入二、35关GET数字型报错注入1、源码分析2、联合查询注入3、updatexml报错注入4、floor报错注入SQL-Labs靶场通关教程:SQL注入第一课SQL注入思路基础SQL无......
  • [ROS 系列学习教程] rqt可视化工具箱 - 日志工具
    ROS系列学习教程(总目录)本文目录零、rqt可视化工具箱一、rqt_console二、rqt_logger_level零、rqt可视化工具箱rqt是ROS的一个软件框架,以插件的形式实现各种GUI工具。可以在rqt中将所有现有的GUI工具作为子窗口运行,也可以以独立方法运行,但rqt可以更轻松地同......
  • AlpineTerm使用教程
    AlpineTerm使用教程上一篇文章介绍了如何使用termux和qemu来搭建alpine虚拟机进而使用docker,而alpineterm是GitHub上面的大神做的封装,使用更加方便安装alpineterm可以从GitHub上面进行下载,由于许久没有更新,请下载最新release即可,大概500MB左右使用升级内核并更新系统通过......
  • RTL8812au网卡安装驱动教程
    购买网卡:https://item.jd.com/10083411831503.html使用说明上没有Linux系统的驱动安装教程,于是根据使用说明上面的下载链接下载了驱动的安装教程和驱动(网卡盒子里面有光盘,但是我没有用),但是下载出的驱动层层套娃,最后使用了Ubuntu12.04才安装完成下面是具体教程:首先,你需要安装......
  • .net core 实例教程(一)新建项目
    本文源码下载地址:http://www.80cxy.com/Blog/ResourceView?arId=202403191532545995NAAqJh系列教程地址:http://www.80cxy.com/Blog/ArticleView?arId=202403191517574161ay3s5V本实例教程将开发一个角色管理系统。主要包http://www.80cxy.com/Blog/ArticleView?arId=20240319151......
  • 基于springboot的线上买菜系统(含源码+sql+视频导入教程)
    ......
  • 最详细爬虫零基础教程08——代理IP
    文章目录一、代理IP二、使用步骤三、小案例总结一、代理IP在Python中使用代理IP进行爬虫是一种常见的技术手段,可以在一定程度上解决被网站限制访问的问题。为什么要使用代理IP1.让服务器以为不是同一个客户端在请求2.防止我们的真实IP被泄露被追究使用场景:1.被......