Frigate NVR - OpenCV, Tensorflow and MQTT

Frigate NVR - a complete and local NVR designed for Home Assistant with AI object detection. Uses OpenCV and Tensorflow to perform realtime object detection locally for IP cameras.

Installation

Frigate is a Docker container that can be run on any Docker host including as a HassOS Addon. Note that a Home Assistant Addon is not the same thing as the integration. The integration is required to integrate Frigate into Home Assistant.

Dependencies

MQTT broker - Frigate requires an MQTT broker. If using Home Assistant, Frigate and Home Assistant must be connected to the same MQTT broker.

Storage

Frigate uses the following locations for read/write operations in the container. Docker volume mappings can be used to map these to any location on your host machine.

  • /media/frigate/clips: Used for snapshot storage. In the future, it will likely be renamed from clips to snapshots. The file structure here cannot be modified and isn't intended to be browsed or managed manually.
  • /media/frigate/recordings: Internal system storage for recording segments. The file structure here cannot be modified and isn't intended to be browsed or managed manually.
  • /media/frigate/frigate.db: Default location for the sqlite database. You will also see several files alongside this file while frigate is running. If moving the database location (often needed when using a network drive at /media/frigate), it is recommended to mount a volume with docker at /db and change the storage location of the database to /db/frigate.db in the config file.
  • /tmp/cache: Cache location for recording segments. Initial recordings are written here before being checked and converted to mp4 and moved to the recordings folder.
  • /dev/shm: It is not recommended to modify this directory or map it with docker. This is the location for raw decoded frames in shared memory and it's size is impacted by the shm-size calculations below.
  • /config/config.yml: Default location of the config file.
mkdir -p /opt/frigate/storage

Calculating required shm-size

Frigate utilizes shared memory to store frames during processing. The default shm-size provided by Docker is 64m.

The default shm-size of 64m is fine for setups with 2 or less 1080p cameras. If frigate is exiting with "Bus error" messages, it is likely because you have too many high resolution cameras and you need to specify a higher shm size.

You can calculate the necessary shm-size for each camera with the following formula:

(width * height * 1.5 * 9 + 270480)/1048576 = <shm size in mb>

Creating a config file

Configure the MQTT server

nano /opt/frigate/config.yml

Frigate requires a functioning MQTT server. Start by adding the mqtt section at the top level in your config:

mqtt:
  host: <ip of your mqtt server>

If using the Mosquitto Addon in Home Assistant, a username and password is required. For example:

mqtt:
  host: 192.168.2.117
  user: admin
  password: instar

Configure a Camera

cameras:
  camera_1: # <------ Name the camera
    ffmpeg:
      inputs:
        - path: rtsp://admin:instar@192.168.2.19:554/livestream/12 # <----- Update for your camera
          roles:
            - detect
            - rtmp
    detect:
      width: 1920 # <---- update for your camera's resolution
      height: 1080 # <---- update for your camera's resolution

Docker-Compose

Pick a Frigate version from the Docker Hub, e.g.:

  • blakeblackshear/frigate:0.10.0-14c74e4-amd64 linux/amd64
  • blakeblackshear/frigate:stable-aarch64 linux/arm64

And edit the Docker-Compose file accordingly:

nano /opt/frigate/docker-compose.yml
version: "3.9"
services:
  frigate:
    container_name: frigate
    privileged: true # this may not be necessary for all setups
    restart: unless-stopped
    image: blakeblackshear/frigate:0.10.0-14c74e4-amd64
    shm_size: "64mb" # update for your cameras based on calculation above
    devices:
      - /dev/bus/usb:/dev/bus/usb # passes the USB Coral, needs to be modified for other versions
      - /dev/dri/renderD128 # for intel hwaccel, needs to be updated for your hardware
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /opt/frigate/config.yml:/config/config.yml:ro
      - /opt/frigate/storage:/media/frigate
      - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear
        target: /tmp/cache
        tmpfs:
          size: 1000000000
    ports:
      - "5000:5000"
      - "1935:1935" # RTMP feeds
    environment:
      FRIGATE_RTSP_PASSWORD: "password"