# jatin-goyal-552/osdhack-22_accidentdetectionai architecture

> Architecture diagram of the GitHub repository jatin-goyal-552/osdhack-22_accidentdetectionai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jatin-goyal-552/osdhack-22_accidentdetectionai
- Repository: https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai

## Overview

##### Project type and purpose
- This is a full-stack web application with an integrated machine-learning component.
- Purpose: continuously monitor a video feed (highway traffic), run an accident-detection ML model (YOLO, PyTorch), and—upon detecting an accident—automatically notify nearby hospitals via email (including a Google Maps link) so that ambulances can be dispatched.

##### Main components of the system
a. Video Capture & ML Inference
- accidentdetectionapp/stream.py: captures frames (e.g., from highway_accident.mp4 or live camera)
- PyTorch model files accident2.pt and best (2).pt (YOLO-based accident detector)
- ML inference engine (runs inside Django or as a separate microservice)
b. Django Backend
- AccidentDetection/ (project config: settings.py, urls.py, wsgi.py/asgi.py)
- accidentdetectionapp/ (app code: views.py, urls.py, models.py, admin.py, migrations/)
- test.py (standalone test harness)
c. Database
- SQLite (db.sqlite3) for storing registered hospitals and incoming accident alerts
d. Front-end UI
- Templates: index.html (video display), hospital.html (hospital dashboard), register.html, test.html, etc.
- Static assets: CSS (static/css/style.css), JS (static/javascript/video.js)
- Bootstrap, custom JS for live video and AJAX calls
e. Notification & External Services
- Email (SMTP server configured in settings.py) to send alert + Google Maps link
- Google Maps API (for location links embedded in email)

##### Relationships and interactions
1. Client browser (ambulance driver or admin) opens index.html → requests video stream endpoint in Django
2. stream.py captures frames → passes them to the PyTorch model → if accident detected → triggers a Django view
3. View: saves an AccidentAlert record (models.py) in SQLite → looks up nearby hospitals (from DB) → sends emails with map links
4. Registered hospital users log in via hospital.html → see incoming alerts → accept or decline
5. On acceptance, another email (or webhook) can be sent to the ambulance driver with precise coordinates

##### Architectural patterns & design principles
- Layered architecture:
· Presentation layer (templates, static assets)
· Application layer (Django views, URL routing)
· ML inference layer (stream.py + PyTorch)
· Data layer (Django ORM + SQLite)
- Single-process vs. Microservice: ML inference can live inside Django or be split into a separate service behind an API
- Event-driven flow: detection event → alert workflow → email notifications

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    Camera["Camera / highway_accident.mp4"]:::ui
    click Camera "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/highway_accident.mp4"

    %% ML Inference Layer
    subgraph "ML Inference Service"
        Stream["stream.py"]:::ml
        Model1["accident2.pt"]:::ml
        Model2["best (2).pt"]:::ml
    end
    click Stream "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/stream.py"
    click Model1 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accident2.pt"
    click Model2 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/best (2).pt"

    Camera -->|"Frame Stream"| Stream
    Stream -->|"Loads Models"| Model1
    Stream --> Model2
    Stream -->|"Detection Event"| Backend

    %% Backend Layer
    subgraph "Django Backend"
        subgraph "Project Configuration"
            Settings["settings.py"]:::backend
            URLsProj["urls.py"]:::backend
            ASGI["asgi.py"]:::backend
            WSGI["wsgi.py"]:::backend
        end
        subgraph "App Logic & ORM"
            Models["models.py"]:::backend
            Views["views.py"]:::backend
            URLsApp["urls.py"]:::backend
            Admin["admin.py"]:::backend
            Apps["apps.py"]:::backend
            M1["0001_initial.py"]:::backend
            M2["0002_hospital.py"]:::backend
        end
        subgraph "Templates"
            Index["index.html"]:::ui
            HospitalT["hospital.html"]:::ui
            Register["register.html"]:::ui
            TestT["test.html"]:::ui
            Index2["index2.html"]:::ui
            Index3["index3.html"]:::ui
        end
        subgraph "Static Assets"
            CSS["style.css"]:::ui
            JS["video.js"]:::ui
        end
    end
    click Settings "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/AccidentDetection/settings.py"
    click URLsProj "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/AccidentDetection/urls.py"
    click ASGI "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/AccidentDetection/asgi.py"
    click WSGI "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/AccidentDetection/wsgi.py"
    click Models "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/models.py"
    click Views "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/views.py"
    click URLsApp "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/urls.py"
    click Admin "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/admin.py"
    click Apps "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/apps.py"
    click M1 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/migrations/0001_initial.py"
    click M2 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/migrations/0002_hospital.py"
    click Index "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/index.html"
    click HospitalT "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/hospital.html"
    click Register "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/register.html"
    click TestT "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/test.html"
    click Index2 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/index2.html"
    click Index3 "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/template/index3.html"
    click CSS "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/static/css/style.css"
    click JS "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/static/javascript/video.js"

    Backend -->|"Save Alert"| DB
    Backend -->|"Send Alert"| Email

    %% Database
    DB["SQLite\ndb.sqlite3"]:::db
    click DB "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/db.sqlite3"

    %% External Services
    Email["SMTP Email Service"]:::ext
    GoogleMaps["Google Maps API"]:::ext
    Email -->|"Generate Map Link"| GoogleMaps

    %% UIs
    HospitalUI["Hospital UI"]:::ui
    AmbulanceUI["Ambulance Driver UI"]:::ui
    Email -->|"Email + Map Link"| HospitalUI
    Email -->|"Email + Map Link"| AmbulanceUI
    HospitalUI -->|"Acknowledge/Decline"| Views
    AmbulanceUI -->|"Requests Stream/Register"| Views

    %% Entry Points
    Manage["manage.py"]:::backend
    README["README.md"]:::backend
    click Manage "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/manage.py"
    click README "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/README.md"
    TestHarness["tests.py"]:::backend
    StandaloneTest["test.py"]:::backend
    click TestHarness "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/accidentdetectionapp/tests.py"
    click StandaloneTest "https://github.com/jatin-goyal-552/osdhack-22_accidentdetectionai/blob/main/test.py"
    Manage --> Settings

    %% Styles
    classDef ml fill:#9f6,stroke:#482;
    classDef backend fill:#6af,stroke:#036;
    classDef db fill:#fa6,stroke:#a53;
    classDef ext fill:#c9f,stroke:#63a;
    classDef ui fill:#ccc,stroke:#666;
```
