Skip to content

anpr2mqtt

anpr2mqtt

Rhizomatics Open Source

Ruff Coverage Tests pre-commit.ci status Github Deploy CodeQL Dependabot Updates

ANPR MQTT Bridge

A simple way to integrate CCTV cameras with built-in ANPR (Automatic Number Plate Recognition, aka ALPR or Automatic Licence Plate Recognition) to MQTT for Home Assistant integration, or any other MQTT consumer. Running under Docker is preferred but not necessary.

All that is needed is for the camera to be configured to upload images on plate recognition, by ftp, NAS or whatever else. ANPR2MQTT monitors the directory where the images lands and publishes plate information to MQTT. Its simple, requires no proprietary vendor APIs ( or differing ONVIF implementations ), and Home Assistant gets a copy of the actual annotated detection image to use on dashboards or to attach to notifications.

While intended for vehicle plate detection, it can be used to watch for and analyze any file, so for example uploaded face detection or line crossing images. A single anpr2mqtt instance can watch multiple paths and patterns for different cameras and events.

Features

  • File System Integration
    • Watches directory for ANPR camera images using Watchdog
      • Uses inotify on Linux, or equiv on other operating systems for efficient listening to file system events without continual polling
    • Extracts target ( for example licence plate ), timestamp and event type information from filenames
  • Home Assistant Integration.
    • Publishes events to MQTT for Home Assistant as a MQTT Sensor Entity
    • Auto-discovery configuration for Home Assistant
    • Creates MQTT Image Entity on Home Assistant for image snapshot, so no web access to ftp needed
  • Plate Enrichment
    • OCR-based extraction of fields using tesseract-ocr
      • By default direction detection (Forward/Reverse)
      • Corrections by regular expression to fix OCR mis-readings
    • Tracks and counts previous sightings
    • Configurable to classify plates as known, to be ignored or as a potential threat
    • Regular expression based corrections, for known plates that the ANPR sometimes mis-reads
    • UK Only
      • DVLA Lookup if API_KEY provided, for detailed MOT and tax information
      • Lookups cached for configurable time

Docker Deployment

Build and run with Docker, or use the example docker-compose.yaml

docker build -t anpr2mqtt .
docker run -d \
  --restart always \
  -v /path/to/ftp:/ftp \
  -e MQTT_HOST=your-mqtt-host \
  -e MQTT_PORT=1883 \
  -e MQTT_USER=username \
  -e MQTT_PASS=password \
  anpr2mqtt

Configuration

ANPR2MQTT uses pydantic-settings, which means configuration can happen in a variety of ways, and these can be combined - yaml configuration file, environment variables, Docker Secrets, built-in defaults, and .env file or command line arguments.

Environment Variables

pydantic-settings needs double underscores for environment variables in config sections. For example, MQTT__HOST means set the value host in the mqtt config section.

Env Variable Description Default
MQTT__HOST MQTT broker hostname localhost
MQTT__PORT MQTT broker port 1883
MQTT__TOPIC_ROOT MQTT topic for events anpr2mqtt
MQTT__USER MQTT username -
MQTT__PASS MQTT password -
LOG_LEVEL Python logging level INFO
DVLA__API_KEY API Key for Gov API Lookup -

Image Filename Format

Expected format: YYYYMMDDHHMMSSmmm_PLATE_VEHICLE_DETECTION.jpg

Example: 20180502174029596_A2GEO_VEHICLE_DETECTION.jpg

Image Box Coordinates

Where cameras provide an estimated direction for the vehicle, this can be captured via OCR and included in the response.

See OCR for explanation and examples.

Primary Dependencies

  • paho-mqtt - MQTT client
  • Pillow - Image processing
  • pytesseract - OCR for direction detection
  • structlog - Structured logging
  • httpx - API Client
  • hishel - API result caching
  • watchdog - File system monitoring (cross-platform)

Home Assistant Integration

See Home Assistant Integration for configuration and example notification automation.

Distribution

ANPR2MQTT is free and open sourced under the Apache 2.0 license.