Skip to content

πŸš€AI-powered app for handwritten digit recognition with an interactive GUI built 🎨 using CNN and CustomTkinter.

Notifications You must be signed in to change notification settings

asRot0/HandDrawnDigitAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎨 HandDrawnDigitAI

Welcome to HandDrawnDigitAI πŸ€– – an intuitive app for recognizing hand-drawn digits! Powered by CustomTkinter, a pre-trained Convolutional Neural Network (CNN), and a user-friendly GUI, this project brings machine learning right to your fingertips. Draw digits, recognize them instantly, and enjoy the seamless experience!


πŸ—‚οΈ Project Structure

HandDrawnDigitAI/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py              # Makes `app` a package
β”‚   β”œβ”€β”€ gui.py                   # Contains the GUI logic
β”‚   β”œβ”€β”€ digit_recognizer.py      # Logic for recognizing digits
β”‚   β”œβ”€β”€ themes.py                # Themes dictionary
β”‚   └── utils.py                 # Helper utility functions
β”‚
β”œβ”€β”€ models/
β”‚   └── cnn_model.h5             # Pre-trained model
β”‚
β”œβ”€β”€ logs/                        # Stores temporary image files
β”‚
β”œβ”€β”€ notebooks/
β”‚   └── cnn_mnist.ipynb          # Notebook for model
β”‚
β”œβ”€β”€ run.py                       # Main entry point to the app
β”œβ”€β”€ requirements.txt             # Dependencies for the project
└── README.md                    # Documentation

πŸš€ Features

  • πŸ–ΌοΈ Interactive Drawing Canvas
    Use the mouse to draw digits directly on the canvas.

  • 🧠 AI-Powered Digit Recognition
    Recognize digits (0-9) using a pre-trained CNN model trained on the MNIST dataset.

  • 🎨 Dynamic Themes
    Choose from multiple visually appealing themes:
    πŸ”΅ Oceanic | πŸŒ™ Dark Mode | 🎨 Vibrant | πŸ–€ Corporate | 🌸 Pink Black

  • πŸ› οΈ Modular Codebase
    Clean and organized project structure for easy navigation.


🎯 How It Works

  1. Draw a digit on the canvas.
  2. Press the "Predict" button, and the model will recognize the digit.
  3. If the prediction isn't clear, adjust and re-draw on the canvas!
  4. Use the "Clear Canvas" button to start over.

πŸ“‹ Requirements

Make sure you have the following dependencies installed before running the app:

tensorflow==2.10.0
customtkinter==5.1.2
pillow==9.2.0
numpy==1.23.0
opencv-python==4.6.0

Install them using:

pip install -r requirements.txt

πŸ“‚ Files & Directories

πŸ“ app/

  • gui.py
    Handles the graphical user interface logic, including button interactions and canvas drawing.

  • digit_recognizer.py
    Preprocesses the canvas image and uses the CNN model for predictions.

  • themes.py
    Stores theme configurations for a visually appealing UI.

  • utils.py
    Helper functions to support digit recognition and GUI.

πŸ“ models/

  • cnn_model.h5
    Pre-trained CNN model saved in Keras HDF5 format.

πŸ“ logs/

  • Temporary directory to store canvas snapshots during runtime.

πŸ“ notebooks/

  • cnn_mnist.ipynb
    A Jupyter Notebook used to train the CNN model on the MNIST dataset.

run.py

  • The main entry point to launch the application.

πŸ–₯️ Running the Application

Follow these steps to get started:

  1. Clone the repository:
git clone https://github.com/asRot0/HandDrawnDigitAI.git
cd HandDrawnDigitAI
  1. Install the required dependencies:
pip install -r requirements.txt
  1. Run the app:
python run.py
  1. Draw digits and enjoy the magic! ✨

🎨 Themes Preview

πŸŒ™ Dark Mode πŸ”΅ Oceanic 🌸 Pink Black
Dark Mode Oceanic Pink Black

🌸 BlackPink Theme

themes


πŸ”’ Key Math Concepts for CNN & MNIST

Here are some important mathematical formulas used in this project

1️⃣ Convolution Operation (Feature Extraction in CNN)

Convolutional layers apply filters to extract features from input images.

$$ \LARGE O(i, j) = \sum_m \sum_n I(i+m, j+n) \cdot K(m, n) $$

  • $O(i, j)$ β†’ Output feature map at position $(i, j)$.
  • $I(i+m, j+n)$ β†’ Input image pixels affected by the filter.
  • $K(m, n)$ β†’ Kernel (filter) values applied to the input.

πŸ“Œ Why Add?

  • Represents how a filter (kernel) slides over an image to extract meaningful features.
  • Core operation in Convolutional Neural Networks (CNNs).

2️⃣ ReLU Activation Function (Hidden Layers)

ReLU introduces non-linearity to the model by keeping only positive values.

$$ \LARGE \text{ReLU}(x) = \max(0, x) $$

  • $x$ β†’ Input value to the activation function.

πŸ“Œ Why Add?

  • Helps prevent vanishing gradients.
  • Improves CNN’s ability to learn complex patterns.

3️⃣ Max Pooling (Dimensionality Reduction)

Max pooling reduces the spatial size of feature maps while preserving key information.

$$ \LARGE P(i, j) = \max_{(m,n) \in R} F(i+m, j+n) $$

  • $P(i, j)$ β†’ Pooled output value at position $(i, j)$.
  • $F(i+m, j+n)$ β†’ Input feature map values in the pooling region.
  • $R$ β†’ Pooling region (e.g., 2Γ—2 or 3Γ—3 window).

πŸ“Œ Why Add?

  • Reduces computation and prevents overfitting.
  • Keeps dominant features while discarding unnecessary details.

4️⃣ Softmax Function (Final Layer for Classification)

The softmax function converts model outputs into probability distributions.

$$ \LARGE \text{Softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{n} e^{z_j}} $$

  • $z_i$ β†’ Raw score (logit) for class $( i )$.
  • $e^{z_i}$ β†’ Exponential of the logit, ensuring positive values.
  • $\sum_{j=1}^{n} e^{z_j}$ β†’ Sum of exponentials across all $( n )$ classes (normalization factor).

πŸ“Œ Why Add?

  • Softmax assigns probabilities to digit classes (0-9).
  • Ensures outputs sum up to 1, making it interpretable.

5️⃣ Cross-Entropy Loss (Training the CNN)

Cross-entropy measures the difference between predicted and actual labels.

$$ \LARGE \mathcal{L} = -\sum_{i=1}^{n} y_i \log(\hat{y_i}) $$

  • $\mathcal{L}$ β†’ Cross-entropy loss value.
  • $y_i$ β†’ Actual label (ground truth) for class $( i )$ (1 for correct class, 0 otherwise).
  • $\hat{y_i}$ β†’ Predicted probability from the softmax function.

πŸ“Œ Why Add?

  • Penalizes incorrect predictions by increasing the loss.
  • Common loss function for multi-class classification.

6️⃣ Adam Optimizer (Gradient Descent for CNN Training)

Adam adjusts learning rates based on gradients to optimize CNN performance.

$$ \LARGE \theta_{t+1} = \theta_t - \frac{\eta}{\sqrt{v_t} + \epsilon} m_t $$

  • $\theta_t$ β†’ Model parameters at step $( t )$.
  • $m_t$ β†’ First moment estimate (mean of gradients).
  • $v_t$ β†’ Second moment estimate (variance of gradients).
  • $\eta$ β†’ Learning rate (step size).
  • $\epsilon$ β†’ Small constant to avoid division by zero.

πŸ“Œ Why Add?

  • Used as the optimizer in this project (optimizer='adam').
  • Combines momentum & adaptive learning rates for faster convergence.

These mathematical concepts power the CNN architecture used in this project. They help in feature extraction, classification, optimization, and learning to recognize hand-drawn digits efficiently and accurately.


πŸ“– Model Training (Optional)

If you'd like to retrain the model:

  1. Open the Jupyter Notebook in the notebooks/ directory.
  2. Train the CNN on the MNIST dataset.
  3. Save the updated model as cnn_model.h5 in the models/ directory.

πŸ’‘ Future Enhancements

  • ✍️ Add support for multiple languages.
  • πŸ“Š Integrate visualization of model predictions (e.g., activation maps).
  • πŸ”„ Real-time digit recognition with camera input.

🌟 Contributors

  • Asif Ahmed – GitHub
    Creator and Maintainer

Want to contribute? Feel free to fork the repository and open a pull request!

🀝 Support

If you find this project helpful, consider giving it a ⭐ on GitHub and sharing it with others!

About

πŸš€AI-powered app for handwritten digit recognition with an interactive GUI built 🎨 using CNN and CustomTkinter.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages