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()
相关推荐
CoovallyAIHub2 小时前
基于SimCLR的自监督 YOLO:YOLOv5/8也能在低标注场景目标检测性能飙升
深度学习·算法·计算机视觉
youngfengying7 小时前
《轻量化 Transformers:开启计算机视觉新篇》
人工智能·计算机视觉
jay神11 小时前
【原创】基于YOLO模型的手势识别系统
深度学习·yolo·计算机·毕业设计·软件设计与开发
思通数科人工智能大模型12 小时前
零售场景下的数智店商:解决盗损问题,化解隐性成本痛点
人工智能·目标检测·计算机视觉·数据挖掘·知识图谱·零售
萧鼎12 小时前
Python Mahotas 图像处理库:高性能计算机视觉工具
图像处理·python·计算机视觉
irisMoon0613 小时前
yolov5单目测距+速度测量+目标跟踪
人工智能·yolo·目标跟踪
nnn__nnn14 小时前
卷积神经网络经典架构全景解析:从 ILSVRC 竞赛到视觉技术的生态级演进
计算机视觉·架构·cnn
GISer_Jing15 小时前
OpenCV头文件路径配置终极修复指南
javascript·opencv·webpack
FL162386312919 小时前
智慧交通自动驾驶场景道路异常检测数据集VOC+YOLO格式8302张6类别
人工智能·yolo·自动驾驶
全球通史19 小时前
[特殊字符] RISC-V实战:从0到100+FPS!进迭时空(Spacemit)开发板YOLOv8部署终极指南
嵌入式硬件·yolo·risc-v