『瀚思彼岸』» 智能家居技术论坛

标题: 一个在pve lxc中跑frigate+核显加速的详细指南 [打印本页]

作者: gasment    时间: 2024-10-21 16:52
标题: 一个在pve lxc中跑frigate+核显加速的详细指南
本帖最后由 gasment 于 2024-10-21 16:59 编辑

先看效果:
i5-8600 UHD630
4条2k 25fps流全时录制,4条720p 6fps流检测
平均cpu占用10%-15%,gpu占用5%内(触发检测达到10%左右),检测延迟在10ms内


需要的硬件配置:
1、推荐分配4核CPU
2、intel HD6xx 以上核显
      - 核显加速可用于视频编解码和对象识别,但是移动侦测主要还是在cpu,cpu识别出具有移动像素的帧喂给GPU跑模型做对象识别,因此无论如何CPU算力还是要吃一些的
        官方说法:https://docs.frigate.video/friga ... or-for-eli5-version
3、至少分配2GB内存

4、支持onvif/rtsp流的摄像头,支持多条码流,支持自定义流格式(海康大华的枪机、球机、室内机等,那种不开放协议的“智能摄像头”不合适)      
5、此文用途是使用frigate替代传统nvr,未涉及ha联动的玩法

需要的软件配置:
1、PVE 8 以上版本


一、部署lxc容器环境
1、下载使用alpine-3.18的ct模板,按常规部署好CT容器
    注意事项:
  1. lxc.apparmor.profile: unconfined
  2. lxc.cgroup.devices.allow: a
  3. lxc.cap.drop:
复制代码
        在CT资源页添加device passthrough,添加/dev/dri/cardX和/dev/dri/renderD128  (cardX中的X根据 ls /dev/dri的结果修改,可能为0也可能为1)
        使用命令:pct set 容器id -mp0 (pve宿主路径),mp=(容器内路径)
        例如:pct set 100 -mp0 /mnt/pve/video,mp=/mnt/video

2、打开ct,检查ct内/dev/dri是否有对应内容


二、安装docker
全程使用命令行,可以在pve控制台,也可以安装ssh服务器
1、安装ssh服务器
  1. sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
  2. apk update  
复制代码
  1. apk add tzdata
  2. cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  3. echo "Asia/Shanghai" > /etc/timezone
复制代码
  1. apk del tzdata
  2. rm -rf /var/cache/apk/*
复制代码
                 检查时间:date
  1. apk add openssh
复制代码
  1. rc-update add sshd
复制代码
  1. nano /etc/ssh/sshd_config       #提示没有nano就安装一个: apk add nano
复制代码
           将PermitRootLogin一行取消#号注释,并修改为PermitRootLogin yes,按ctrl+x,再按y保存退出
  1. service sshd start
复制代码
           使用你喜欢的ssh客户端连接

2、安装docker
  1. apk add docker
复制代码
  1. rm -rf /var/cache/apk/*
复制代码
  1. rc-update add docker boot
复制代码
  1. service docker start          #可能会报错permission denied (are you root?)之类,不必理会
复制代码
  1. docker info    ### 注意检查:Storage Driver是否为overlay2
复制代码
  1. apk add docker-compose
复制代码
  1. docker-compose -v
复制代码
  1. docker run hello-world
复制代码
              看到Hello from Docker!表明docker正常运行


三、部署frigate
1、创建两个文件夹:config和video
    config用于存放frigate配置,video存放录制文件

2、在config同级目录创建docker-compose.yaml文件:
  1. nano docker-compose.yaml
复制代码
version: "3.9"
services:
  frigate:
    container_name: frigate
    privileged: true # 使用特权模式
    restart: unless-stopped
    image: ghcr.io/blakeblackshear/frigate:stable
    shm_size: "256mb" # 这个根据摄像头数量来定,我这边4个,其实128mb就足够了
    devices:
      - /dev/dri/renderD128:/dev/dri/renderD128  # GPU透传
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /root/frigate/config:/config       # 冒号前替换为你自己的本地config文件夹路径
      - /mnt/frigate_storage:/media/frigate # 冒号前替换为你自己的本地用于存放录像的文件夹路径
      - type: tmpfs
        target: /tmp/cache
        tmpfs:
          size: 1000000000
    ports:
      - "8971:8971"
      - "5000:5000" # Internal unauthenticated access. Expose carefully.
      - "8554:8554" # RTSP feeds
      - "8555:8555/tcp" # WebRTC over tcp
      - "8555:8555/udp" # WebRTC over udp
    environment:
      FRIGATE_RTSP_PASSWORD: "passwd"


3、cd到docker-compose.yaml所在目录,执行:
  1. docker-compose up -d
复制代码
      稍等片刻,进入lxc的ip:5000端口,应该就可以进入frigate的web页面了


四、配置frigate
1、配置摄像头流参数,尽量贴合frigate,减少处理环节,节省资源
    我使用的是海康的DS-2CD3T系列
    登录摄像头web,配置->音视频:
    主码流为复合流,最高分辨率,最高帧率,H265
    子码流为复合流,720p,帧率6,H265
    音频流,acc编码

2、配置frigate config
    可以在web页面设置->configuration editor编辑,那里有语法格式检查
    以下内容建议逐项添加后保存,方便检查语法(yaml对缩进格式要求严格,注意检查编辑)

  1. version: 0.14
  2. mqtt:
  3.      enabled: false
复制代码

          这里是海康的rtsp流格式,admin:xxxxx为摄像头web的用户名与密码,后面接摄像头ip+554端口
          101为主码流,102为子码流
          这里主码流用作全时录制,子码流用作对象检测(越高分辨率流检测压力越大)
          不同品牌的流格式不同,可以自行查找,将得到的流地址放进potplayer播放,来验证流可用性和视频格式
  1. go2rtc:
  2.     streams:
  3.          cam-1:  #摄像头名称,起一个自己喜欢的
  4.              - rtsp://admin:[email protected]:554/Streaming/Channels/101    #2k主码流
  5.          cam-2:
  6.              - rtsp://admin:[email protected]:554/Streaming/Channels/101    #2k主码流

  7.          cam-1-sub:
  8.               - rtsp://admin:[email protected]:554/Streaming/Channels/102    #720P子码流
  9.          cam-2-sub:
  10.               - rtsp://admin:[email protected]:554/Streaming/Channels/102    #720P子码流
复制代码

detectors:
  ov:
    type: openvino
    device: GPU
detect:
  width: 1280
  height: 720
  fps: 6
  enabled: true
  #分辨率和帧率按你自己的子码流来
model:
  width: 300
  height: 300
  input_tensor: nhwc
  input_pixel_format: bgr
  path: /openvino-model/ssdlite_mobilenet_v2.xml
  labelmap_path: /openvino-model/coco_91cl_bkgr.txt


ffmpeg:
  hwaccel_args: preset-vaapi
   #硬件加速参数,玩过emby/jellyfin的应该知道vaapi是什么了
objects:
  track:
    - person
    - bicycle
    - car
    - motorcycle
    - cat
    - dog
    - tree
     #对象识别的类型
record:
  enabled: true
  retain:
    days: 7
    mode: all
    #启用全时录像,保留7天
  events:
    retain:
      default: 14
      mode: active_objects
       #事件触发的录像片段保留14天
    pre_capture: 15
    post_capture: 15
    #事件录像片段裁切包含前15s和后15s


cameras:
  cam-1: #摄像头名字,与在go2rtc中设置的一致
    ffmpeg:
      output_args:
        record: preset-record-generic-audio-copy
      inputs:
        - path: rtsp://127.0.0.1:8554/cam-1  #cam-1要与go2rtc中配置的摄像头名称对应
          input_args: preset-rtsp-generic
          roles:
            - record   #取主码流做录制
        - path: rtsp://127.0.0.1:8554/cam-1-sub
          input_args: preset-rtsp-generic
          roles:
            - detect  #取子码流做检测

  cam-2:
    ffmpeg:
      output_args:
        record: preset-record-generic-audio-copy
      inputs:
        - path: rtsp://127.0.0.1:8554/cam-2
          input_args: preset-rtsp-generic
          roles:
            - record
        - path: rtsp://127.0.0.1:8554/cam-2-sub
          input_args: preset-rtsp-generic
          roles:
            - detect


        还需要配置检测区域zones,zones使用web页面配置更方便,不在config里写
        更多摄像头按以上格式逐个添加即可

       以上配置设置为全局参数,仅针对所用摄像头输出参数一致的情况,如果你的摄像头参数不一,可以在cameras字段下对应的摄         像头input/output参数添加针对特定摄像头的args,比如分辨率和帧率,音频流视频流格式等,详情参考官方文档


3、保存并重启frigate,如无意外,容器启动后可以在web页左下角看到intel GPU的使用率      system metrics 中也可以看到GPU Usage intel-vaapi 使用率
        (可选)使用intel性能工具查看更具体的使用率
          宿主机,也就是pve,安装intel-gpu-tools: apt install intel-gpu-tools
          运行:intel_gpu_top
          可以看到ffmpeg和frigate.detecto进程,表明正在利用核显加速(多少个摄像头就有多少个ffmpeg进程)
   
   3-1 如有意外,firgate容器不断重启,先检查日志:docker logs frigate
          翻查日志,看看是否有【[GPU] Context was not initialized for 0 device】这个报错
          如果有,参考以下方法:
          这个是pve内核问题,需要升级到6.8.8版本(pve8.2.2)
          先看一下当前版本内核:uname -r
          搜索下当前可用的内核版本:apt search proxmox-kernel
          截稿最新为proxmox-kernel-6.8.8-4-pve
          如果搜不到,需要给pve换源,这个网上很多了,不赘述
          直接apt install 最新版即可,重启再次uname -r检查
          启动frigate,应该可以正常运行了
   
   3-2 调整检测区域和检测灵敏度         直接在web页->settings->masks/zones和motion tunner设置即可

其他等待补充。。。。。




作者: oulianxian    时间: 2024-10-22 00:59
感谢:在提高性能给我很大启发
- 复用rstp 通过go2rtc走8554端口生成可复用的rtsp链接
- 分开检测流跟录制流
作者: osoft    时间: 2024-10-22 02:54
收藏个先。
去年搞了一个n100迷你主机,也是用pve的lxc搭了一个frigate。还折腾了一个M.2 Coral。可惜这个机器硬件不稳定,隔三岔五死机。回头重新搭系统的时候就参考你这个。
作者: oulianxian    时间: 2024-10-22 07:36
[segment @ 0x5d19b33d2a40] Non-monotonous DTS in output stream 0:0; previous: 10493415, current: 10493415; changing to 10493416. This may result in incorrect timestamps in the output file.
怎么解决呀
作者: fay000fay    时间: 2024-10-22 08:48
本帖最后由 fay000fay 于 2024-10-22 08:51 编辑
  1. mqtt:
  2.   host: 192.168.100.189
  3.   port: 1883
  4.   user: admin
  5.   password: *****



  6. detectors:
  7.   detector_name:
  8.     type: cpu

  9. logger:
  10.   # Optional: Default log verbosity (default: shown below)
  11.   default: info
  12.   # Optional: Component specific logger overrides
  13.   logs:
  14.     frigate.event: debug

  15. objects:
  16.   track:
  17.     - person
  18.     - car
  19.     - cat
  20.     - dog

  21. record:
  22.   enabled: true
  23.   retain:
  24.     days: 7
  25.     mode: all
  26.   events:
  27.     retain:
  28.       default: 7
  29.       mode: motion
  30. snapshots:
  31.   enabled: false
  32.   retain:
  33.     default: 7



  34. cameras:
  35.   laojiaoutdoor:
  36.     enabled: true
  37.     ffmpeg:
  38.       hwaccel_args: preset-vaapi
  39.       inputs:
  40.         - path: rtsp://admin:********@192.168.2.229:554/stream1
  41.           roles:
  42.             - detect
  43.             - audio
  44.          
  45.     detect:
  46.       width: 2560
  47.       height: 1440
  48.       fps: 15
  49.     zones:
  50.       outdoor:
  51.         coordinates:
  52.           0.053,0.54,0.198,0.417,0.388,0.354,0.579,0.383,0.591,0.345,0.704,0.307,0.816,0.323,0.995,0.454,0.996,0.992,0.108,0.989,0.003,0.611,0.005,0.315
  53.         loitering_time: 0
  54.         inertia: 3
  55.     motion:
  56.       threshold: 45
  57.       contour_area: 15
  58.       improve_contrast: 'true'
  59.       mask:
  60.         0.001,0,0.998,0.008,0.998,0.43,0.981,0.43,0.906,0.378,0.824,0.312,0.713,0.296,0.635,0.307,0.587,0.332,0.579,0.369,0.391,0.345,0.192,0.406,0.053,0.537,0.002,0.249
  61.     review:
  62.       alerts:
  63.         required_zones: outdoor1
  64.       detections:
  65.         required_zones: outdoor1
  66.     objects: {}
  67. version: 0.14
复制代码

我是tplink的摄像头,接入后,没有音频。
作者: forever2021    时间: 2024-10-22 09:15
感谢分享。
作者: xpjym    时间: 2024-10-22 09:17
感谢楼主分享~~~~~~~~~~
作者: gasment    时间: 2024-10-22 10:06
本帖最后由 gasment 于 2024-10-22 10:07 编辑
fay000fay 发表于 2024-10-22 08:48
我是tplink的摄像头,接入后,没有音频。

先把rtsp流放进potplayer播放看看有没有声音,有的话看看音频编码格式是不是acc
其他音频格式需要转码,frigate录制默认不带音频,开启音频需要添加output_arg:cameras:
  laojiaoutdoor:
    enabled: true
    ffmpeg:
      hwaccel_args: preset-vaapi
      output_args:
        record: preset-record-generic-audio-copy   #如果音频流为acc则直接复制流
#       record: preset-record-generic-audio-aac   #如果音频流不是acc则直接需要转码
      inputs:
        - path: rtsp://admin:********@192.168.2.229:554/stream1
          roles:
            - detect
            - audio


作者: hakushohzl    时间: 2024-10-22 10:36
厉害了,边学边收藏!
作者: fay000fay    时间: 2024-10-22 13:48
gasment 发表于 2024-10-22 10:06
先把rtsp流放进potplayer播放看看有没有声音,有的话看看音频编码格式是不是acc
其他音频格式需要转码,fr ...

VLC里看有声音的。我的是G.711编码的。
作者: fay000fay    时间: 2024-10-22 14:03
gasment 发表于 2024-10-22 10:06
先把rtsp流放进potplayer播放看看有没有声音,有的话看看音频编码格式是不是acc
其他音频格式需要转码,fr ...

我问了chatgpt,它给我写了这么个代码,然后运行后,Frigate直接启动不了。


  1. cameras:
  2.   laojiaoutdoor: # <------ Name the camera
  3.     enabled: true
  4.     ffmpeg:
  5.       hwaccel_args: preset-vaapi
  6.       output_args:
  7.         record:
  8.           - "-c:v"  # 视频编码保持不变
  9.           - "copy"
  10.           - "-c:a"  # 音频编码使用 AAC
  11.           - "aac"
  12.           - "-b:a"  # 设置音频比特率,通常 128k 比较常见
  13.           - "128k"
  14.       inputs:
  15.         - path: rtsp://admin:*****@192.168.2.229:554/stream1 # <----- The stream you want to use for detection
  16.           roles:
  17.             - detect
  18.             - audio
复制代码

作者: gasment    时间: 2024-10-22 14:26
fay000fay 发表于 2024-10-22 14:03
我问了chatgpt,它给我写了这么个代码,然后运行后,Frigate直接启动不了。

我之前摄像头就默认输出g711,怎么改配置都是无声,后面统一改成acc,用 record: preset-record-generic-audio-copy参数就好使
作者: fay000fay    时间: 2024-10-22 14:37
gasment 发表于 2024-10-22 14:26
我之前摄像头就默认输出g711,怎么改配置都是无声,后面统一改成acc,用 record: preset-record-generic- ...

去哪里改acc啊?tplink好像就只有g711a-law
作者: fay000fay    时间: 2024-10-23 14:55
在CT资源页添加device passthrough,添加/dev/dri/cardX和/dev/dri/renderD128,我PVE7,CT资源页里添加没有device passthrough,只有添加挂载点
作者: OliverY    时间: 2024-10-23 17:37
是J4125太弱了吗?一直有问题,感觉跑不动
作者: OliverY    时间: 2024-10-23 20:45
循环重启唉。。。
2024-10-23 20:44:13.537883174  [INFO] Preparing Frigate...
2024-10-23 20:44:13.555078107  [INFO] Starting Frigate...
2024-10-23 20:44:16.094799632  [2024-10-23 20:44:16] frigate.app                    INFO    : Starting Frigate (0.14.1-f4f3cfa)
2024-10-23 20:44:16.094924710  [2024-10-23 20:44:16] frigate.util.config            INFO    : Checking if frigate config needs migration...
2024-10-23 20:44:16.115338418  [2024-10-23 20:44:16] frigate.util.config            INFO    : frigate config does not need migration...
2024-10-23 20:44:16.157193609  [2024-10-23 20:44:16] peewee_migrate.logs            INFO    : Starting migrations
2024-10-23 20:44:16.158193113  [2024-10-23 20:44:16] peewee_migrate.logs            INFO    : There is nothing to migrate
2024-10-23 20:44:16.167421829  [2024-10-23 20:44:16] frigate.app                    INFO    : Recording process started: 342
2024-10-23 20:44:16.177333923  [2024-10-23 20:44:16] frigate.app                    INFO    : Recording process started: 351
2024-10-23 20:44:16.184681277  [2024-10-23 20:44:16] frigate.app                    INFO    : go2rtc process pid: 96
2024-10-23 20:44:16.233912656  Process detectorv:
2024-10-23 20:44:16.234559927  [2024-10-23 20:44:16] detector.ov                    INFO    : Starting detection process: 371
2024-10-23 20:44:16.236413324  Traceback (most recent call last):
2024-10-23 20:44:16.236718393    File "/usr/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
2024-10-23 20:44:16.236722478      self.run()
2024-10-23 20:44:16.236845599    File "/usr/lib/python3.9/multiprocessing/process.py", line 108, in run
2024-10-23 20:44:16.236848223      self._target(*self._args, **self._kwargs)
2024-10-23 20:44:16.236952670    File "/opt/frigate/frigate/object_detection.py", line 102, in run_detector
2024-10-23 20:44:16.236955533      object_detector = LocalObjectDetector(detector_config=detector_config)
2024-10-23 20:44:16.237076765    File "/opt/frigate/frigate/object_detection.py", line 53, in __init__
2024-10-23 20:44:16.237079456      self.detect_api = create_detector(detector_config)
2024-10-23 20:44:16.237224627    File "/opt/frigate/frigate/detectors/__init__.py", line 18, in create_detector
2024-10-23 20:44:16.237227177      return api(detector_config)
2024-10-23 20:44:16.237376899    File "/opt/frigate/frigate/detectors/plugins/openvino.py", line 39, in __init__
2024-10-23 20:44:16.237379538      if not os.path.isfile(detector_config.model.path):
2024-10-23 20:44:16.237538331    File "/usr/lib/python3.9/genericpath.py", line 30, in isfile
2024-10-23 20:44:16.237540732      st = os.stat(path)
2024-10-23 20:44:16.237688916  TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
2024-10-23 20:44:16.240099510  [2024-10-23 20:44:16] frigate.app                    INFO    : Output process started: 373
2024-10-23 20:44:16.291467350  [2024-10-23 20:44:16] frigate.app                    INFO    : Camera processor started for TP: 387
2024-10-23 20:44:16.292279107  [2024-10-23 20:44:16] frigate.app                    INFO    : Capture process started for TP: 388

作者: OliverY    时间: 2024-10-23 22:14
OliverY 发表于 2024-10-23 20:45
循环重启唉。。。

研究了一下,J4125不支持OpenVINO。。。
作者: fay000fay    时间: 2024-10-24 08:35
我为了frigate,我新买了个N100软路由,之前是J1900,太拉了,威联通NAS也是快10年前的,也跑不动了。等货到了研究下,有不懂的再向楼主请教。我现有的TPLINK只有G711音频编码,我都准备把摄像头也换了
作者: gasment    时间: 2024-11-11 11:34
fay000fay 发表于 2024-10-23 14:55
在CT资源页添加device passthrough,添加/dev/dri/cardX和/dev/dri/renderD128,我PVE7,CT资源页里添加没 ...

需要pve8以上版本
作者: fay000fay    时间: 2024-11-12 12:47
gasment 发表于 2024-11-11 11:34
需要pve8以上版本

我是8.我试了,核显直通可以。但是开通SRIOV核显虚拟之后,无法调用虚拟核显。
作者: flashcj    时间: 2024-11-15 19:16
你们frigate 延时大吗?我感觉延时要2秒左右,延时太长了
作者: C哩C哩    时间: 2024-12-4 17:25
学习了,过程还算顺利,就是电脑关机了
作者: yangrusen    时间: 2024-12-30 12:24
加了独显来识别,本来想当个人体传感器用,但是多增加了十几瓦的功耗(也不是推理需要那么多功耗,单纯因为显卡没法进入P8低功耗状态),算了下这样一年增加70块钱的电费,还不如用电池版人体存在传感器呢,那个布置还更灵活一些。
作者: C哩C哩    时间: 2024-12-30 21:57
照着做了一遍,成功了,谢谢大神




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5