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()
相关推荐
千宇宙航3 小时前
闲庭信步使用图像验证平台加速FPGA的开发:第十四课——图像二值化的FPGA实现
图像处理·计算机视觉·fpga开发
橡晟3 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
PyAIExplorer4 小时前
图像亮度调整的简单实现
人工智能·计算机视觉
AI technophile5 小时前
OpenCV计算机视觉实战(15)——霍夫变换详解
人工智能·opencv·计算机视觉
JNU freshman6 小时前
计算机视觉 之 数字图像处理基础(一)
人工智能·计算机视觉
千宇宙航6 小时前
闲庭信步使用图像验证平台加速FPGA的开发:第十五课——基于sobel算子边缘检测的FPGA实现
图像处理·计算机视觉·fpga开发
Tony沈哲10 小时前
OpenCV 图像调色优化实录:从 forEach 到并行 + LUT 提速之路
opencv·算法
hans汉斯11 小时前
【计算机科学与应用】面向APT攻击调查的溯源图冗余结构压缩
网络·算法·安全·web安全·yolo·目标检测·图搜索算法
AndrewHZ12 小时前
【图像处理基石】什么是色盲仿真技术?
图像处理·人工智能·pytorch·深度学习·计算机视觉·颜色科学·hvs
Mikowoo00713 小时前
02_MAC_XCode配置OpenCV
opencv