Automating Your Smart Gate with Raspberry Pi 5 and Coral TPU: Frigate vs. Custom Script

admin Avatar

In the world of home automation, securing your property is a top priority. One way to achieve this is by automating your gate with advanced object detection and license plate recognition (ALPR) technology. In this blog post, we’ll explore two approaches to automating your gate using the Raspberry Pi 5 and Coral TPU NVMe: one using the powerful Frigate software and the other a custom Python script. We’ll also provide a detailed parts list to help you get started.

Parts List:
  • Raspberry Pi 5 (4GB) – RM280.20
  • Coral TPU M.2 B+M Module – RM123.99
  • Raspberry Pi 5 M.2 HAT+ – RM56.04
  • 27W USB-C PD Power Supply – RM56.04
  • Raspberry Pi 5 Active Cooler (Heatsink/Fan) – RM23.35

All parts are from mouser electronics. Not including shipping from UK warehouse which costs RM73.

Approach 1: Using Frigate

Frigate is an open-source NVR with real-time object detection capabilities. It integrates seamlessly with Home Assistant, providing an all-in-one solution for monitoring, recording, and automating tasks based on detected events.

Key Features:
  • Real-Time Object Detection: Frigate uses TensorFlow Lite models optimized for Coral TPU to detect objects like cars and people in real-time.
  • ALPR Integration: While Frigate doesn’t natively support ALPR, you can integrate it with external tools like DeepStack or use custom object detection models for license plate recognition.
  • Home Assistant Integration: Frigate events can trigger Home Assistant automations, such as sending Telegram alerts or controlling smart devices.
  • Ease of Use: Frigate is easier to set up and configure compared to writing a custom script, making it ideal for those who prefer an out-of-the-box solution.
Pros and Cons:
  • Pros: Simplified setup, Home Assistant integration, reliable detection.
  • Cons: Limited customization, requires external ALPR tools for license plate recognition.

Approach 2: Custom Python Script

For those who enjoy getting their hands dirty with code, a custom Python script offers unmatched flexibility and control. With this approach, you can directly leverage the Coral TPU’s processing power for real-time object detection and ALPR, integrating the results with your home automation system.

Key Features:
  • Full Customization: Write Python code to handle exactly what you need, from object detection to ALPR, and automate actions based on the results.
  • Direct Integration with Coral TPU: Use TensorFlow Lite models optimized for Coral to accelerate object detection.
  • Advanced Automation: Create complex conditions, such as checking if the gate is already open or if a person is detected before triggering the gate.
  • Integration with Home Assistant: Use Home Assistant’s API for controlling smart devices and sending notifications.
Pros and Cons:
  • Pros: Complete control, tailored automation, direct ALPR integration.
  • Cons: Steeper learning curve, more time-intensive to set up and maintain.

Sample Codes for Automating Your Gate Using Raspberry Pi 5 and Coral TPU

Below are simplified sample codes for both the Frigate approach and the custom Python script approach.

Approach 1: Frigate Configuration

Frigate requires a configuration file (frigate.yml) to set up cameras, detectors, and object detection rules.

frigate.yml Example:

detectors:
  coral:
    type: edgetpu
    device: pci

cameras:
  front_gate:
    ffmpeg:
      inputs:
        - path: rtsp://[USERNAME]:[PASSWORD]@[CAMERA_IP]:554/stream1
          roles:
            - detect
    objects:
      track:
        - person
        - car
    snapshots:
      enabled: True
      timestamp: False
      bounding_box: True
      retain:
        default: 10
    rtmp:
      enabled: False

mqtt:
  host: [MQTT_BROKER_IP]
  user: [MQTT_USERNAME]
  password: [MQTT_PASSWORD]
  topic_prefix: frigate

Home Assistant Automation Example:

You can trigger an automation in Home Assistant when Frigate detects a car with a known license plate.

alias: Car Detected at Front Gate
trigger:
  - platform: mqtt
    topic: frigate/events
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ 'car' in trigger.payload_json['after']['label'] }}"
        sequence:
          - service: notify.telegram
            data:
              message: "Car detected at the front gate. License plate recognized."
          - service: switch.turn_on
            target:
              entity_id: switch.autogate_wifi_switch
mode: single

Approach 2: Custom Python Script

The custom script leverages OpenCV, the Coral TPU, and OpenALPR for object and license plate detection. The script can trigger actions via Home Assistant API and send notifications via Telegram.

Sample Python Script:

import cv2
import requests
from openalpr import Alpr
from pycoral.adapters import common, detect
from pycoral.utils.edgetpu import make_interpreter

# Configuration Variables (Add your sensitive info here)
HOME_ASSISTANT_URL = 'http://[HOME_ASSISTANT_IP]:8123/api/services/switch/turn_on'
HA_BEARER_TOKEN = '[YOUR_HOME_ASSISTANT_TOKEN]'
TELEGRAM_TOKEN = '[YOUR_TELEGRAM_BOT_TOKEN]'
TELEGRAM_CHAT_ID = '[YOUR_TELEGRAM_CHAT_ID]'
RTSP_STREAM_URL = 'rtsp://[USERNAME]:[PASSWORD]@[CAMERA_IP]/stream1'
MODEL_PATH = 'mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
LABELS_PATH = 'coco_labels.txt'
KNOWN_PLATES = ['ABC1234', 'XYZ5678']

def load_labels(path):
    """Loads labels from file."""
    with open(path, 'r') as f:
        return {i: line.strip() for i, line in enumerate(f)}

LABELS = load_labels(LABELS_PATH)

def send_telegram_message(message):
    url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
    data = {"chat_id": TELEGRAM_CHAT_ID, "text": message}
    requests.post(url, data=data)

def turn_on_gate_switch():
    headers = {'Authorization': f'Bearer {HA_BEARER_TOKEN}', 'Content-Type': 'application/json'}
    data = {"entity_id": "switch.autogate_wifi_switch"}
    requests.post(HOME_ASSISTANT_URL, headers=headers, json=data)

def detect_objects(interpreter, frame):
    _, scale = common.set_resized_input(interpreter, frame.shape[:2], lambda size: cv2.resize(frame, size))
    interpreter.invoke()
    return detect.get_objects(interpreter, score_threshold=0.5, image_scale=scale)

def detect_plate_numbers(frame):
    alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data")
    if not alpr.is_loaded():
        return []

    results = alpr.recognize_ndarray(frame)
    alpr.unload()
    return [plate['plate'] for plate in results['results']]

def main():
    interpreter = make_interpreter(MODEL_PATH)
    interpreter.allocate_tensors()

    cap = cv2.VideoCapture(RTSP_STREAM_URL)
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        objects = detect_objects(interpreter, frame)
        for obj in objects:
            if LABELS.get(obj.id, '') == 'car':
                bbox = obj.bbox
                car_frame = frame[bbox.ymin:bbox.ymax, bbox.xmin:bbox.xmax]
                plate_numbers = detect_plate_numbers(car_frame)
                for plate in plate_numbers:
                    if plate in KNOWN_PLATES:
                        send_telegram_message(f"Car detected with plate {plate}.")
                        turn_on_gate_switch()

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

Both approaches provide robust solutions for automating your smart gate using the Raspberry Pi 5 and Coral TPU, with each offering unique advantages depending on your needs.

Conclusion:

Both approaches—using Frigate or a custom Python script—are powerful ways to automate your smart gate using the Raspberry Pi 5 and Coral TPU. If you prefer ease of use and quick setup, Frigate is the way to go. However, if you need full control and are comfortable with coding, a custom script offers unparalleled flexibility.

Whichever approach you choose, the parts list remains the same, ensuring you have the hardware needed to handle these tasks efficiently.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Posts