# vibhu-dixit/openmonitor architecture

> Architecture diagram of the GitHub repository vibhu-dixit/openmonitor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vibhu-dixit/openmonitor
- Repository: https://github.com/vibhu-dixit/openmonitor

## Overview

##### Project Type and Purpose
- This is a full-stack web application (“OpenMonitor”) built on Django.
- Purpose: real-time video monitoring, object detection via YOLOv8, historical logging, and alerting (email/SMS) when unauthorized access is detected.

##### Top-Level Architectural Components
a. Video Sources (CCTV/IP Cameras)
b. Inference Engine
- YOLOv8 model (yolov8s.pt) loaded in `backend/messaging_alert_system/inference_manager` and supplemented by code in `model/video_stream.py` and `model/yolo.py`.
- Uses OpenCV to capture frames and run object detection.
c. Alert & Logging Subsystem
- Django app `messages_logger` writes events to SQLite3 (`db.sqlite3`) and triggers external notifications.
- Configured in `backend/messaging_alert_system/messages_logger/models.py` & `views.py`.
d. Web Application (Django + Channels)
- REST/admin/API endpoints plus WebSocket consumers for live video (`messaging_alert_system/consumers.py`, `routing.py`).
- Templates (`vid_stream.html`, `show_alert.html`) and static assets serve the dashboard.
e. Database
- SQLite3 (lightweight, file‐based) stores user/access data, event logs, zone configurations.
f. External Services
- SMTP for email alerts, third-party SMS API for text messaging.
g. Admin Interface & Configuration
- Django admin (`static/admin/*`) and Django REST Framework docs (`static/rest_framework/*`) for management.

##### Component Interactions / Data Flows
1. Cameras → Inference Engine: raw video feeds streamed or polled.
2. Inference Engine → Detection Results:
- If unauthorized object/person detected →
- Write an “alert” record to SQLite via `messages_logger`.
- Invoke email/SMS API to dispatch notifications.
- Emit a WebSocket message to the front end.
3. Frontend (browser) ← WebSocket Consumer (Django Channels): updates real-time video display and alert banners.
4. Admin/REST UI ↔ Database: CRUD on zones, users, historical logs via Django admin/DRF.

##### Architectural Patterns & Principles
- MVC (Django apps: Models, Views, Templates).
- Event-driven/asynchronous messaging (Django Channels for WebSockets).
- Separation of concerns:
- `inference_manager` for CV/inference logic
- `messages_logger` for persistence & notifications
- Single‐file DB (SQLite) for simplicity in a hackathon context.

a. Cameras at the top left feeding into the Inference Engine component.
– one to Database (SQLite) via Message Logger,
– one to External Services (Email/SMS).
d. On the far right or bottom, show Django Admin/DRF connecting to the Database.
– Inference Engine: `inference_manager/models.py`, `model/yolo.py`
– Message Logger: `messages_logger/views.py`, `models.py`
– Web App: `consumers.py`, `views.py`, `templates/vid_stream.html`

##### Final Touches
- Every component has a clear, human-readable label.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Sources
    subgraph "External Sources"
        direction TB
        Camera["CCTV/IP Cameras"]:::external
    end

    %% Application Logic
    subgraph "Application Logic"
        direction TB

        subgraph "Inference Engine" 
            direction TB
            FG["Frame Grabber\nmodel/video_stream.py"]:::backend
            INF_MODEL["Inference Manager Models\nbackend/messaging_alert_system/inference_manager/models.py"]:::backend
            INF_VIEW["Inference Manager Views\nbackend/messaging_alert_system/inference_manager/views.py"]:::backend
            INF_WEIGHTS["YOLO Weights\nbackend/messaging_alert_system/inference_manager/yolov8s.pt"]:::backend
            YOLO_SCRIPT["YOLO Detector\nmodel/yolo.py"]:::backend
        end

        subgraph "Alert & Logging Subsystem"
            direction TB
            ML_MODEL["Logger Models\nbackend/messaging_alert_system/messages_logger/models.py"]:::backend
            ML_VIEW["Logger Views\nbackend/messaging_alert_system/messages_logger/views.py"]:::backend
            ML_URLS["Logger URLs\nbackend/messaging_alert_system/messages_logger/urls.py"]:::backend
            ML_ADMIN["Logger Admin\nbackend/messaging_alert_system/messages_logger/admin.py"]:::backend
        end

        subgraph "Web Application"
            direction TB

            subgraph "Real-time Updates"
                direction TB
                CONS["WebSocket Consumers\nbackend/messaging_alert_system/messaging_alert_system/consumers.py"]:::backend
                ROUTE["Routing\nbackend/messaging_alert_system/messaging_alert_system/routing.py"]:::backend
            end

            subgraph "REST/Admin Endpoints"
                direction TB
                API_VIEW["Django Views\nbackend/messaging_alert_system/messaging_alert_system/views.py"]:::backend
                API_URL["URLs\nbackend/messaging_alert_system/messaging_alert_system/urls.py"]:::backend
            end

            subgraph "Frontend Templates"
                direction TB
                VID["Video Stream Template\nbackend/messaging_alert_system/messaging_alert_system/templates/vid_stream.html"]:::frontend
                ALERT_TPL["Alert Template\nbackend/messaging_alert_system/messaging_alert_system/templates/show_alert.html"]:::frontend
            end
        end
    end

    %% Data & External Services
    subgraph "Data Layer"
        direction TB
        DB["SQLite3 Database\nbackend/messaging_alert_system/db.sqlite3"]:::data
    end

    subgraph "External Services"
        direction TB
        Email["SMTP Email API"]:::external
        SMS["SMS Third-Party API"]:::external
    end

    %% Admin & Static
    subgraph "Admin & Documentation"
        direction TB
        ADMIN_STATIC["Django Admin Static\nbackend/messaging_alert_system/static/admin/"]:::external
        DRF_STATIC["DRF Docs Static\nbackend/messaging_alert_system/static/rest_framework/"]:::external
    end

    %% Core Flows
    Camera -->|"Video frames"| FG
    FG -->|"Frames"| YOLO_SCRIPT
    YOLO_SCRIPT -->|"Use Models & Views"| INF_MODEL
    YOLO_SCRIPT -->|"Use Models & Views"| INF_VIEW
    YOLO_SCRIPT -->|"Weights"| INF_WEIGHTS

    YOLO_SCRIPT -->|"Detection event"| ML_MODEL
    ML_MODEL -->|"DB write"| DB
    ML_MODEL -->|"Email/SMS call"| Email
    ML_MODEL -->|"Email/SMS call"| SMS
    ML_MODEL -->|"Emit WebSocket update"| CONS

    CONS -->|"/ws/video/"| VID
    ROUTE -->|routing| CONS

    API_VIEW <-->|"HTTP/REST"| DB
    API_URL -->|routes| API_VIEW

    %% Static connections
    API_VIEW -->|serves| ADMIN_STATIC
    API_VIEW -->|serves| DRF_STATIC

    %% Click Events
    click FG "https://github.com/vibhu-dixit/openmonitor/blob/main/model/video_stream.py"
    click INF_MODEL "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/inference_manager/models.py"
    click INF_VIEW "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/inference_manager/views.py"
    click INF_WEIGHTS "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/inference_manager/yolov8s.pt"
    click YOLO_SCRIPT "https://github.com/vibhu-dixit/openmonitor/blob/main/model/yolo.py"
    click ML_MODEL "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messages_logger/models.py"
    click ML_VIEW "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messages_logger/views.py"
    click ML_URLS "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messages_logger/urls.py"
    click ML_ADMIN "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messages_logger/admin.py"
    click CONS "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/consumers.py"
    click ROUTE "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/routing.py"
    click API_VIEW "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/views.py"
    click API_URL "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/urls.py"
    click VID "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/templates/vid_stream.html"
    click ALERT_TPL "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/messaging_alert_system/templates/show_alert.html"
    click DB "https://github.com/vibhu-dixit/openmonitor/blob/main/backend/messaging_alert_system/db.sqlite3"
    click ADMIN_STATIC "https://github.com/vibhu-dixit/openmonitor/tree/main/backend/messaging_alert_system/static/admin/"
    click DRF_STATIC "https://github.com/vibhu-dixit/openmonitor/tree/main/backend/messaging_alert_system/static/rest_framework/"

    %% Styles
    classDef frontend fill:#D19FE8,stroke:#333,stroke-width:1px
    classDef backend fill:#9EC1CF,stroke:#333,stroke-width:1px
    classDef data fill:#A8D5BA,stroke:#333,stroke-width:1px
    classDef external fill:#F4B183,stroke:#333,stroke-width:1px
```
