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()
相关推荐
fai厅的秃头姐!33 分钟前
OpenCV实战篇
人工智能·opencv·计算机视觉
厚皮龙16 小时前
OpenCV 版本导致 AprilTag 检测数量不同
人工智能·opencv·webpack
zzm62818 小时前
Focal Loss与RetinaNet:解决目标检测中类别不平衡的新型损失函数
人工智能·目标检测·计算机视觉
m沐沐1 天前
【机器学习】朴素贝叶斯算法:从贝叶斯定理到手写数字识别实战
人工智能·深度学习·算法·机器学习·计算机视觉·手写数字·数字识别
梦想三三1 天前
YOLOv5口罩检测完整实战(二):从现成数据集到训练、评估与摄像头识别
人工智能·yolo·计算机视觉·目标跟踪
c#上位机1 天前
线扫相机之采集图像高度
数码相机·opencv·计算机视觉
Microvision维视智造1 天前
行业洞察系列 06 | 汽车制造的视觉革命
人工智能·计算机视觉·汽车·视觉检测·制造·机器视觉
小保CPP2 天前
OpenCV C++基于CNN模型的场景文本检测(OCR)
c++·人工智能·opencv·计算机视觉·cnn·ocr·光学字符识别
sali-tec2 天前
C# 基于OpenCv的视觉工作流-章99-换背景
图像处理·人工智能·opencv·算法·计算机视觉
念啊啊啊啊丶2 天前
【工业异常异常检测】2026-ICML-CoGeoAD:基于多视图注意力机制的分层颜色几何融合零样本3D异常检测
人工智能·深度学习·神经网络·机器学习·计算机视觉