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()
相关推荐
一勺汤42 分钟前
YOLO12 改进、魔改|秩增强线性注意力RALA,通过增强 KV 缓冲与输出特征的矩阵秩,增强 YOLO 对小目标、复杂场景目标的识别能力
线性代数·yolo·矩阵·yolov12·yolo12·yolo12改进·小目标
再__努力1点1 小时前
【11】特征检测与匹配:AKAZE特征算法详解与实现
人工智能·python·opencv·算法·计算机视觉·特征提取
深蓝海拓2 小时前
YOLO v11的学习记录(五) 使用自定义数据从头训练一个实例分割的模型
学习·yolo
张人玉8 小时前
Cognex VisionPro 相机工具集成代码分析笔记
数码相机·计算机视觉·vsionpro
AI浩10 小时前
VSSD:具有非因果状态空间对偶性的视觉Mamba模型
人工智能·目标检测·计算机视觉
深度学习lover12 小时前
<数据集>yolo航拍斑马线识别数据集<目标检测>
人工智能·深度学习·yolo·目标检测·计算机视觉·数据集·航拍斑马线识别
这张生成的图像能检测吗13 小时前
(论文速读)AIMV2:一种基于多模态自回归预训练的大规模视觉编码器方法
人工智能·计算机视觉·预训练·视觉语言模型
AndrewHZ14 小时前
【图像处理基石】 怎么让图片变成波普风?
图像处理·算法·计算机视觉·风格迁移·cv
CV实验室15 小时前
AAAI 2026 Oral 之江实验室等提出MoEGCL:在6大基准数据集上刷新SOTA,聚类准确率最高提升超8%!
人工智能·机器学习·计算机视觉·数据挖掘·论文·聚类
Sunhen_Qiletian16 小时前
YOLOv2算法详解(上篇):从经典到进化的目标检测之路
算法·yolo·目标检测