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()
相关推荐
Carl_奕然2 小时前
【机器视觉】最强算法之:3D高斯泼溅(3D Gaussian Splatting)
人工智能·python·算法·目标检测·计算机视觉·3d
代码山河3 小时前
OpenCV进阶:鼠标事件的回调函数使用方法
目标检测·计算机视觉
≮傷£≯√4 小时前
OpenCV 边缘检测 Canny
人工智能·opencv·计算机视觉
怎么全是重名5 小时前
CLIP-Learning Transferable Visual Models From Natural Language Supervision
人工智能·深度学习·计算机视觉·异常检测
yiyideda6 小时前
高效的医疗票据OCR+查验怎么做?字段补全、可信标注与查验返回结构
计算机视觉·ocr·医疗票据识别·医疗ocr·医疗发票查验·项目审核
shoufeipin90hou7 小时前
同色异料怎么分:可见光、近红外、高光谱三条技术路线的选型边界
计算机视觉
陈嘿萌8 小时前
NeurIPS | 3EED:车辆、无人机、机器狗谁更会找东西?在三维空间中定位各处万物!
人工智能·深度学习·计算机视觉·学术研究与理论基础
≮傷£≯√8 小时前
opencv 图像处理(一)
图像处理·人工智能·opencv
qq_25294131689 小时前
混凝土损伤分割与检测数据集 | 混凝土损伤 实例分割 裂缝检测 裸露钢筋 结构健康监测9261期
人工智能·yolo·目标检测·计算机视觉·混泥土
sali-tec9 小时前
C# 基于OpenCv的视觉工作流-章114-视觉与http通讯
图像处理·人工智能·opencv·计算机视觉·c#