0
0
Computer Visionml~5 mins

Handwriting recognition basics in Computer Vision

Choose your learning style9 modes available
Introduction

Handwriting recognition helps computers read and understand written text. It turns pictures of handwriting into digital letters.

Reading handwritten notes to make them searchable on a computer
Automatically sorting mail by reading addresses on envelopes
Helping people with disabilities write using a pen or stylus
Digitizing old handwritten documents for easy access
Recognizing numbers and letters on forms or checks
Syntax
Computer Vision
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

This example shows a simple neural network for recognizing handwritten digits.

Input images are 28x28 pixels, flattened into a list of numbers.

Examples
A smaller model with 64 neurons in the hidden layer for faster training.
Computer Vision
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense

model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])
Using SGD optimizer instead of Adam for training the model.
Computer Vision
model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Sample Model

This program trains a simple neural network to recognize handwritten digits from the MNIST dataset. It shows training progress, test accuracy, and predictions for 5 images.

Computer Vision
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense

# Load MNIST dataset of handwritten digits
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize pixel values to 0-1
x_train, x_test = x_train / 255.0, x_test / 255.0

# Build a simple neural network model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model for 3 epochs
model.fit(x_train, y_train, epochs=3, verbose=2)

# Evaluate the model on test data
loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f'Test accuracy: {accuracy:.4f}')

# Predict the first 5 test images
predictions = model.predict(x_test[:5])
predicted_labels = predictions.argmax(axis=1)
print('Predicted labels:', predicted_labels)
print('True labels:     ', y_test[:5])
OutputSuccess
Important Notes

Handwriting recognition models often start simple and get more complex for better accuracy.

Normalizing pixel values helps the model learn faster and better.

Using a dataset like MNIST is a great way to practice handwriting recognition.

Summary

Handwriting recognition turns images of writing into text.

Simple neural networks can learn to recognize digits from images.

Training on labeled data like MNIST helps the model improve accuracy.