YOLOV8最简图像分类检测推理代码

安装YOLOV8

首先要去YOLOV8的官网安装库
YOLOV8官方网站

powershell 复制代码
# Install the ultralytics package from PyPI
pip install ultralytics

安装opencv

powershell 复制代码
pip install opencv-python
python 复制代码
import cv2
from ultralytics import YOLO

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

# Open the video file
video_path = 0
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()

更短的

python 复制代码
import cv2
from ultralytics import YOLO

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

# Open the video file
video_path = 0
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, show=True)

        # 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()
相关推荐
hans汉斯10 小时前
基于改进交叉熵损失函数与Transformer的心电信号高风险分类研究
功能测试·深度学习·算法·yolo·目标检测·分类·transformer
动物园猫12 小时前
牛行为目标检测数据集:4类别 | 目标检测
人工智能·目标检测·计算机视觉
棱镜研途17 小时前
【EI检索会议 | SPIE出版】 2026年智能计算与多模态信号处理国际学术会议(CIMSP 2026)
图像处理·算法·计算机视觉·信号处理·多模态·学术会议·智能计算
暂未成功人士!18 小时前
基于 YOLOv8 的端到端一体式二次分类检测实现思路
yolo·目标检测·分类
云空19 小时前
《洪水应急场景下基于 YOLOv8 的目标检测与实例分割模型训练方案》
人工智能·yolo·计算机视觉
2601_9628517421 小时前
计算机毕业设计之基于YOLOV10的低光照目标检测算法研究与实现
大数据·算法·yolo·目标检测·信息可视化·cnn·课程设计
懂AI的老郑1 天前
YOLOv26联手大模型,开启智能视觉新时代
人工智能·yolo·目标跟踪
画中有画1 天前
使用yolov8实现自动化脚本
运维·yolo·自动化
动物园猫2 天前
车位与车牌目标检测数据集:4类别 | 目标检测
人工智能·目标检测·计算机视觉
西西弗Sisyphus2 天前
计算机视觉领域 选择 卷积神经网络 还是选择 Vision Transformer
计算机视觉·cnn·transformer