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()
相关推荐
yuan199977 小时前
PCA源码与可见光-红外图像融合MATLAB实现
图像处理·计算机视觉·matlab
sali-tec10 小时前
C# 基于OpenCv的视觉工作流-章60-点点距离
图像处理·人工智能·opencv·算法·计算机视觉
极智视界10 小时前
分类数据集 - 糖尿病视网膜病变检测图像分类数据集下载
人工智能·yolo·数据集·图像分类·算法训练·医学影像分析·糖尿病视网膜病变检测
guo_xiao_xiao_12 小时前
YOLOv11港口码头船只目标检测数据集-3766张-Boat-recognition-1
yolo·目标检测·目标跟踪
hhzz13 小时前
(深度学习/计算机视觉)手把手教你从零部署YOLOv8目标检测算法-----环境安装(1/4)
深度学习·yolo·计算机视觉
kay_54514 小时前
YOLO26改进| 主干网络 | 提升长距离特征建模与全局上下文理解能力【CVPR】
人工智能·目标检测·计算机视觉·目标跟踪·论文·yolo26·yolo26改进
老黄编程14 小时前
大型工地实时数据处理与三维重构系统方案(极简中心化部署版)
人工智能·数码相机·计算机视觉·大数据处理·入侵检测·三维重构
_李小白17 小时前
【android opencv学习笔记】Day 5: 高效的图像扫描
android·opencv·学习
开开心心就好1 天前
仅168KB的桌面图标自动隐藏工具
windows·计算机视觉·计算机外设·excel·启发式算法·宽度优先·csdn开发云
guo_xiao_xiao_1 天前
YOLOv11多场景生活与运动目标检测数据集-6703张-13-Merged-1
yolo·目标检测·生活