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()
相关推荐
ZCXZ12385296a5 分钟前
黄瓜目标检测与识别_yolov8-seg-efficientViT实战应用
yolo·目标检测·目标跟踪
极智视界3 小时前
目标检测数据集 - 手持危险物检测数据集下载
yolo·目标检测·数据集·voc·coco·算法训练·手持危险物
吃人陈乐游刘3 小时前
10实战经验yolov5的部署(2026年01月)
yolo
saoys4 小时前
Opencv 学习笔记:图像金字塔实现上采样(pyrUp)与降采样(pyrDown)
笔记·opencv·学习
2501_941507944 小时前
【技术实践】基于YOLOv8与ConvNeXtV2的猫狗图像分类与目标检测系统详解
yolo·目标检测·分类
2501_905967335 小时前
双目视觉:CREStereo论文超详细解读
人工智能·python·计算机视觉·双目视觉
AI即插即用6 小时前
超分辨率重建(论文精读) | CVPR 2025 LSRNA:利用隐空间超分与噪声对齐,打破扩散模型生成 4K 图像的效率瓶颈
图像处理·人工智能·深度学习·计算机视觉·视觉检测·超分辨率重建
海天一色y6 小时前
基于CNN实现Mnist手写数字识别
人工智能·深度学习·计算机视觉
saoys7 小时前
Opencv 学习笔记:循环读取文件夹中图片并动态展示
笔记·opencv·学习