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>
  user: <username>
  password: <password>

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 your Cameras

cameras:

  fullhd31: # <------ Name the camera
    ffmpeg:
      inputs:
        - path: rtsp://admin:instar@192.168.2.31:554/11 # <----- Update for your camera
          roles:
            - detect
            - rtmp
    detect:
      width: 1920 # <---- update for your camera's resolution
      height: 1080 # <---- update for your camera's resolution

  wqhd120: # <------ Name the camera
    ffmpeg:
      inputs:
        - path: rtsp://admin:instar@192.168.2.120: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

Frigate NVR - OpenCV, Tensorflow and MQTT

Downloading the Docker Image

The Docker image is being build on Github and can be downloaded from there - Docker Images. The - at the time of writing - latest image can be downloaded with:

docker pull ghcr.io/blakeblackshear/frigate:0.12.0-beta5

Docker-Compose

Edit the following Docker-Compose file according to the container image and configuration path you have chosen above:

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: ghcr.io/blakeblackshear/frigate:0.12.0-beta5
    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/apex_0:/dev/apex_0 # passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux
      - /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"

Docker CLI

If you can't use docker compose, you can run the container with this:

docker run -d \
  --name frigate \
  --restart=unless-stopped \
  --mount type=tmpfs,target=/tmp/cache,tmpfs-size=1000000000 \
  --device /dev/bus/usb:/dev/bus/usb \
  --device /dev/dri/renderD128 \
  --shm-size=64m \
  -v /opt/frigate/storage:/media/frigate \
  -v /opt/frigate/config.yml:/config/config.yml:ro \
  -v /etc/localtime:/etc/localtime:ro \
  -e FRIGATE_RTSP_PASSWORD='password' \
  -p 5000:5000 \
  -p 1935:1935 \
  ghcr.io/blakeblackshear/frigate:0.12.0-beta5

Web User Interface

With the Docker container started head over to your web browser (I had an issue using Firefox - but Chrome worked) and open the following address:

http://localhost:5000

You should now be able to see all your cameras:

Frigate NVR - OpenCV, Tensorflow and MQTT

And since we configured one of our cameras as an MQTT broker, Frigate now reports software states and events under frigate/:

Frigate NVR - OpenCV, Tensorflow and MQTT