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()
相关推荐
QQ676580088 小时前
基于深度学习YOLO的苹果采摘点图像识别 苹果枝条分割识别 苹果分割检测 苹果茎叶分割识别 果园自动化采摘设备目标识别算法第10386期
深度学习·yolo·自动化·苹果采摘点图像·苹果枝条分割·苹果茎叶分割·果园自动化采摘设备
Coovally AI模型快速验证9 小时前
IEEE IoT-J | CoDrone:Depth Anything V2+VLM云边端协同,无人机自主导航飞行距离+40%
人工智能·物联网·计算机视觉·无人机
Westward-sun.9 小时前
OpenCV + dlib 人脸关键点检测学习笔记(68点)
人工智能·笔记·opencv·学习·计算机视觉
就是有点傻10 小时前
机器视觉图像处理学习第一天
人工智能·计算机视觉
QQ6765800811 小时前
基于YOLO26算法的智慧农业橙子图像识别 橙子采摘识别 水果采摘识别 高清采摘过程图像识别 YOLO+voc个数据集第10410期
算法·yolo·橙子采摘·水果采摘识别·高清采摘过程图像识别
木心术111 小时前
openclaw与Hermes的优劣势对比
人工智能·python·opencv·自动化
深度学习lover11 小时前
<数据集>yolo 瓶盖识别<目标检测>
人工智能·python·yolo·计算机视觉·瓶盖识别
sali-tec12 小时前
C# 基于OpenCv的视觉工作流-章50-霍夫找圆
图像处理·人工智能·opencv·算法·计算机视觉
乔公子搬砖12 小时前
告别识别率焦虑:视频 AI 工程化实战 —— 检测→判定→聚合→治理全链路拆解
人工智能·yolo·决策树·计算机视觉·视觉检测
预见AI13 小时前
C#索引器练习题
开发语言·计算机视觉·c#