A web application that uses a Convolutional Neural Network (CNN) to recognize handwritten digits (0-9) drawn on an HTML canvas.
- Interactive Drawing Canvas - Draw digits with your mouse
- Real-time Predictions - Get instant digit recognition
- High Accuracy - CNN model trained to 99%+ accuracy on MNIST dataset
- Confidence Score - See how confident the model is in its prediction
A deep learning-powered web application that recognizes handwritten digits (0-9) using a Convolutional Neural Network (CNN) trained on the MNIST dataset. Draw a digit on the canvas and watch the AI predict it in real-time!
- Interactive Drawing Canvas: Draw digits directly in your browser with a smooth, responsive canvas
- Real-Time Predictions: Get instant predictions powered by a trained CNN model
- High Accuracy: Model achieves high accuracy on the MNIST test dataset through data augmentation and dropout regularization
- Modern UI: Clean, responsive interface built with Bootstrap 5
- Easy Deployment: Simple Flask-based architecture for easy hosting
-
Backend:
- Python 3.8+
- Flask 3.1.2 - Web framework
- TensorFlow/Keras 2.20.0 - Deep learning framework
- NumPy 2.3.2 - Numerical computing
- Pillow 11.3.0 - Image processing
-
Frontend:
- HTML5 Canvas for drawing
- Bootstrap 5 for responsive design
- Vanilla JavaScript for interactivity
-
Machine Learning:
- Convolutional Neural Network (CNN)
- Data augmentation for improved generalization
- Dropout layers for regularization
MNIST_Digit_Recognition/
βββ app.py # Flask application with prediction endpoint
βββ train_model.py # CNN model training script
βββ mnist_model.keras # Trained Keras model
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration for deployment
βββ templates/
β βββ index.html # Frontend HTML
βββ static/
βββ js/
βββ main.js # Canvas drawing and API logic
- Python 3.10+
- pip
-
Clone the repository
git clone https://github.com/YOUR_USERNAME/MNIST_Digit_Recognition.git cd MNIST_Digit_Recognition -
Create virtual environment
python -m venv venv # Windows .\venv\Scripts\activate # Linux/Mac
β
βββ app.py # Flask web application
βββ train_model.py # Model training script
βββ mnist_model.keras # Pre-trained model file
βββ requirements.txt # Python dependencies
β
βββ templates/
β βββ index.html # Main web interface
β
βββ static/
β βββ js/
β βββ main.js # Canvas drawing and prediction logic
β
βββ README.md # This file
- Python 3.8 or higher
- pip package manager
-
Clone the repository
git clone https://github.com/0xASR-dev/MNIST_Digit_Recognition.git cd MNIST_Digit_Recognition -
Create a virtual environment (recommended)
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application =======
-
Start the Flask server
python app.py
-
Open in browser =======
-
Open your browser and navigate to:
http://127.0.0.1:5000
The CNN model consists of:
| Layer | Description |
|---|---|
| Conv2D (32 filters) | 3x3 kernel, ReLU activation |
| MaxPooling2D | 2x2 pool size |
| Dropout (0.25) | Regularization |
| Conv2D (64 filters) | 3x3 kernel, ReLU activation |
| MaxPooling2D | 2x2 pool size |
| Dropout (0.25) | Regularization |
| Flatten | Converts to 1D |
| Dense (128) | Fully connected, ReLU |
| Dropout (0.5) | Regularization |
| Dense (10) | Output layer, Softmax |
-
Draw a digit (0-9) on the canvas using your mouse
-
Click "Predict" to see the AI's prediction
-
Click "Clear" to reset the canvas and draw again
If you want to retrain the model with different parameters:
python train_model.pyThis will:
- Download the MNIST dataset
- Train for 15 epochs with data augmentation
- Save the model as
mnist_model.keras
docker build -t mnist-recognition .
docker run -p 7860:7860 mnist-recognition- Create a new Space at huggingface.co/spaces
- Select Docker as the SDK
- Upload these files:
Dockerfileapp.pyrequirements.txtmnist_model.kerastemplates/index.htmlstatic/js/main.js
Request:
{
"image": "data:image/png;base64,..."
}Response:
{
"digit": 7,
"confidence": 98.5
}- Backend: Flask, TensorFlow/Keras
- Frontend: HTML5 Canvas, JavaScript, Bootstrap 5
- Deployment: Docker, Gunicorn
MIT License
- Download the MNIST dataset (if not cached)
- Train a CNN model with data augmentation
- Save the trained model as
mnist_model.keras
Training parameters:
- Epochs: 15
- Batch size: 128
- Optimizer: Adam
- Data augmentation: rotation, zoom, width/height shifts
The CNN model consists of the following layers:
Conv2D(32, 3x3, relu) β MaxPooling(2x2) β Dropout(0.25)
β
Conv2D(64, 3x3, relu) β MaxPooling(2x2) β Dropout(0.25)
β
Flatten() β Dense(128, relu) β Dropout(0.5)
β
Dense(10, softmax) [Output Layer]
Key Features:
- Two convolutional layers for feature extraction
- MaxPooling layers for dimensionality reduction
- Dropout layers (0.25 and 0.5) to prevent overfitting
- Data augmentation during training (rotation, zoom, shifts)
- Softmax activation for multi-class classification (10 digits)
The model achieves high accuracy on the MNIST test dataset through:
- Convolutional layers for spatial feature learning
- Data augmentation for better generalization
- Dropout regularization to prevent overfitting
- 15 epochs of training with the Adam optimizer
- User draws a digit on the HTML5 canvas
- Canvas converts the drawing to a PNG image (280x280 pixels)
- Frontend sends the base64-encoded image to the Flask backend
- Backend preprocesses the image:
- Decodes base64 data
- Converts to grayscale
- Resizes to 28x28 pixels
- Inverts colors (white background β black, black digit β white)
- Normalizes pixel values to [0, 1]
- Model predicts the digit using the trained CNN
- Result is sent back to the frontend and displayed
Base64 Image β PIL Image β Grayscale β Resize(28x28)
β Invert Colors β Normalize β Reshape(1, 28, 28, 1) β PredictThe application features a clean, intuitive interface where users can:
- Draw digits using their mouse or touchpad
- See predictions instantly
- Clear the canvas to try again
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement) - Make your changes
- Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
- Add touch support for mobile devices
- Implement confidence scores for predictions
- Add visualization of the model's internal layers
- Support for custom model training parameters via UI
- Add ability to save and share drawings
- Implement batch prediction for multiple digits
This project is licensed under the MIT License - see the LICENSE file for details.
- MNIST Dataset: Created by Yann LeCun and Corinna Cortes
- TensorFlow/Keras: For providing excellent deep learning tools
- Flask: For the lightweight and flexible web framework
- Bootstrap: For the responsive UI components
For questions or suggestions, please open an issue on the GitHub repository.