JAlcocerTech E-books

Chapter 12 — Computer Vision on the Edge: Pi Camera and Frigate

Computer vision on a Raspberry Pi started as a curiosity — “can this hardware actually do it?” — and became a genuinely useful part of a homelab setup.

The short answer to the hardware question: yes, but with the right software choices.

The Pi Camera Stack

The Raspberry Pi Camera Module v2 (8MP, ~€25) connects directly to the Pi’s CSI port. It’s faster than a USB webcam at the same resolution because the data doesn’t have to go through the USB subsystem.

Raspberry Pi 4 with the camera module attached via CSI ribbon cable

Pi camera module close-up — the OV5647 sensor used for Frigate NVR

Scrypted is a home automation platform with excellent camera support. It runs in Docker, integrates with Home Assistant, and handles the RTSP stream from the Pi camera efficiently. It also supports camera plugins for object detection models.

OpenCV is the standard library for computer vision in Python. On a Pi 4, it handles real-time processing at 720p resolution at around 10–15 FPS. For motion detection, object classification, and image analysis, OpenCV is the tool.

A motion detection script:

import cv2

cap = cv2.VideoCapture(0)
ret, prev_frame = cap.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    diff = cv2.absdiff(prev_gray, gray)
    _, thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)
    motion_pixels = cv2.countNonZero(thresh)
    if motion_pixels > 5000:
        print("Motion detected")
    prev_gray = gray

Frigate: NVR with Object Detection

Frigate is an open-source Network Video Recorder (NVR) with built-in object detection. It runs in Docker, supports Google Coral TPU for accelerated inference, and integrates natively with Home Assistant.

The key capability: Frigate can distinguish between motion caused by a cat, a person, or a car — without cloud processing. Everything stays local.

For a homelab setup, Frigate replaces cloud-dependent cameras like Ring or Nest. The footage stays on your server, the detections trigger Home Assistant automations, and there’s no monthly subscription.

The hardware recommendation: a Google Coral USB Accelerator (~€60) plugged into the Pi 4 enables real-time object detection at 30+ FPS with negligible CPU load. Without it, detection is still possible but slower and more CPU-intensive.

# frigate config.yml
cameras:
  front_door:
    ffmpeg:
      inputs:
        - path: rtsp://192.168.1.200:8554/cam
          roles:
            - detect
    detect:
      width: 1280
      height: 720
      fps: 5
    objects:
      track:
        - person
        - car

Takeaway: Frigate + Home Assistant + Coral TPU is the local, private alternative to cloud camera subscriptions. Motion detection in OpenCV is 20 lines of Python.