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()
相关推荐
I'm a winner7 分钟前
基于YOLO算法的医疗应用专题:第一章 计算机视觉与深度学习概述
算法·yolo·计算机视觉
这张生成的图像能检测吗2 小时前
(论文速读)开放词汇3D场景理解的掩蔽点-实体对比
人工智能·计算机视觉·图像生成·1024程序员节·开放词汇·3d重建
Antonio9153 小时前
【图像处理】灰度图像与二值化
图像处理·opencv
tangchen。4 小时前
YOLOv4 :兼顾速度与精度!
人工智能·计算机视觉·目标跟踪
学术头条4 小时前
用视觉压缩文本!清华、智谱推出Glyph框架:通过视觉-文本压缩扩展上下文窗口
人工智能·深度学习·计算机视觉
Mrliu__5 小时前
Opencv(一): 用Opencv了解图像
人工智能·opencv·计算机视觉
m0_650108245 小时前
【论文精读】Animate Anyone:实现角色动画的一致性与可控性图像到视频合成
计算机视觉·扩散模型·论文精读·图像到视频合成·角色动画·姿态引导器·可控生成
B站_计算机毕业设计之家6 小时前
基于python人脸识别系统 人脸检测 实时检测 深度学习 Dlib库 ResNet深度卷积神经网络 pyqt设计 大数据(源码)✅
python·深度学习·目标检测·计算机视觉·信息可视化·人脸识别·1024程序员节
Theodore_10227 小时前
机器学习(9)正则化
人工智能·深度学习·机器学习·计算机视觉·线性回归·1024程序员节
yanxing.D9 小时前
penCV轻松入门_面向python(第七章 图像平滑处理)
图像处理·人工智能·opencv·计算机视觉