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()
相关推荐
W_3260014 小时前
Python-OpenCV图像像素与通道:通道拆分合并、深浅拷贝
图像处理·人工智能·python·opencv·机器学习
欧特克_Glodon15 小时前
OpenCV计算机视觉开发入门与实践<十一>:矩阵的复制和矩形类Rect
c++·opencv·计算机视觉·矩阵
雪的季节16 小时前
OPENCV学习(补充1)
人工智能·opencv·学习
深圳雨林凯AI16 小时前
无缝平铺的图像处理原理:边缘匹配、重复单元与密度控制|雨林凯AI 四方连图
图像处理·人工智能·计算机视觉
W_3260020 小时前
Python-OpenCV图像滤波:卷积、高斯、中值、双边美颜滤波
开发语言·python·opencv
AndrewHZ21 小时前
图像处理入门008 | 阶段总结:环境测试与基础概念测验
图像处理·python·opencv·计算机视觉·cv
jay神1 天前
深度学习为什么需要反向传播
人工智能·深度学习·yolo·目标检测·cnn·毕业设计
汐ya~1 天前
【PageEyes Agent 跨端 UI 自动化】开源Pydantic AI、OmniParser 多源感知的UI 自动化Agent
计算机视觉·agent·ui自动化·github开源
动物园猫1 天前
猪危险行为目标检测数据集:3类别、5,000+张图像 | 目标检测
人工智能·目标检测·计算机视觉
YOLO数据集集合2 天前
一站式AI数据自动化标注与训练平台:零门槛玩转YOLO全系列模型
人工智能·深度学习·yolo·ai·自动化·数据集·标注软件