使用 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()
相关推荐
Jerryhut16 小时前
opencv对齐算法及其应用
人工智能·opencv·算法
热心不起来的市民小周17 小时前
100种动物语义分割数据集(A100-Seg)
python·深度学习·计算机视觉
stsdddd17 小时前
YOLO系列目标检测数据集大全【第二十五期】
yolo·目标检测·目标跟踪
盼小辉丶18 小时前
OpenCV-Python实战(28)——OpenCV计算摄影从HDR图像融合到全景拼接
python·opencv·计算机视觉
Deitymoon18 小时前
RV1126——交叉编译 SDL_TTF库并渲染文字
计算机视觉·音视频·rv1126·osd
西西弗Sisyphus1 天前
YOLO26 自定义损失函数 分类任务自定义损失的接口约定
yolo·yolo26
埃科光电1 天前
应用分享丨告别测量盲区!锂电极片刻槽检测新方案
图像处理·计算机视觉·视觉检测·相机
code_pgf1 天前
mllm训练过程中有效地利用辅助监督信号来减少幻觉的方法
人工智能·深度学习·计算机视觉
stsdddd1 天前
YOLO系列目标检测数据集大全【第二十二期】
yolo·目标检测·目标跟踪
armwind1 天前
openISP学习11-BNF — Bilateral Noise Filtering(双边滤波降噪)
图像处理·计算机视觉