# mcguirepr89/birdnet-pi architecture

> Architecture diagram of the GitHub repository mcguirepr89/birdnet-pi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mcguirepr89/birdnet-pi
- Repository: https://github.com/mcguirepr89/birdnet-pi

## Overview

##### Project Type & Purpose
- BirdNET-Pi is a complete embedded system that leverages machine learning (using pre-built TFLite models) to classify bird sounds captured from a USB microphone.
- It empowers end users with a full-stack setup that combines low-level audio recording, real-time ML inference, data logging, and web-based management/control (via PHP pages and integrated tools like file managers, gotty, and database utilities).
- The system is designed to run on Raspberry Pi hardware with a native ecosystem (Python scripts, shell scripts, PHP web UI, cron jobs, etc.).

##### Analyzing the File Structure
- Top-Level Directories:
- "homepage": Contains the PHP files, stylesheets, and other resources that comprise the web interface.
- "model": Houses various TensorFlow Lite models (*.tflite) and related label files (ZIPs and TXT) used for bird classification.
- "scripts": A diverse set of scripts (shell, Python, PHP) to manage core functionality such as recording, analysis, installation, system control, logging, database management, and notifications.
- "templates" and "tests": Provide boilerplate configurations (e.g., bang scripts for cron, tmux, bashrc) and testing routines for ensuring functionality.
- Configuration and Metadata Files:
- Files like .flake8, .github configurations, and requirements.txt indicate adherence to coding standards, continuous integration for Python, and dependency management.
- Several installer and update scripts ensure the platform is self-updating and can manage system-wide settings.

##### Insights from the README
- The README confirms the system’s purpose, compatibility with various Raspberry Pi models, and its reliance on BirdNET’s ML framework.
- It describes integration points like the web interface (accessed via a local URL), the underlying ML backend powered by TFLite, and various system control scripts.

a. Main Components:
- Hardware:
- Raspberry Pi (target models such as 4B, 400, etc.)
- USB Microphone / Sound Card
- Audio Input Subsystem:
- Recording scripts (e.g., birdnet_recording.sh in scripts) that capture microphone data.
- Machine Learning (Classification) Subsystem:
- TFLite models stored in the "model" directory.
- Analysis scripts (e.g., analyze.py, species.py) that process audio data for bird identification.
- Data Management Subsystem:
- Storage of logs, clips, and metadata (using SQLite3 or other file-based databases).
- Scripts for cleanup, disk management, and data extraction.
- Web Interface & API Layer:
- PHP pages and associated assets (within "homepage") that provide a web dashboard.
- File Manager, Adminer, gotty (web terminal) for administration.
- Notification system integration (using Apprise and relevant utility scripts).
- Utility & Support Services:
- Cron job templates for automated tasks (e.g., cleanup.cron, weekly_report.cron).
- Installation and update scripts.

b. Relationships and Interactions:
- Audio Input Subsystem → ML Classification Subsystem: Captured audio is piped into analysis scripts that use TFLite models for real-time inference.
- ML Classification Subsystem → Data Management: Inferences (and possibly raw or processed audio clips) are stored/logged.
- Data Management Subsystem ↔ Web Interface: The web UI retrieves data (logs, results) for display and offers administrative views.
- Utility Scripts (e.g., update, disk management) interact with the above components to maintain health, update configurations, and provide notifications.

c. Important Architectural Patterns & Technologies:
- A layered structure separating audio acquisition, ML processing, and UI:
- The “data flow” pattern from sensor capture to inference to storage, and finally to user-display via the web interface.
- Use of shell, Python, and PHP components to address specific responsibilities (each with its own set of scripts).
- Modular design allowing extensibility (e.g., adding additional notification platforms via Apprise) and integration points (e.g., BirdWeather, FTP server, Adminer).
- Reliance on pre-built machine learning binaries (TFLite) on embedded hardware ensuring efficient on-device inference.

d. Relevant Technologies:
- TensorFlow Lite models for ML inference.
- PHP for the web user interface and service monitoring.
- Python and shell scripts for system operations and audio processing.
- SQLite for lightweight database management.
- Supporting tools such as Caddy (for web hosting), gotty (for remote terminal access), and Adminer (for database maintenance).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Subsystem
    subgraph "Hardware"
        RP("Raspberry Pi"):::hardware
        Mic("USB Microphone"):::hardware
    end

    %% Audio Capture Subsystem
    subgraph "Audio Capture"
        AudioIn("Audio Input (Recording Script)"):::audio
    end

    %% Machine Learning Subsystem
    subgraph "Machine Learning"
        MLModels("ML Models (TFLite)"):::ml
        MLAnalysis("ML Analysis Engine"):::ml
    end

    %% Data Management Subsystem
    subgraph "Data Management"
        DataLog("Data Logger & DB"):::data
    end

    %% Web Interface & External Control
    subgraph "Web & Control"
        WebUI("Web Interface & API"):::web
        FileMgr("File Manager & Remote Terminal"):::web
        Notify("Notification System"):::notification
    end

    %% Utility & Support Services
    subgraph "Utility & Support"
        Utilities("Utility Services (Cron, Updates)"):::utility
    end

    %% Connections
    Mic -->|"captures"| AudioIn
    RP -->|"hosts"| AudioIn
    AudioIn -->|"sends_audio"| MLAnalysis
    MLModels ---|"used_by"| MLAnalysis
    MLAnalysis -->|"logs_results"| DataLog
    DataLog -->|"serves_data"| WebUI
    DataLog -->|"alerts"| Notify
    WebUI -->|"administers"| FileMgr
    Utilities -->|"maintains"| DataLog
    Utilities -->|"updates"| AudioIn
    Utilities -->|"updates"| MLAnalysis
    Notify -->|"notifies"| WebUI

    %% Click Events
    click AudioIn "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/birdnet_recording.sh"
    click MLModels "https://github.com/mcguirepr89/birdnet-pi/tree/main/model/"
    click MLAnalysis "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/analyze.py"
    click DataLog "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/birdnet_log.sh"
    click DataLog "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/createdb.sh"
    click WebUI "https://github.com/mcguirepr89/birdnet-pi/tree/main/homepage/"
    click FileMgr "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/filemanager/filemanager.php"
    click FileMgr "https://github.com/mcguirepr89/birdnet-pi/tree/main/scripts/gotty/"
    click Utilities "https://github.com/mcguirepr89/birdnet-pi/blob/main/templates/cleanup.cron"
    click Utilities "https://github.com/mcguirepr89/birdnet-pi/blob/main/templates/weekly_report.cron"
    click Utilities "https://github.com/mcguirepr89/birdnet-pi/blob/main/newinstaller.sh"
    click Utilities "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/install_birdnet.sh"
    click Notify "https://github.com/mcguirepr89/birdnet-pi/blob/main/scripts/utils/notifications.py"

    %% Styles
    classDef hardware fill:#b3cde0,stroke:#333,stroke-width:2px;
    classDef audio fill:#ccebc5,stroke:#333,stroke-width:2px;
    classDef ml fill:#ffebcd,stroke:#333,stroke-width:2px;
    classDef data fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef web fill:#dcdcdc,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffcccb,stroke:#333,stroke-width:2px;
    classDef notification fill:#e6e6fa,stroke:#333,stroke-width:2px;
```
