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()
相关推荐
system_error_x37 分钟前
YOLOv8 研究方向及价值:从目标检测到多任务视觉基座
yolo·目标检测·目标跟踪
陈嘿萌1 小时前
CVPR 2026 | 全新的五模态信息融合数据集,包括RGB、事件、热成像、4D 雷达与 LiDAR
人工智能·深度学习·计算机视觉·自动驾驶·学术研究与理论基础·点云雷达
晨间晓1 小时前
目标下一秒会往哪里走?我用 YOLO + ByteTrack 做了轨迹预测
yolo
陈嘿萌2 小时前
TPAMI 2026|红外、SAR、深度与地图能否用一套特征匹配?北理工HIMO给出无需预训练的通用路线
图像处理·人工智能·深度学习·计算机视觉·图像匹配·学术研究与理论基础
陈嘿萌3 小时前
TIP 2026 | SCAC:不用训练也能做视频时刻检索,让事件链与边界校准协同
图像处理·人工智能·深度学习·计算机视觉·学术研究与理论基础
LittroInno3 小时前
全景拼接中的重叠区域 视差与动态重影
图像处理·opencv·计算机视觉·图像配准·全景拼接
陈嘿萌16 小时前
ICML 2026|浙大 1D-Fusion:用 Tokenizer 将图像“压”成一维 Token,重构融合表示
图像处理·人工智能·计算机视觉·图像融合·计算机科学与技术·学术研究与理论基础·顶级期刊与会议
陈嘿萌18 小时前
TPAMI 2026|武大 DuS-DiFuse:图像融合从“拼信息”走向“补语义”,兼顾真实退化鲁棒性
图像处理·人工智能·计算机视觉·图像融合·计算机科学与技术·学术研究与理论基础·顶级期刊会议
陈嘿萌20 小时前
TIP 2026|没有分割标签,也能做任务导向图像融合:SSVIF的自监督语义注入
图像处理·人工智能·深度学习·计算机视觉·图像融合·学术研究与理论基础
一木 之林21 小时前
LangChain 第3章 消息与提示词模板:四类消息、历史管理与 ChatPromptTemplate
人工智能·计算机视觉·langchain