A complete, beginner-friendly Convolutional Neural Network (CNN) project built in PyTorch. The system supports two modes of operation:
- Digit Recognition Mode (10 Classes): Trained on the classic MNIST dataset to classify digits
0-9. - General OCR Mode (85 Classes): Trained on a dynamically synthesized, font-distorted dataset to classify:
- Digits
0-9(10 classes) - Uppercase letters
A-Z(26 classes) - Lowercase letters
a-z(26 classes) - Special characters
@,#,%,&,+,-,*,/,=,?,!,$,(,),[,],{,},<,>,:,;,,,.(23 classes)
- Digits
- Double-Model Capability: Supports both
digit_model.pthandocr_model.pthwith automatic model selection based on weight file availability. - On-the-Fly Font Dataset Synthesizer: Uses standard system fonts with random rotation, translation, scaling, and noise to generate robust OCR training datasets (
ocr_dataset.npz) instantly. - 10x Vectorized Training Performance: Dataset pre-processed directly to normalized PyTorch tensors, reducing training times to under 40 seconds per epoch on CPU.
- Robust Inference Processing:
- Median-Based Background Subtraction: Wipes out solid backgrounds of any color (e.g. Red-on-Green) automatically.
- Dynamic Binarization & Centering: Automatically crops the character bounding box, resizes it to
20x20preserving aspect ratio, and centers it in a standard28x28black grid.
- Beautiful Terminal Dashboard: Integrated with
tqdmto show progress bars, loss, and accuracy in real-time during training and validation.
nural-network/
│
├── model.py # Parameterized CNN architecture
├── train.py # OCR & Digit training logic with progress bar
├── predict.py # Advanced preprocessing and inference script
├── dataset_generator.py # Synthesizes 25,500 character images using system fonts
├── requirements.txt # Project dependencies (includes tqdm)
├── .vscode/
│ └── settings.json # Editor configurations for local virtual environment
└── README.md # User documentation
python -m venv .venv
.venv\Scripts\activatepython3 -m venv .venv
source .venv/bin/activatepip install -r requirements.txtGenerate 25,500 distorted character images from system fonts:
python dataset_generator.pyOutputs: ocr_dataset.npz
Train the 85-class OCR CNN model:
python train.pyOutputs: ocr_model.pth
Test on any custom image:
python predict.py path/to/your/image.pngTo run the classic 10-digit classifier:
- Ensure
ocr_model.pthis deleted or renamed, anddigit_model.pthis active. - Run
train.py(which will download the standard MNIST dataset if not present):python train.py
- Test your custom digits:
python predict.py test_digit_7.png
Note: predict.py automatically detects which model is present in the workspace (ocr_model.pth or digit_model.pth) and adjusts classes mapping accordingly!
You can run a local web server to draw characters with your mouse/touch, or capture photos from your phone camera and upload images from your gallery.
python web_app.py- Binds to port
5000by default. - Open your browser at:
- On the same device:
http://localhost:5000 - From another device (e.g. phone) on the same Wi-Fi:
http://<YOUR_PC_IP>:5000
- On the same device:
- Draws are dynamically preprocessed and fed into the AI model, displaying results both on the web screen and logging full details to the Termux/PC console.
If you want to train the model on your own handwriting styles:
- Create a
custom_data/folder (created automatically on first run ofadd_custom_data.py). - Inside
custom_data/, create subfolders named after the target characters. To prevent case-sensitivity and OS issues (especially on Windows & Android), use the following naming mapping:- Digits
0-9->0,1, ...,9 - Uppercase
A-Z->A_upper,B_upper, ...,Z_upper - Lowercase
a-z->a_lower,b_lower, ...,z_lower - Special Characters ->
at(@),hash(#),percent(%),amp(&),plus(+),minus(-),asterisk(*),slash(/),equal(=),question(?),excl(!),paren_open((),paren_close()), etc.
- Digits
- Save your handwritten drawings/images (e.g.
.pngor.jpg) in their corresponding character subfolders. - Run the data loader script to preprocess and append this data to the dataset:
python add_custom_data.py
- Force-retrain the model on the updated dataset:
python train.py --force --epochs 6
To clean up debug PNG images that accumulate in the project root:
python clean_project.pySince PyTorch can be tricky to compile on native Android, the recommended and most robust way is to run a PRoot Ubuntu environment inside Termux.
Install Termux via F-Droid (do not use the obsolete Google Play version).
Open Termux and run:
termux-setup-storageThis mounts your phone's internal storage inside Termux at ~/storage/shared/.
Run the following commands in Termux:
pkg update && pkg upgrade -y
pkg install proot-distro -y
proot-distro install ubuntu
proot-distro login ubuntuInside the logged-in Ubuntu session, run:
apt update && apt upgrade -y
apt install -y python3 python3-pip python3-venv gitClone the repository or copy files into your Ubuntu folder, activate virtual environment, and install dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip install torch torchvision numpy pillow tqdm flaskRun the web application to draw or capture photos:
python web_app.pyOpen your mobile browser at http://localhost:5000 to start drawing and predicting!