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()
相关推荐
欧特克_Glodon1 天前
OpenCV计算机视觉开发入门与实践<二十七>:图像分割概述
c++·人工智能·opencv·计算机视觉
hahaha60161 天前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
qq_25294131682 天前
航拍羊群目标检测数据集 | 航拍羊群 智慧畜牧业 动物检测 无人机巡检5010期
人工智能·yolo·目标检测·计算机视觉·无人机·羊群识别·羊群
yyk333242 天前
OpenCV中运动目标检测
opencv·目标检测·目标跟踪
AndrewHZ2 天前
图像处理入门021 | 直方图:图像像素分布的可视化 —— cv2.calcHist 与 4 类典型分布
图像处理·opencv·直方图·直方图均衡化·cdf·灰度直方图·多通道直方图
音视频牛哥2 天前
拆解人形机器人:从关节、灵巧手到VLA与媒体神经系统
机器学习·计算机视觉·机器人
爱吃火鸡面呀2 天前
OpenCV 实战:文档扫描与神经风格迁移完整指南
人工智能·opencv·计算机视觉
java1234_小锋2 天前
YOLO26 计算机视觉 - 训练自己的 YOLO26 模型
人工智能·yolo·机器学习·计算机视觉·yolo26
Hello-FPGA2 天前
量子计算滨松c15550-22up单光子相机与PCIe1004采集卡
人工智能·计算机视觉·fpga开发
梦想的颜色2 天前
【AI科普】什么是计算机视觉:硬核科普,它和大 AI 大模型到底是什么关系
人工智能·深度学习·计算机视觉·多模态·aiagent·#vlm·ai工程实战