# maharabhi01/handwritten_digit-reconizer architecture

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

- Interactive diagram: https://gitdiagram.com/maharabhi01/handwritten_digit-reconizer
- Repository: https://github.com/maharabhi01/handwritten_digit-reconizer

## Overview

#### Purpose and Entry Point
This is a single-file Streamlit application for classifying uploaded handwritten digit images with a scikit-learn K-Nearest Neighbors model. The executable UI and all application logic are concentrated in `App.py`.

#### Interactive Application Shell
`App.py` runs under the Streamlit runtime, providing the browser-facing controls, image upload handling, prediction display, accuracy output, and digit-reference visualization. The user selects the neighbor count (K) and supplies a PNG/JPG/JPEG digit image.

#### Dataset and Model Training
The app loads scikit-learn’s built-in `load_digits()` dataset: 1,797 labeled 8×8 grayscale samples, represented as 64 flattened features. Within `App.py`, data is split into train/test partitions and a KNN classifier is trained using the currently selected K value.

#### Image Preprocessing Boundary
Uploaded images enter the system through Streamlit and are processed in `App.py` using Pillow/NumPy. The preprocessing pipeline converts the image to grayscale, resizes it to 8×8, conditionally inverts it based on background characteristics, scales pixels into the dataset’s 0–16 range, and flattens the result into the model’s 64-feature input contract.

#### Inference and Evaluation Flow
The processed upload is passed to the trained KNN model in `App.py`, which returns a digit class prediction. The same model is evaluated against the held-out test partition to produce an accuracy value for the selected K, coupling model selection, retraining, evaluation, and interactive inference in one request-driven UI flow.

#### Visualization and Dependencies
`App.py` uses Matplotlib to render the original upload, processed 8×8 image, and reference digit images. Core runtime dependencies are Python, Streamlit, NumPy, Pillow, Matplotlib, and scikit-learn; the dataset is obtained locally through scikit-learn rather than from a separate database or remote service.

Main entry points: [`App.py`](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py) (KNN classifier)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Interactive UI"]
  node_user(("User<br/>actor"))
  node_streamlit{{"Streamlit runtime<br/>web runtime"}}
  node_app["App.py<br/>application module<br/>[App.py]"]
  node_controls["K selector<br/>UI control<br/>[App.py]"]
  node_upload["Image upload<br/>UI input<br/>[App.py]"]
  node_display["Results and visualizations<br/>UI output<br/>[App.py]"]
end

subgraph group_ml["Digit Classification Pipeline"]
  node_digits[("sklearn digits dataset<br/>local dataset<br/>[App.py]")]
  node_split["Train/test split<br/>data partition<br/>[App.py]"]
  node_preprocess["Image preprocessing<br/>transform pipeline<br/>[App.py]"]
  node_knn["KNN classifier<br/>scikit-learn model<br/>[App.py]"]
  node_prediction["Digit prediction<br/>inference result<br/>[App.py]"]
  node_accuracy["Test accuracy<br/>evaluation metric<br/>[App.py]"]
  node_reference["Reference digit images<br/>Matplotlib visualization<br/>[App.py]"]
end

node_user -->|"uses"| node_streamlit
node_streamlit -->|"executes"| node_app
node_app -->|"renders"| node_controls
node_app -->|"accepts"| node_upload
node_controls -->|"sets K"| node_knn
node_app -->|"loads"| node_digits
node_digits -->|"partitions"| node_split
node_split -->|"training data"| node_knn
node_upload -->|"image bytes"| node_preprocess
node_preprocess -->|"64 features"| node_knn
node_knn -->|"predicts"| node_prediction
node_split -->|"held-out test set"| node_accuracy
node_knn -->|"evaluates"| node_accuracy
node_digits -->|"provides examples"| node_reference
node_prediction -->|"shows"| node_display
node_accuracy -->|"shows"| node_display
node_preprocess -->|"shows 8×8 image"| node_display
node_reference -->|"shows"| node_display

click node_app "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_controls "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_upload "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_display "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_digits "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_split "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_preprocess "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_knn "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_prediction "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_accuracy "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"
click node_reference "https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_user,node_streamlit,node_app,node_controls,node_upload,node_display toneBlue
class node_digits,node_split,node_preprocess,node_knn,node_prediction,node_accuracy,node_reference toneAmber
```

## Components

### Interactive UI

- **User**: actor
- **Streamlit runtime**: web runtime · Hosts the browser-facing application.
- [App.py](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): application module · Contains all UI, preprocessing, training, evaluation, and inference logic. · `App.py`
- [K selector](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): UI control · Selects the KNN neighbor count. · `App.py`
- [Image upload](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): UI input · Accepts PNG, JPG, and JPEG digit images. · `App.py`
- [Results and visualizations](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): UI output · Shows prediction, accuracy, upload, processed image, and references. · `App.py`

### Digit Classification Pipeline

- [sklearn digits dataset](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): local dataset · 1,797 labeled 8×8 grayscale digit samples. · `App.py`
- [Train/test split](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): data partition · Separates digit features and labels for training and evaluation. · `App.py`
- [Image preprocessing](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): transform pipeline · Grayscale, resize, conditional invert, scale to 0–16, and flatten to 64 features. · `App.py`
- [KNN classifier](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): scikit-learn model · Trained with the selected K value. · `App.py`
- [Digit prediction](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): inference result · `App.py`
- [Test accuracy](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): evaluation metric · Computed on the held-out test partition for the selected K. · `App.py`
- [Reference digit images](https://github.com/maharabhi01/handwritten_digit-reconizer/blob/main/App.py): Matplotlib visualization · Displays dataset examples for comparison. · `App.py`

## Connections

- User → Streamlit runtime: uses
- Streamlit runtime → App.py: executes
- App.py → K selector: renders
- App.py → Image upload: accepts
- K selector → KNN classifier: sets K
- App.py → sklearn digits dataset: loads
- sklearn digits dataset → Train/test split: partitions
- Train/test split → KNN classifier: training data
- Image upload → Image preprocessing: image bytes
- Image preprocessing → KNN classifier: 64 features
- KNN classifier → Digit prediction: predicts
- Train/test split → Test accuracy: held-out test set
- KNN classifier → Test accuracy: evaluates
- sklearn digits dataset → Reference digit images: provides examples
- Digit prediction → Results and visualizations: shows
- Test accuracy → Results and visualizations: shows
- Image preprocessing → Results and visualizations: shows 8×8 image
- Reference digit images → Results and visualizations: shows
