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()
相关推荐
YOLO数据集集合27 分钟前
无人船视角水面目标检测数据集 |无人船 USV 水面目标检测 自主航行 避障 水域监测 目标检测9076期
深度学习·yolo·目标检测·无人机·无人机视角·无人船·自主航线
YOLO数据集集合38 分钟前
渔船船只检测数据集 | 船只检测 渔船识别 拖船检测 海事监管 目标检测 YOLO格式 深度学习数据集 计算机视觉9076期
人工智能·深度学习·yolo·目标检测·计算机视觉
AI人工智能+42 分钟前
了一种融合OCR与大模型的文档抽取系统,解决传统OCR“识字不识意”的痛点
深度学习·计算机视觉·语言模型·自然语言处理·ocr·文档抽取
Tp_jh10 小时前
AMD 395本地部署Qwen3.8-Flash-Next实测,端侧AGI时代也要来了
图像处理·人工智能·opencv·语言模型·自然语言处理·千问
民乐团扒谱机13 小时前
【一文吃透】图像灰度非线性拉伸:对数、Gamma、S型曲线与直方图均衡化公式详解与避坑
人工智能·计算机视觉
陈嘿萌15 小时前
BeyondFusion:无需显式配准,统一完成「红外超分」与红外-可见光融合
计算机视觉·图像融合·学术研究与理论基础
imbackneverdie16 小时前
论文中的机制图、原理图用什么软件画?AI生成可编辑矢量图的方法分享
人工智能·深度学习·计算机视觉·aigc·科研·ai绘图·科研绘图
yyk3332419 小时前
计算机视觉OpenCV中的FisherFace人脸识别
人工智能·opencv·计算机视觉
AI吃大瓜19 小时前
人脸检测和行人检测4:Android实现YOLOv8 YOLO11 YOLO26人脸检测和人体检测(含源码,可实时检测)
android·yolo·人脸检测·人体检测·行人检测·yolo26
Zentceh20 小时前
AI-ISP vs 传统ISP全面对比
人工智能·深度学习·计算机视觉·车载系统·transformer·无人机·智能硬件