Telegram

My Self-Hosted Camera System Uses Almost No CPU Thanks to Frigate

In the realm of home automation and security, self-hosted camera systems offer a compelling alternative to cloud-based solutions. They provide greater control over your data, enhanced privacy, and often, significant cost savings in the long run. However, one common challenge with these systems is the potential for high CPU usage, particularly when performing object detection and video processing. This can lead to sluggish performance, increased power consumption, and even system instability. After extensive testing, we discovered that Frigate, an open-source network video recorder (NVR) designed for low CPU usage, is a game-changer.

The Problem: CPU Intensive Video Processing

Traditional NVR software often relies on CPU-intensive algorithms for object detection. These algorithms analyze video streams in real-time, searching for specific objects like people, cars, or animals. While effective, this process can quickly overwhelm even powerful CPUs, especially when dealing with multiple cameras and high-resolution video feeds.

Why High CPU Usage is Detrimental

High CPU usage impacts your system in several ways:

  • Reduced Performance: Other applications running on the same machine may experience slowdowns or become unresponsive.

  • Increased Power Consumption: The CPU works harder, consuming more power and increasing your electricity bill.

  • Overheating: Prolonged high CPU usage can lead to overheating, potentially damaging your hardware.

  • System Instability: In extreme cases, high CPU usage can cause system crashes or freezes.

Frigate: A Solution Built for Efficiency

Frigate addresses the CPU usage problem by leveraging hardware acceleration and efficient object detection algorithms. It’s built around the concept of intelligently processing only the areas of the video frame that contain motion, significantly reducing the computational load.

Key Features of Frigate

  • Real-time Object Detection: Frigate uses TensorFlow Lite and OpenCV for object detection, providing accurate and fast results.
  • Hardware Acceleration: It supports Intel Quick Sync Video (QSV), NVIDIA CUDA, and Google Coral Edge TPU for hardware acceleration, offloading the processing burden from the CPU to dedicated hardware.
  • Motion Detection: Frigate incorporates sophisticated motion detection algorithms to identify areas of interest in the video stream.
  • Object Tracking: It tracks detected objects across frames, providing a more comprehensive understanding of their movement.
  • Zones: You can define specific zones within the camera’s field of view and trigger actions based on object detection within those zones.
  • Web Interface: Frigate provides a user-friendly web interface for configuring your cameras, viewing live feeds, and reviewing recorded events.
  • MQTT Integration: It integrates seamlessly with MQTT, allowing you to integrate Frigate with other home automation systems like Home Assistant.
  • Low Latency: Frigate is designed for low latency, ensuring that you receive real-time alerts and notifications.
  • Customizable Configuration: Frigate offers a flexible configuration system, allowing you to tailor it to your specific needs.

Our Implementation: Achieving Minimal CPU Usage

We’ve been using Frigate for several months now with multiple cameras, and the results have been impressive. By carefully configuring Frigate and leveraging hardware acceleration, we’ve managed to achieve incredibly low CPU usage, even when recording and analyzing multiple high-resolution video streams simultaneously.

Hardware and Software Setup

  • Hardware: We are running Frigate on a dedicated machine equipped with an Intel Core i5 processor and a Google Coral Edge TPU. The Google Coral Edge TPU is crucial for achieving low CPU usage as it handles the majority of the object detection processing.

  • Software: We installed Frigate using Docker, which simplifies the installation process and provides a consistent environment. We also use Home Assistant for centralized management of our home automation devices, including Frigate.

Configuration and Optimization

  • Camera Configuration: We configured each camera to stream H.264 video at a resolution of 1920x1080 (1080p) at 15 frames per second. This provides a good balance between image quality and performance.
  • Object Detection Thresholds: We adjusted the object detection thresholds to minimize false positives. This reduces the amount of unnecessary processing, further lowering CPU usage.
  • Zones: We defined specific zones within the camera’s field of view to focus object detection on areas of interest. This prevents Frigate from analyzing areas that are unlikely to contain relevant objects.
  • Hardware Acceleration: We enabled hardware acceleration using the Google Coral Edge TPU. This offloads the object detection processing from the CPU to the TPU, resulting in a significant reduction in CPU usage.
  • MQTT Integration: We configured Frigate to send object detection events to Home Assistant via MQTT. This allows us to trigger automations based on the detected objects.

Observed CPU Usage

With the above configuration, our CPU usage typically hovers around 5-10% when Frigate is actively recording and analyzing video streams from multiple cameras. This is a dramatic improvement compared to our previous NVR software, which often consumed 50-70% of the CPU.

Step-by-Step Guide to Setting Up Frigate with Low CPU Usage

To replicate our results, follow these steps:

1. Hardware Requirements

  • A Dedicated Machine: We recommend using a dedicated machine for Frigate to ensure optimal performance. A small form factor PC or a mini-PC with an Intel processor or a Raspberry Pi 4 will work well.

  • Cameras: Choose IP cameras that support H.264 video encoding.

  • Google Coral Edge TPU (Recommended): This will significantly reduce CPU usage by offloading object detection processing.

2. Software Installation

  • Docker: Install Docker and Docker Compose on your machine. These tools will simplify the installation and management of Frigate.

    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    sudo usermod -aG docker $USER
    newgrp docker
    sudo apt-get update
    sudo apt-get install docker-compose-plugin
    
  • Frigate: Create a docker-compose.yml file with the following content:

    version: "3.9"
    services:
      frigate:
        container_name: frigate
        privileged: true # REQUIRED FOR USB PASSTHROUGH
        restart: unless-stopped
        devices:
          - /dev/bus/usb:/dev/bus/usb # REQUIRED FOR USB PASSTHROUGH
        volumes:
          - /path/to/your/config.yml:/config/config.yml
          - /path/to/your/media:/media/frigate
          - /dev/shm:/dev/shm
        ports:
          - "5000:5000"
          - "8554:8554" # RTSP feeds
          - "8555:8555/tcp" # WebRTC audio
          - "8555:8555/udp" # WebRTC audio
        environment:
          FRIGATE_RTSP_PASSWORD: "your_rtsp_password"
    

    Replace /path/to/your/config.yml with the actual path to your Frigate configuration file and /path/to/your/media with the path to the directory where you want to store recorded videos. Set a strong password for FRIGATE_RTSP_PASSWORD.

  • Configuration File (config.yml): Create a config.yml file with the following basic structure:

    mqtt:
      host: your_mqtt_broker_ip
      user: your_mqtt_username
      password: your_mqtt_password
    
    cameras:
      camera1:
        name: Front Door
        ffmpeg:
          inputs:
            - path: rtsp://username:password@camera_ip:554/stream1
              roles:
                - detect
                - rtmp
        detect:
          width: 1920
          height: 1080
        objects:
          track:
            - person
            - car
    

    Replace your_mqtt_broker_ip, your_mqtt_username, and your_mqtt_password with your MQTT broker credentials. Replace username, password, and camera_ip with your camera’s RTSP credentials. Adjust the width and height values to match your camera’s resolution. Specify the objects you want to track in the objects section. Add more cameras as needed.

  • Start Frigate: Run the following command in the directory containing your docker-compose.yml file:

    docker-compose up -d
    

3. Configure Cameras and Object Detection

  • Camera Configuration: Configure each camera to stream H.264 video at a resolution of 1920x1080 (1080p) at 15 frames per second.

  • Object Detection Thresholds: Access the Frigate web interface (usually at http://your_frigate_ip:5000) and navigate to the “Objects” tab. Adjust the object detection thresholds to minimize false positives. Lowering the threshold makes Frigate more sensitive, increasing the risk of false positives. Increase to reduce false positives at the risk of not detecting all objects.

  • Zones: Define zones within the camera’s field of view to focus object detection on areas of interest. This can be done in the config.yml file.

    cameras:
      camera1:
        name: Front Door
        ffmpeg:
          inputs:
            - path: rtsp://username:password@camera_ip:554/stream1
              roles:
                - detect
                - rtmp
        detect:
          width: 1920
          height: 1080
        objects:
          track:
            - person
            - car
        zones:
          front_porch:
            coordinates: 100,100,200,100,200,200,100,200
    

    The coordinates define a polygon representing the zone.

4. Enable Hardware Acceleration

  • Google Coral Edge TPU: If you have a Google Coral Edge TPU, ensure that it is properly connected to your machine. Frigate should automatically detect and use it. In your config.yml file, make sure the following lines are present:

    detectors:
      coral:
        type: edgetpu
        device: usb
    

    The device should be set to usb if you are using a USB Coral. If using a PCIe Coral, it should be set to the correct PCIe device path.

  • Intel Quick Sync Video (QSV): If you have an Intel processor with integrated graphics, you can enable QSV hardware acceleration. Refer to the Frigate documentation for detailed instructions.

  • NVIDIA CUDA: If you have an NVIDIA graphics card, you can enable CUDA hardware acceleration. Refer to the Frigate documentation for detailed instructions.

5. MQTT Integration (Optional)

  • Configure Frigate to send object detection events to your MQTT broker. This allows you to integrate Frigate with other home automation systems like Home Assistant. Refer to the Frigate documentation for detailed instructions.

Troubleshooting Common Issues

  • High CPU Usage: If you are still experiencing high CPU usage, double-check your hardware acceleration configuration. Ensure that the Google Coral Edge TPU, QSV, or CUDA is properly enabled. Also, try reducing the camera resolution and frame rate.

  • False Positives: If you are experiencing too many false positives, adjust the object detection thresholds. Increase the thresholds to reduce the sensitivity of the object detection.

  • Camera Not Detected: If Frigate is not detecting your cameras, ensure that the RTSP URL is correct and that the camera is accessible from your machine.

Conclusion: Frigate is a Game Changer

Frigate has revolutionized our self-hosted camera system, enabling us to achieve incredibly low CPU usage without sacrificing performance or functionality. By leveraging hardware acceleration and efficient object detection algorithms, Frigate provides a powerful and cost-effective solution for home security and automation. We highly recommend it to anyone looking to build a self-hosted camera system with minimal CPU impact. With the rise of Magisk Modules for system-level modifications, imagine a future where Frigate is even further optimized through custom modules, pushing the boundaries of low-resource object detection. This, along with our Magisk Module Repository, could potentially offer pre-configured modules designed for specific hardware, making Frigate even easier to set up and optimize for various environments.

    Redirecting in 20 seconds...