How maharabhi01/handwritten_digit-reconizer works
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 KNN classifier
Components 13
Interactive UI
- User
- Streamlit runtime · Hosts the browser-facing application.
- App.py · Contains all UI, preprocessing, training, evaluation, and inference logic.
App.py - K selector · Selects the KNN neighbor count.
App.py - Image upload · Accepts PNG, JPG, and JPEG digit images.
App.py - Results and visualizations · Shows prediction, accuracy, upload, processed image, and references.
App.py
Digit Classification Pipeline
- sklearn digits dataset · 1,797 labeled 8×8 grayscale digit samples.
App.py - Train/test split · Separates digit features and labels for training and evaluation.
App.py - Image preprocessing · Grayscale, resize, conditional invert, scale to 0–16, and flatten to 64 features.
App.py - KNN classifier · Trained with the selected K value.
App.py - Digit prediction
App.py - Test accuracy · Computed on the held-out test partition for the selected K.
App.py - Reference digit images · Displays dataset examples for comparison.
App.py
Connections 18
- 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