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()
相关推荐
三毛的二哥1 小时前
BEV:典型BEV算法总结
人工智能·算法·计算机视觉·3d
我是无敌小恐龙5 小时前
Java SE 零基础入门Day01 超详细笔记(开发前言+环境搭建+基础语法)
java·开发语言·人工智能·opencv·spring·机器学习
懷淰メ6 小时前
【AI加持】基于PyQt+YOLO+DeepSeek的口罩佩戴检测系统(详细介绍)
yolo·计算机视觉·pyqt·口罩检测·deepseek·ai加持
是梦终空8 小时前
计算机毕业设计271—基于python+深度学习+YOLOV7的车牌识别系统(源代码+数据库+3万字论文)
python·深度学习·opencv·yolo·毕业设计·pyqt5·车牌识别系统
程序员爱德华10 小时前
2026 计算机视觉研究方向
人工智能·计算机视觉
爱凤的小光10 小时前
OpenCV4机器学习算法原理与代码---个人学习篇
opencv·机器学习
思绪无限11 小时前
YOLOv5至YOLOv12升级:石头剪刀布手势识别系统的设计与实现(完整代码+界面+数据集项目)
深度学习·yolo·目标检测·yolov12·yolo全家桶·石头剪刀布手势识别系统
Techblog of HaoWANG11 小时前
目标检测与跟踪(15)-- conda 环境与 roslaunch 节点解释器不一致问题的排查与工程化修复
人工智能·目标检测·计算机视觉·机器人·conda
ComputerInBook12 小时前
数字图像处理(4版)——第 4 章——频域滤波(下)(Rafael C.Gonzalez&Richard E. Woods)
人工智能·算法·计算机视觉·频域滤波
思绪无限12 小时前
YOLOv5至YOLOv12升级:舰船检测与识别系统的设计与实现(完整代码+界面+数据集项目)
深度学习·yolo·目标检测·yolov12·yolo全家桶·舰船检测与识别