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()
相关推荐
scott19851232 分钟前
DIFIX3D+: Improving 3D Reconstructions with Single-Step Diffusion Models
人工智能·计算机视觉·扩散模型·生成式
Julyers1 小时前
【Paper】FRST(快速径向对称变换)算法
图像处理·人工智能·计算机视觉·圆检测
dazzle1 小时前
计算机视觉处理(OpenCV基础教学(十七):图像轮廓检测技术详解)
人工智能·opencv·计算机视觉
qq_526099132 小时前
机器视觉网卡的全面选型指南
数码相机·计算机视觉·自动化
逸俊晨晖3 小时前
昇腾310P算力卡 10路1080p实时YOLOv8目标检测
人工智能·yolo·目标检测·昇腾
寻找华年的锦瑟4 小时前
Qt-YOLO-OpenCV
qt·opencv·yolo
zl_vslam4 小时前
SLAM中的非线性优-3D图优化之地平面约束(十五)
人工智能·算法·计算机视觉·3d
AI浩4 小时前
MFDA-YOLO:一种用于无人机小目标检测的多尺度特征融合与动态对齐网络
yolo·目标检测·无人机
_codemonster4 小时前
计算机视觉入门到实战系列(六)边缘检测sobel算子
人工智能·计算机视觉
热心不起来的市民小周4 小时前
测测你的牌:基于 MobileNetV2 的车牌内容检测
python·深度学习·计算机视觉