# anuragaryanyt/number_plate_detection architecture

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

- Interactive diagram: https://gitdiagram.com/anuragaryanyt/number_plate_detection
- Repository: https://github.com/anuragaryanyt/number_plate_detection

## Overview

##### Project type and purpose
- This is a full-stack, monolithic web application whose purpose is real-time number-plate detection and recognition. It combines a video-processing pipeline (OpenCV + Tesseract OCR) with a Flask-based backend, a MongoDB database, and a small frontend (HTML/CSS templates).

##### File structure
Top-level:
- myapp.py – the Flask application entry point, holds routes, video-processing thread, and system logic.
- requirements.txt – lists Python dependencies.
- static/ – CSS assets (login, record and result page styles).
- templates/ – Jinja2 HTML templates (login, index/dashboard, recognized_plates, records).
- README.md – project overview, architecture, workflow, and code details.

##### README for additional insights
- Core libraries: OpenCV (cascade detection), pytesseract, Flask, PyMongo.
- Workflow: load vehicle DB, spin up a video-capture loop (process_video), detect plates, OCR, lookup/insert in MongoDB, annotate and stream frames.
- Flask routes: /login, /index (dashboard—last 24h), /records (all vehicles), /recognized_plates (current session list), /video_feed (multipart JPEG stream), /add_vehicle (POST).
- Authentication: basic session-based admin credentials.
- Concurrent design: video processing runs in a separate thread within the same Flask app context.

##### Main components & interactions (
A. Camera / Video Source
B. Video-Processing Service
- OpenCV CascadeClassifier (haarcascade_russian_plate_number.xml)
- Image pre-processing (grayscale, ROI extraction, contour tracing)
- Tesseract OCR engine (external executable)
- Plate detection & recognition logic
C. Flask Backend
- REST / HTML endpoints (routes)
- Video feed generator (multipart streaming)
- Session/auth module
- Vehicle management module (add_vehicle)
- Dashboard module (/index, /records, /recognized_plates)
D. Database
- MongoDB Atlas (vehicles and history collections)
E. Web Frontend
- Browser client
- HTML templates + CSS (static assets)
- JavaScript (if any for video display)
F. Admin User
- Interacts via browser (login, add vehicle, view data)

##### Data flows & relationships
1. Browser → Flask (HTTP GET/POST for login, pages, add_vehicle)
2. Flask → MongoDB (queries: load all vehicles into in-memory dict; writes: new vehicle, history insert)
3. Camera → Video-Processing thread → Flask (stream frames to /video_feed)
4. Video Processing → MongoDB (history inserts), → in-memory dict (lookup)
5. Flask → Browser (streamed MJPEG, rendered templates populated with DB query results)

##### Architectural patterns & design principles
- Single-process, multi-threaded monolith
- MVC-like division:
- Model = MongoDB collections + in-memory vehicle_database
- View = Jinja2 templates + video stream
- Controller = Flask routes + process_video function
- Separation of concerns:
- Video/vision logic isolated in process_video()
- HTTP logic in Flask routes
- DB logic centralized in load_vehicle_database() and route handlers
- Real-time streaming via generator pattern and multipart responses

##### Final checklist
- Clear component labels (Camera, Video Processor, Flask App, MongoDB, Browser Admin)
- External dependencies (Tesseract executable path, Haar cascade file)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entities
    Browser["Browser (Admin)"]:::user
    Camera["Camera / Video Source"]:::external

    subgraph "Monolithic Application"
        subgraph "Video-Processing Service"
            VP["Video-Processing Service"]:::processing
            cascade["Haar Cascade File"]:::external
            tess["Tesseract OCR Executable"]:::external
            cache["In-Memory Vehicle Cache"]:::database
        end
        subgraph "Flask Backend"
            FB["Flask Backend"]:::backend
        end
        subgraph "Database Module"
            DB["MongoDB (vehicles & history)"]:::database
        end
        subgraph "Frontend Templates"
            T1["index.html"]:::view
            T2["login.html"]:::view
            T3["recognized_plates.html"]:::view
            T4["records.html"]:::view
        end
        subgraph "Static Assets"
            S1["loginstyle.css"]:::static
            S2["mystyle.css"]:::static
            S3["recognizedstyle.css"]:::static
            S4["recordstyle.css"]:::static
        end
        subgraph "Dependencies & Entry"
            R1["requirements.txt"]:::doc
            R2["README.md"]:::doc
        end
    end

    %% Data Flows
    Camera -->|"capture frames"| VP
    cascade --> VP
    tess --> VP
    VP -->|"detectMultiScale & OCR"| cache
    DB -->|"load vehicles (find())"| cache
    VP -->|"insert history (insert_one())"| DB
    VP -->|"stream frames generator"| FB
    Browser -->|"GET/POST /login, /index, /add_vehicle, /records, /recognized_plates"| FB
    FB -->|"find & insert queries"| DB
    FB -->|"/video_feed MJPEG stream"| Browser
    FB -->|"/index"| T1
    FB -->|"/login"| T2
    FB -->|"/recognized_plates"| T3
    FB -->|"/records"| T4
    FB -->|"serve CSS assets"| S1
    FB -->|"serve CSS assets"| S2
    FB -->|"serve CSS assets"| S3
    FB -->|"serve CSS assets"| S4

    %% Click Events
    click VP "https://github.com/anuragaryanyt/number_plate_detection/blob/main/myapp.py"
    click FB "https://github.com/anuragaryanyt/number_plate_detection/blob/main/myapp.py"
    click DB "https://github.com/anuragaryanyt/number_plate_detection/blob/main/myapp.py"
    click T1 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/templates/index.html"
    click T2 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/templates/login.html"
    click T3 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/templates/recognized_plates.html"
    click T4 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/templates/records.html"
    click S1 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/static/loginstyle.css"
    click S2 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/static/mystyle.css"
    click S3 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/static/recognizedstyle.css"
    click S4 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/static/recordstyle.css"
    click R1 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/requirements.txt"
    click R2 "https://github.com/anuragaryanyt/number_plate_detection/blob/main/README.md"

    %% Styles
    classDef processing fill:#fdd835,stroke:#f9a825,color:#000
    classDef backend    fill:#42a5f5,stroke:#1e88e5,color:#fff
    classDef database   fill:#66bb6a,stroke:#43a047,color:#fff
    classDef view       fill:#ab47bc,stroke:#8e24aa,color:#fff
    classDef static     fill:#8d6e63,stroke:#6d4c41,color:#fff
    classDef doc        fill:#90a4ae,stroke:#607d8b,color:#fff
    classDef external   fill:#bdbdbd,stroke:#9e9e9e,color:#000
    classDef user       fill:#ce93d8,stroke:#ab47bc,color:#000
```
