Frigate NVR + HA on QNAP NAS + TrueNAS Scale 部署教學

Frigate NVR + HA on QNAP NAS + TrueNAS Scale 部署教學

Frigate 即時物件辨識 + Home Assistant APP 推播 demo

簡單的寫一下目前 IPCAM NVR 的配置,這幾年陸陸續續用過 IPCAM 內建的 APP 、 Zoneminder (搭配 zmNinja 手機推播)、Synology Surveillance、UniFi Protect 等,一直找不到理想的方案(內建 APP 只支援該廠牌的 IPCAM、Zoneminder 介面老舊且 Event Server 設定太複雜,Synology 兩支以上需要授權,UniFi Protect 一樣只支援 UniFi 生態,另外 Blue Iris 需要用 VM 裝 Windows 系統所以一直不考慮),直到 Frigate NVR 出來後,解決了上述所有的問題,稍微操作了一下 demo 頁面,立刻感受到「摁,就是這個了」。

Frigate 的 event list

1. 系統需求

監視器的話有支援 rtsp 協定就好(例如 Tapo C100),若需要 PTZ 操作雲台則需要有 ONVIF 支援。若有監視器廠牌的安全疑慮可以在 router 端切 VLAN 或設定 WAN OUT 的 firewall rule 阻擋對外的連線。

硬體的話只要有支援 container 都行,例如 Intel PC 、 Raspberry Pi ,我這裡用的是 QNAP 的 TS-673A 。

OS 的話也是一樣有支援 container 就可以,使用 NAS 內建的 QuTS 或是安裝任意 Linux 系列的 OS 也行,我這裡用的是 TrueNAS Scale

因為我的 QNAP NAS 沒有內建顯示介面,需要裝一張顯示卡來完成 TrueNAS 的安裝步驟,顯示卡也可以做來用 Frigate 的硬體加速和物件辨識,支援的硬體可以看這裡那裡

硬碟的話因為我是 always-on 錄影,裝了兩個 WD RED HDD 做 RAID1。

另外如果要用物件辨識的功能,最好再加裝 detector ,我用的是 Google Coral TPU USB

2. 安裝步驟

用部署過 docker container 的話應該都很熟悉,照著官網的步驟裝就好,要注意若有安裝顯卡要設定一下 GPU passthrough ,若要用 NVIDIA 顯卡做物件辨識的話要用帶有 tensorrt 後綴的 image 。另外設定一下 /dev/shm/tmp/cache 使用 tmpfs ,避免頻繁的硬碟存取;若有用 Coral TPU USB 還需要開啟 privileged mode 讓 container 能存取 USB 設備。

點我展開 TrueNAS Scale 的詳細設定
  • Image Repository: ghcr.io/blakeblackshear/frigate
  • Image Tag: stablestable-tensorrt
  • Add external Interfaces 或 Port forwarding:
    • Container Port: 5000
    • Protocol: TCP
  • Host Path Volumes:
    • Mount Path: /media/frigate
    • Mount Path: /config
  • Memory Backed Volumes:
    • Mount Path: /dev/shm
    • Mount Path: /tmp/cache
  • Privileged Mode: Enabled
  • GPU Configuration: Allocate GPU resource (如果要使用硬體加速)

因為我有用 HA ( Home Assistant )收 event ,所以還要再裝個 MQTT broker 的 container ,我這裡避免麻煩不使用帳號密碼認證,直接建立一個檔案,內容為:

# This is a Mosquitto configuration file that creates a listener on port 1883
# that allows unauthenticated access.

listener 1883
allow_anonymous true

然後 mount 到 container 內的 /mosquitto/config/mosquitto.conf

最後是 Home Assistant ,可以用 VM 裝 HAOS 或是 container ,要注意的是如果用 container 的話, network 要開 host 模式(才可以用 auto-discovery 的功能)。

apps dashboard

3. Config 設定

Frigate 的 config 檔案請照這個頁面一步步設定,增減想要的功能(例如錄影保留多久、辨識哪些物件等)就好,並根據現有的硬體設定硬體加速,有問題的話可以留言或 Discord 討論。

Home Assistant 需要先裝 HACSMQTT integration ,然後在 HACS 找到 Frigate 安裝。如果只是單純做錄影(不需要手機推播通知的話),就不需要裝 Home Assistant 。

ha integrations

都裝好後你的 Integrations 頁面應該會有黃底的這三個。

接下來要設定根據藍圖設定手機推播,這邊要注意如果你是想要在外網收到通知並正常顯示照片,那就需要 port forwarding 或是 Home Assistant Cloud 。我這裡是用 nginx reverse proxy 做 hostname 和 port 的 forwarding 。

手機安裝 Home Assistant 並登入後,Integrations 頁面會多個 Mobile App ,接著用匯入的藍圖建立新的 Automation ,並且在 Mobile Device 選擇你的手機裝置。

select mobile device in blueprint

要注意你的 Frigate detection 功能要在 config 或 UI 打開(可以看 Frigate Cameras 頁面,有個人型的按鈕有沒有按下去),手機才會收到通知,要 debug 的話藍圖下面有個 debug 模式打開後,可以從 HA 的 Logbook 看 error message 。

ensure detect is on

Frigate 之後也會支援 PTZ (但在我寫這篇的時候還沒支援),我是在 HA 裝 ONVIF ,在 Lovelace UI 加入按鈕進行 PTZ 。這邊避免多餘的網路流量和 IPCAM 負擔,影像是抓 Frigate 串流的,所以 ONVIF 這裡的 FFMPEG 設定為 null input 。

set ffmpeg input to null in onvif
onvif ptz demo

也可以設定預設點的按鈕(上圖的 reset icon 和下圖的骰子), preset 的名字要跟 IPCAM APP 裡面設定的一樣就是了。

點我展開 Card 的 YAML
type: picture-glance
entities:
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        pan: LEFT
        speed: 1
        distance: 0.3
        move_mode: ContinuousMove
    name: Pan Left
    show_state: false
    icon: mdi:arrow-left
    show_icon: true
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        tilt: UP
        speed: 1
        distance: 0.3
        move_mode: ContinuousMove
    name: Tilt Up
    icon: mdi:arrow-up
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        tilt: DOWN
        speed: 1
        distance: 0.3
        move_mode: ContinuousMove
    name: Tilt Down
    icon: mdi:arrow-down
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        pan: RIGHT
        speed: 1
        distance: 0.3
        move_mode: ContinuousMove
    name: Pan Right
    icon: mdi:arrow-right
    show_icon: true
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        preset: '1'
        move_mode: GotoPreset
    name: Preset 1
    icon: mdi:dice-1
    show_icon: true
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        preset: '2'
        move_mode: GotoPreset
    name: Preset 2
    icon: mdi:dice-2
    show_icon: true
  - entity: camera.YOUR_ONVIF_CAMERA_STREAM
    tap_action:
      action: call-service
      service: onvif.ptz
      service_data:
        entity_id: camera.YOUR_ONVIF_CAMERA_STREAM
        preset: '3'
        move_mode: GotoPreset
    name: Preset 3
    icon: mdi:dice-3
    show_icon: true
camera_image: camera.YOUR_FRIGATE_CAMERA_STREAM
camera_view: live

onvif ptz presets

另外有開啟物件辨識或是 IPCAM 數量很多的話,可能會有升級硬體的需求,這邊提供手邊現有的 CPU/GPU/TPU 使用量供參考:下面左圖是使用 NVIDIA T400 GPU 和 tensorRT 做 decoder 和 detector( model 為 yolov7-tiny-416 );右圖則是用 Intel J4105 的內顯做 decoder 、 Coral USB 作 detector ,兩邊分別裝了 6-8 台 1080p / 2K 的 IPCAM ,目前運作都順暢。建議可以先用手邊現有的硬體部署,若遇上效能瓶頸再考慮升級(聽說中國最近有一批很好用的 Tesla P4 ,不介意二手的話可以買來賭人品,不過因為 P4 是被動散熱方案,需要自行再加裝風扇就是了)。

system utilization
nvidia-smi
2K 的 h.265 rtsp stream 做 decode 大約占用 266 MB 的 VRAM; 1080p 的則約 168MB ,可以根據 IPCAM 數量計算一下 GPU 所需的 VRAM 。

Frigate | NVR | HA | TrueNAS 相關討論的 Discord 連結

Leave a reply (please do not include URL in reply)