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()
相关推荐
鬼手点金2 小时前
Claude Code示范案例-进阶学习路径
java·服务器·前端·学习·计算机视觉·前向传播
闭包不眠2 小时前
端侧AI能省多少服务器钱:把账换成字节算
运维·服务器·图像处理·人工智能·计算机视觉
AomanHao3 小时前
【阅读笔记】Weighted Guided Image Filtering(WGIF)权重引导滤波
图像处理·人工智能·笔记·计算机视觉·滤波·引导滤波
李航198314 小时前
给自己的图形引擎,配上了AI渲染,做设计真是太方便了
人工智能·python·计算机视觉·ai·ai编程
Zentceh15 小时前
安防监控夜视方案2026:AI全彩+边缘计算+云平台
图像处理·人工智能·科技·计算机视觉·车载系统·无人机·边缘计算
YOLO数据集集合17 小时前
风机叶片表面损伤检测数据集 | 风机叶片 表面损伤 污渍检测 无人机巡检 风电运维9119期
运维·人工智能·计算机视觉·目标跟踪·无人机·智慧城市·电力巡检
小蒋观天下19 小时前
行业拆解|两轮车检测AI摄像头的核心技术迭代、产品演进及未来技术发展
大数据·人工智能·安全·计算机视觉·ai大模型
鬼手点金20 小时前
Claude Code示范案例-常用快捷命令
java·服务器·前端·计算机视觉·前向传播
兴通物联科技1 天前
对俄出口诚实标识 2026 时间节点与产线赋码采集技术方案
大数据·服务器·单片机·嵌入式硬件·深度学习·计算机视觉
欧特克_Glodon1 天前
OpenCV计算机视觉开发入门与实践(基于C++):专栏内容介绍及目录
c++·人工智能·opencv·计算机视觉