Python Keras Handwriting Recognition

Handwriting recognition is one of those projects that truly feels like magic when you first get it right. I remember the first time I built a model that could actually read my scribbled notes; it changed how I viewed automation.

In this guide, I will show you how to build a robust handwriting recognition system using Python and Keras. We will focus on practical steps that you can use for real-world applications like digitizing handwritten zip codes on mail or processing forms.

Method 1: Build a Convolutional Neural Network (CNN) with Python Keras

I have found that CNNs are the gold standard for image-related tasks because they capture spatial hierarchies in data. For this method, we will use the classic MNIST dataset, which contains thousands of handwritten digits.

We will reshape the data to include a grayscale channel and then normalize it to improve training speed. This ensures the model focuses on the patterns of the strokes rather than the pixel intensity.

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist

# Load the dataset of handwritten digits
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Reshape data to fit the CNN (28x28 pixels, 1 channel)
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

# Build the Python Keras CNN model
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

# Compile and train
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)

# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Model Accuracy: {test_acc*100:.2f}%")

I executed the above example code and added the screenshot below.

Keras Handwriting Recognition

Method 2: Use Data Augmentation in Python Keras to Improve Accuracy

In my experience, real-world handwriting is never as clean as a pre-made dataset. People write at different angles and with varying thicknesses, which can confuse a basic model.

I use the ImageDataGenerator class to artificially expand my training set by rotating and zooming into images. This makes the Python Keras model much more resilient to messy handwriting found on actual paper.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Initialize the Data Generator for Python Keras
datagen = ImageDataGenerator(
    rotation_range=10,
    zoom_range=0.1,
    width_shift_range=0.1,
    height_shift_range=0.1
)

# Fit the generator to our training data
datagen.fit(train_images)

# Train the model using the augmented data
model.fit(datagen.flow(train_images, train_labels, batch_size=32),
          steps_per_epoch=len(train_images) / 32, epochs=5)

print("Augmented training complete.")

I executed the above example code and added the screenshot below.

Python Keras Handwriting Recognition

Method 3: Save and Load the Python Keras Model for Local Apps

Once I have trained a model to a high degree of accuracy, I don’t want to lose that progress. Saving the model allows you to deploy it into a desktop application or a web service later.

This code snippet shows you how to export your trained architecture and weights into a single file. You can then reload it in any Python environment without needing to retrain from scratch.

import os

# Save the trained Python Keras model
model_path = 'handwriting_model.h5'
model.save(model_path)
print(f"Model saved to {model_path}")

# Load the model back for future predictions
reloaded_model = tf.keras.models.load_model(model_path)

# Verify the loaded model
predictions = reloaded_model.predict(test_images[:5])
print("Predicted digits:", np.argmax(predictions, axis=1))

I executed the above example code and added the screenshot below.

Handwriting Recognition Keras

Method 4: Predict Custom Handwritten Images via Python Keras

The ultimate goal of this tutorial is to recognize your own handwriting. I usually take a photo of a number, crop it, and convert it to grayscale before feeding it into the model.

This method uses OpenCV to resize your custom image to the required 28×28 format. It is a vital step because the Python Keras model expects inputs to match the exact dimensions used during training.

import cv2
import matplotlib.pyplot as plt

def predict_custom_image(image_path):
    # Read the image in grayscale
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    
    # Resize to 28x28 and invert colors (if paper is white and ink is dark)
    img_resized = cv2.resize(img, (28, 28))
    img_final = cv2.bitwise_not(img_resized)
    img_final = img_final.reshape(1, 28, 28, 1).astype('float32') / 255

    # Predict using the Python Keras model
    prediction = model.predict(img_final)
    return np.argmax(prediction)

# Example usage (Ensure you have a 'digit.png' in your folder)
# result = predict_custom_image('digit.png')
# print(f"The model recognizes this digit as: {result}")

Method 5: Implement Early Stopping in Python Keras to Prevent Overfitting

I often see beginners let their models train for too many epochs, which leads to overfitting. This means the model memorizes the training data but fails on new, unseen handwriting.

I use the EarlyStopping callback to automatically halt the training process once the validation loss stops improving. This saves time and ensures the Python Keras model remains generalized for real-world use.

from tensorflow.keras.callbacks import EarlyStopping

# Define the callback
early_stop = EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)

# Train the Python Keras model with Early Stopping
model.fit(
    train_images, train_labels, 
    epochs=20, 
    validation_split=0.2, 
    callbacks=[early_stop]
)

print("Training stopped early to maintain model quality.")

Handwriting recognition is a powerful tool for automating data entry and digitizing physical records. Whether you are processing bank checks or sorting mail, these Python Keras methods provide a solid foundation.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.