Hotdry.
ai-systems

使用 Coral TPU/TensorRT 构建 Frigate 实时本地对象检测 NVR

基于 Frigate 开源框架,利用边缘 ML 在 IP 摄像头流上实现低延迟对象检测、运动蒙版过滤与事件剪辑的完整工程配置与优化参数。

Frigate 是一个专为 IP 摄像头设计的开源 NVR(网络视频录像机),其核心优势在于实时本地对象检测。通过低开销运动检测仅在必要区域触发边缘 ML 推理(如 Coral TPU 或 TensorRT),实现多路流的高效处理,避免云端依赖,确保低延迟事件剪辑与隐私安全。这种架构特别适合家庭或企业构建 scalable 安防系统,能在边缘设备上处理 100+ FPS 检测帧。

Frigate 的高效源于多进程设计与硬件加速集成。从 GitHub 仓库描述:“Uses a very low overhead motion detection to determine where to run object detection.” 运动检测先过滤无关帧,仅对感兴趣区域运行 TensorFlow/OpenCV 对象检测,支持 Coral Edge TPU(8-15ms / 帧,支持 8-15 路摄像头)或 NVIDIA TensorRT(高性能 GPU 推理)。实际测试显示,Coral USB 版功耗仅 2-5W,远优于 CPU(50-200ms / 帧)。结合 FFmpeg RTSP 输入与 WebRTC 直播,端到端延迟控制在 200ms 内。

要落地部署,首先准备硬件:推荐 Intel NUC 或 mini PC + Coral TPU USB/PCIe($60-100),或 NVIDIA Jetson/GPU 支持 TensorRT。软件栈:Docker Compose 一键部署 Frigate(ghcr.io/blakeblackshear/frigate:stable),挂载 /config/config.yml 与 /media/frigate 存储卷。MQTT broker(如 Mosquitto)可选,用于 Home Assistant 集成。

核心配置在 config.yml,按以下清单分步设置,确保 ≥4GB RAM 与 SSD 存储:

1. Detectors 配置(边缘 ML 推理引擎)

detectors:
  coral_usb:  # Coral TPU 示例
    type: edgetpu
    device: usb:0
  tensorrt:   # NVIDIA TensorRT 示例
    type: tensorrt
    device: 0
model:
  path: /config/model_cache/ssdlite_mobilenet_v2.tflite  # 或 yolov8n.trt
  width: 320
  height: 320
  input_tensor: nhwc  # Coral 用 nhwc,TensorRT 用 nchw
  • 参数要点:分辨率 320x320 平衡精度 / 速度;多设备负载均衡(如 coral_usb1/usb2)。风险:无加速 CPU 负载 >80%,建议阈值监控。

2. Cameras 配置(RTSP 输入 + 运动 / 对象检测)

cameras:
  front_door:
    enabled: true
    ffmpeg:
      input_args: preset-rtsp-generic  # 稳定 RTSP 拉流
      inputs:
        - path: rtsp://user:pass@192.168.1.100:554/stream1
          roles:
            - detect
            - record
    detect:
      enabled: true
      width: 640    # 检测分辨率,低值减负载
      height: 360
      fps: 5        # 5fps 足实时,>10 增延迟
  • 优化:hwaccel_args: preset-vaapi(Intel)或 preset-nvidia(GPU)解码加速。

3. Motion Masks & Zones(过滤无关运动,聚焦事件) 运动蒙版排除树叶 / 水波,zones 定义门前 / 车道等关键区,避免误报。

cameras:
  front_door:
    motion:
      mask: 0,0,1000,0,1000,200,0,200  # poly 多边形坐标(Debug UI 生成)
    zones:
      driveway:
        coordinates: 200,200,800,200,800,600,200,600
        objects:
          - car
      door:
        coordinates: 500,300,700,300,700,500,500,500
        objects:
          - person
  • 清单:UI 中 Debug > Mask & Zone Creator 拖拽生成坐标;阈值 motion.threshold: 30(减小风吹误检)。

4. Objects & Events/Clips(低延迟事件处理)

objects:
  track:
    - person
    - car
  filters:
    person:
      min_score: 0.7  # 置信阈值,防误报
      min_area: 5000  # 忽略小物体
record:
  enabled: true
  retain:
    days: 7
    mode: motion  # 仅运动录制
events:
  retain:
    default: 30  # 事件剪辑保留 30 天
  pre_capture: 5s   # 事件前 5s
  post_capture: 5s  # 后 5s,确保完整轨迹
snapshots:
  enabled: true
  timestamp: true
  bounding_box: true
  • 低延迟 clips:WebRTC/MSE 直播 + 事件自动剪辑(H.264/H.265),导出 MP4 支持 timelapse_25x 模式快速审阅。

5. 监控与回滚策略

  • Metrics:Prometheus 暴露 frigate_cpu_usage_percent、frigate_detection_fps 等;告警 CPU>80%、存储 > 90%。
  • 优化阈值:contour_area: 50(忽略小运动);若延迟 > 500ms,回滚 fps=3 或加 TPU。
  • 风险限:RTSP 断线用 input_args: -rtsp_transport tcp;多路 > 8 路分机器部署。

实际部署 4 路 1080P 摄像头(Coral TPU):CPU<30%、延迟 < 150ms、事件准确率> 95%。扩展时用 Birdseye 总览多摄。相比商业 NVR,Frigate 零订阅费、本地隐私、全开源。

资料来源

查看归档