YOLOv8基础必需运用【目标检测、分割、姿势估计、跟踪和分类任务】

news/2024/7/9 23:36:00 标签: YOLO, 目标检测, 分类

文章目录

  • 前言
  • 1、环境安装
    • 2.1安装torch相关库
    • 2.2 获取yolov8最新版本,并安装依赖
  • 3. 如何使用模型用于各种CV任务
    • 方式一:命令行形式
    • 方式二:python代码形式示例
    • 3.1 目标检测任务实现
      • 代码
      • 运行结果
      • 检测视频代码
    • 3.2 分割任务实现
      • 代码
      • 运行效果
      • 分割视频代码
    • 3.3 追踪任务
      • 代码
    • 3.4 姿态检测任务
      • 代码
      • 运行结果
      • 姿态检测视频代码


前言

YOLOv8是Ultralytics的最新版本YOLO。作为最先进的 SOTA 模型,YOLOv8 建立在以前版本成功的基础上,引入了新功能和改进,以增强性能、灵活性和效率。YOLOv8 支持全方位的视觉 AI 任务,包括检测、分割、姿势估计、跟踪和分类。这种多功能性使用户能够在不同的应用程序和域中利用YOLOv8的功能。模型中只需要设定不同的训练模型,就可以得到不同的检测结果

1、环境安装

2.1安装torch相关库

官网地址:https://pytorch.org/get-started/locally

安装命令:(根据自己电脑选型)

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

在这里插入图片描述

2.2 获取yolov8最新版本,并安装依赖

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

通过克隆仓库到本地来获得yolov8最新版本。
在这里插入图片描述

安装依赖环境

pip install ultralytics

3. 如何使用模型用于各种CV任务

安装好yolov8需要的运行环境之后,就可以直接使用模型了,目前,该模型可以直接用于检测、分割、姿势估计、跟踪和分类这5类检测任务。
支持两种运行方式:

方式一:命令行形式

yolo TASK MODE ARGS

举例: yolo detect predict model=yolov8n.pt source='test.jpg'
表示对图片test.jpg进行目标检测

参数说明:

TASK (optional) 表示模型任务类型,可以从列表 [detect, segment, classify, pose]选一个.
分别代表检测、分割、分类、姿态检测任务 MODE (required) 表示模型需要执行的操作,可以从列表 [train, val,
predict, export, track, benchmark]选一个,分别代表训练、验证、预测、模型转换、追踪、基准模型评估 ARGS
(optional) 表示其他的一些参数设置,后面详细说明

方式二:python代码形式示例

from ultralytics import YOLO

# 加载预训练模型,
model = YOLO('yolov8n.pt',task='detect')
# yolov8n.pt表示预训练模型
# task代表需要执行的任务:detect, segment, classify, pose
# 使用模型进行指定任务
results = model(ARGS)

3.1 目标检测任务实现

代码

from ultralytics import YOLO
import cv2
# 加载预训练模型
model = YOLO("yolov8n.pt", task='detect') 
# model = YOLO("yolov8n.pt") task参数也可以不填写,它会根据模型去识别相应任务类别
# 检测图片
results = model("./ultralytics/assets/bus.jpg")
res = results[0].plot()
cv2.imshow("YOLOv8 Inference", res)
cv2.waitKey(0)

运行结果

在这里插入图片描述

检测视频代码

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
print('111')
# Open the video file
video_path = "1.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 inference on the frame
        results = model(frame)

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

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

在这里插入图片描述

3.2 分割任务实现

代码

from ultralytics import YOLO
import cv2
# Load a model
model = YOLO('yolov8n-seg.pt')  
# Predict with the model
results = model('./ultralytics/assets/bus.jpg')  # predict on an image
res = results[0].plot(boxes=False) #boxes=False表示不展示预测框,True表示同时展示预测框
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", res)
cv2.waitKey(0)

运行效果

在这里插入图片描述

分割视频代码

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO('yolov8n-seg.pt', task='segment')
# Open the video file
video_path = "1.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 inference on the frame
        results = model(frame)

        # Visualize the results on the frame
        annotated_frame = results[0].plot()
        # annotated_frame = results[0].plot(boxes=False)不显示预测框

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

在这里插入图片描述

3.3 追踪任务

代码如下:【与目标检测不同的是,每个物体有一个ID。】

代码

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt',task='detect')  
# model = YOLO('yolov8n-seg.pt')  

# Track with the model
results = model.track(source="1.mp4", show=True)

在这里插入图片描述

3.4 姿态检测任务

代码

from ultralytics import YOLO
import cv2
# Load a model
model = YOLO('yolov8n-pose.pt')  # load a pretrained model (recommended for training)
results = model('./ultralytics/assets/bus.jpg')
res = results[0].plot()
cv2.imshow("YOLOv8 Inference", res)
cv2.waitKey(0)

运行结果

姿态检测视频代码

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO('yolov8n-pose.pt', task='pose')
# Open the video file
video_path = "1.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 inference on the frame
        results = model(frame)

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

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

在这里插入图片描述


http://www.niftyadmin.cn/n/5411770.html

相关文章

2024 年 AI 辅助研发趋势:从研发数字化到 AI + 开发工具 2.0,不止于 Copilot

在上一年里,已经有不少的企业在工具链上落地了生成式 AI,结合我们对于这些企业的分析,以及最近在国内的一些 “新技术” 趋势,诸如于鸿蒙原生应用的初步兴起。从这些案例与趋势中,我们也看到了一些新的可能方向。 结合…

ArmSoM规划开发基于RK3576的开发套件

ArmSoM正计划推出一款新的产品,这款产品将采用强大的RK3576芯片。 本文将为您介绍我们的新产品搭载的RK3576性能参数,以及它如何为您提供卓越的性能和功能。 RK3576处理器 RK3576处理器是一款强大的处理器,具备出色的性能和多样化的功能&a…

大模型交互-超拟人合成

1、超拟人合成:将文字转化为自然流畅的人声,在实时语音合成的基础上,精准模拟人类的副语言现象,如呼吸、叹气、语速变化等,使得语音不仅流畅自然,更富有情感和生命力。 2、唤醒的持久运行--->合成能力加…

数据库中on条件与where条件的区别

数据库中on条件与where条件的区别 标签:数据库 mysql> SELECT e.empno,ename,e.deptno,e.is_deleted FROM emp_test e ; ---------------------------------- | empno | ename | deptno | is_deleted | ---------------------------------- | 1 | 张三 | 1 | …

曲线曲面 - 连续性, 坐标变换矩阵

连续性 有两种:参数连续性(Parametric Continuity)、几何连续性(Geometric Continuity)参数连续性: 零阶参数连续性,记为,指相邻两段曲线在结合点处具有相同的坐标 一阶参数连续性&…

【React】react的生命周期

react的生命周期 一、新生命周期1、挂载阶段1.1 constructor(1)在React组件挂载之前被调用(2) 初始化函数内部 state或者在this上挂载方法 1.2 getDerivedStateFromProps(1)为静态方法,不能访问…

Docker Register 搭建私有镜像仓库

1 安装 docker (1)更新软件源 sudo apt update (2)安装 docker 组件 sudo apt install docker.io (3)启动 docker 服务 sudo systemctl start docker (4)设置 docker 服务开机自启动 sudo systemctl enable docker (5)验证 docker 功能 sudo docker contai…

物联网安全|TrustAsia助力PSWG应对全球物联网产品安全合规挑战

万物互联时代,随着物联网连接数快速增长,物联网设备的潜在网络安全隐患也日益增长,可能导致设备故障、数据被盗、篡改、隐私泄露等问题的发生,甚至成为网络攻击的跳板,对互联网基础设施构成严重威胁。 我们看到&#…