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()
相关推荐
Daydream.V40 分钟前
OpenCV高端操作——光流估计(附案例)
人工智能·opencv·计算机视觉
映辉5 小时前
ultralytics yolo入门实践
yolo·ai
JicasdC123asd6 小时前
SimAM无参数注意力机制改进YOLOv26神经科学启发的自适应特征增强突破
人工智能·yolo·目标跟踪
gorgeous(๑>؂<๑)6 小时前
【CVPR26-索尼】EW-DETR:通过增量低秩检测Transformer实现动态世界目标检测
人工智能·深度学习·目标检测·计算机视觉·transformer
格林威6 小时前
Baumer相机锂电池极片裁切毛刺检测:防止内部短路的 5 个核心方法,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·视觉检测
xinxiangwangzhi_7 小时前
立体匹配--GCNet(2017)
计算机视觉
bryant_meng8 小时前
【AI】《Explainable Machine Learning》(2)
人工智能·深度学习·机器学习·计算机视觉·explanation
xinxiangwangzhi_10 小时前
立体匹配--Dispnet(2016)
计算机视觉
章鱼丸-11 小时前
DAY 42 Grad-CAM 与 Hook 函数
pytorch·深度学习·计算机视觉
这张生成的图像能检测吗11 小时前
(论文速读)FDGLM:面向多场景工业故障诊断的深度数字双动力大视觉语言模型
人工智能·深度学习·计算机视觉·故障诊断·视觉语言大模型·问答模型