使用 ultralytics 摄像头yolo推理

使用 ultralytics 摄像头yolo推理

官方网站
https://docs.ultralytics.com/

github
https://github.com/ultralytics/ultralytics

搭建环境

bash 复制代码
# Install the ultralytics package using conda
conda install -c conda-forge ultralytics

linux下摄像头推理

python 复制代码
import cv2
from ultralytics import YOLO
import time

# 加载轻量级模型,并指定较小的图像尺寸以提高速度
model = YOLO('yolov8n.pt')  # 或者根据实际情况选择其他轻量模型
IMG_SIZE = 320  # 调整输入图像尺寸

cap = cv2.VideoCapture(0)

prev_time = 0
curr_time = 0
fps = 0

while True:
    ret, frame = cap.read()
    if not ret:
        print("未能成功获取视频帧,退出...")
        break
    
    # 缩放图像以减小推理负担
    frame = cv2.resize(frame, (IMG_SIZE, IMG_SIZE))
    
    curr_time = time.time()
    fps = 1 / (curr_time - prev_time)
    prev_time = curr_time
    
    results = model(frame)
    
    annotated_frame = results[0].plot()
    cv2.putText(annotated_frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    
    cv2.imshow('YOLO Real-Time Detection with FPS', annotated_frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
相关推荐
深度学习lover1 小时前
<数据集>yolo 交通违规标志识别<目标检测>
人工智能·深度学习·yolo·目标检测·计算机视觉·交通违规标志识别
3D探路人2 小时前
模灵 大模型聚合API 转发流程技术实现
java·大数据·开发语言·前端·人工智能·计算机视觉
Ares-Wang2 小时前
图像》》仿射变换和透视变换放 、图像分割、目标检测
人工智能·计算机视觉
大空大地20263 小时前
# C#基础语法总结
人工智能·计算机视觉
霍夫曼vx_helloworld73525 小时前
字符提取与字符识别
图像处理·人工智能·计算机视觉
大江东去浪淘尽千古风流人物6 小时前
【MAGS-SLAM】纯单目多智能体Gaussian SLAM:Sim(3)位姿图优化与占用感知融合深度解析
人工智能·目标检测·计算机视觉
Hua-Jay7 小时前
OpenCV联合C++/Qt 学习笔记(十七)----凸包检测、直线检测及点集拟合
c++·笔记·qt·opencv·学习·计算机视觉
ComputerInBook8 小时前
数字图像处理——倍频(octave)图像
人工智能·深度学习·计算机视觉·倍频图像
人月神话-Lee8 小时前
【图像处理】颜色科学与灰度化——人眼看到的和数字记录的不一样
图像处理·人工智能·计算机视觉·ios·swift
Akttt9 小时前
Evaluating Object Hallucination in Large Vision-Language Models
人工智能·深度学习·计算机视觉