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