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()
相关推荐
技术支持者python,php2 小时前
物体识别:分类器模型
人工智能·opencv·计算机视觉
雍凉明月夜2 小时前
视觉opencv学习笔记Ⅳ
笔记·opencv·学习·计算机视觉
提娜米苏3 小时前
[论文笔记] ASR is all you need: Cross-modal distillation for lip reading (2020)
论文阅读·深度学习·计算机视觉·语音识别·知识蒸馏·唇语识别
Coding茶水间3 小时前
基于深度学习的西红柿成熟度检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
Bdygsl4 小时前
数字图像处理总结 Day 2 —— 数字化
图像处理·人工智能·计算机视觉
散峰而望4 小时前
AI 知识科普
人工智能·python·深度学习·机器学习·计算机视觉
棒棒的皮皮4 小时前
【OpenCV】Python图像处理之图像表示方法
图像处理·python·opencv
棒棒的皮皮4 小时前
【OpenCV】Python图像处理之通道拆分与合并
图像处理·python·opencv·计算机视觉
CoovallyAIHub4 小时前
为什么企业如今不应该忽视计算机视觉?计算机视觉如何为企业降本增效、规避风险?
深度学习·算法·计算机视觉
才思喷涌的小书虫4 小时前
实战教程:从 0 到 1 手搓 DINO-X 定制模板,实现长尾场景精准检测和数据标注
人工智能·目标检测·计算机视觉·具身智能·数据标注·图像标注·模型定制