# mahirqt/kiddycanvas architecture

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

- Interactive diagram: https://gitdiagram.com/mahirqt/kiddycanvas
- Repository: https://github.com/mahirqt/kiddycanvas

## Overview

##### Project Type & Purpose
- KiddyCanvas is a full-stack web application:
- Backend: Flask server exposing an API endpoint
- AI/ML: a pre-trained TensorFlow/Keras CNN (EMNIST) for character recognition
- TTS: pyttsx3 engine to speak out the predicted character

##### Top-Level Components
- Frontend
- index.html (Flask template)
- style.css (UI styling)
- script.js (Canvas drawing, AJAX to backend)
- Backend
- app.py (Flask app: serves UI, handles POST image data, returns JSON/text)
- render.yaml (deployment config)
- requirements.txt (dependencies: Flask, TensorFlow, Pillow, pyttsx3, etc.)
- Machine Learning
- cnn_emnist_model.h5 (trained CNN)
- label_mapping.npy (map from model index to character)
- EMNIST raw files under emnist_balanced/ (training/testing data, only used for retraining)
- Text-to-Speech
- pyttsx3 invoked in app.py after prediction
- Miscellaneous
- .gitignore, “how to use.md”, static/animated_gifs (optional assets)

##### Relationships & Data Flows
- User draws on HTML5 Canvas
→ script.js captures drawing, sends image payload (base64 PNG) via AJAX POST to Flask endpoint
→ app.py receives image, uses Pillow to preprocess (resize, grayscale, normalize)
→ Preprocessed image passed to CNN model (.h5) for prediction
→ Numeric class index mapped to character via label_mapping.npy
→ Flask returns JSON {“prediction”: “A”} and simultaneously invokes pyttsx3 to speak “A for Apple”
→ script.js plays audio (or audio triggered server-side), updates UI with predicted letter

##### Architectural Patterns & Principles
- MVC-style separation:
- View: templates/index.html + static assets
- Controller: Flask routes in app.py
- Model: Keras CNN + label mapping
- Single-page interaction with REST-style API call for recognition
- Modularity: ML, TTS, and web layers clearly decoupled

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        Browser["Browser<br/>(HTML5/CSS3/JS Canvas)"]:::frontend
        Template["templates/index.html"]:::frontend
        Style["static/style.css"]:::frontend
        Script["static/script.js"]:::frontend
        Animated["static/animated_gifs/"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        FlaskServer["Flask Server<br/>(app.py)"]:::backend
        RenderConfig["render.yaml"]:::backend
        Requirements["requirements.txt"]:::backend
    end

    %% ML Model Layer
    subgraph "ML Model Layer"
        direction TB
        Preprocessor["Preprocessor<br/>(Pillow)"]:::ml
        CNN["cnn_emnist_model.h5"]:::ml
        LabelMap["label_mapping.npy"]:::ml
        RawData["emnist_balanced/"]:::ml
        RetrainScript["emnist_cnn.py"]:::ml
    end

    %% TTS Layer
    subgraph "TTS Layer"
        direction TB
        TTS["TTS Engine<br/>(pyttsx3)"]:::tts
    end

    %% Data Flows
    Browser -->|"POST base64-PNG"| FlaskServer
    FlaskServer -->|"serves UI/template"| Browser
    FlaskServer -->|"preprocess image"| Preprocessor
    Preprocessor -->|"tensor input"| CNN
    CNN -->|"class index"| LabelMap
    LabelMap -->|"character"| FlaskServer
    FlaskServer -->|"JSON {prediction}"| Browser
    FlaskServer -.->|"invoke TTS"| TTS
    TTS -->|"audio output"| Browser

    %% Click Events
    click Template "https://github.com/mahirqt/kiddycanvas/blob/main/templates/index.html"
    click Style "https://github.com/mahirqt/kiddycanvas/blob/main/static/style.css"
    click Script "https://github.com/mahirqt/kiddycanvas/blob/main/static/script.js"
    click Animated "https://github.com/mahirqt/kiddycanvas/tree/main/static/animated_gifs/"
    click FlaskServer "https://github.com/mahirqt/kiddycanvas/blob/main/app.py"
    click RenderConfig "https://github.com/mahirqt/kiddycanvas/blob/main/render.yaml"
    click Requirements "https://github.com/mahirqt/kiddycanvas/blob/main/requirements.txt"
    click CNN "https://github.com/mahirqt/kiddycanvas/blob/main/cnn_emnist_model.h5"
    click LabelMap "https://github.com/mahirqt/kiddycanvas/blob/main/label_mapping.npy"
    click RawData "https://github.com/mahirqt/kiddycanvas/tree/main/emnist_balanced/"
    click RetrainScript "https://github.com/mahirqt/kiddycanvas/blob/main/emnist_cnn.py"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#000,stroke-width:1px
    classDef ml fill:#FFD580,stroke:#000,stroke-width:1px
    classDef tts fill:#DDA0DD,stroke:#000,stroke-width:1px
```
