9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to create an Optical Character Recognition (OCR) example?


This example will demonstrate how to use Python and Keras to create an Optical Character Recognition (OCR) example.

First, we need to import the necessary libraries and packages. This includes Keras, Numpy, and Matplotlib.

import keras
import numpy as np
import matplotlib.pyplot as plt

Next, we need to create a function to load the MNIST dataset. This dataset consists of handwritten digits and is commonly used for testing OCR applications.

def load_data():
    from keras.datasets import mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    return (x_train, y_train), (x_test, y_test)

Then, we need to create a model for our OCR application. We can use a convolutional neural network (CNN) for this purpose. The CNN will consist of two convolutional layers, a max pooling layer, and a fully connected layer.

def create_model():
    model = keras.Sequential()
    model.add(keras.layers.Conv2D(32, kernel_size=(3, 3),
                                  activation='relu',
                                  input_shape=(28, 28, 1)))
    model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(128, activation='relu'))
    model.add(keras.layers.Dense(10, activation='softmax'))
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adam(),
                  metrics=['accuracy'])
    return model

Finally, we can train our model on the MNIST dataset and use it to make predictions.

# Load the data
(x_train, y_train), (x_test, y_test) = load_data()

# Reshape data
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

# Create model
model = create_model()

# Train model
model.fit(x_train, y_train,
          batch_size=128,
          epochs=10,
          verbose=1,
          validation_data=(x_test, y_test))

# Evaluate model
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Output example

Test loss: 0.0329
Test accuracy: 0.9907

The code consists of the following parts:

  1. Importing the necessary libraries and packages (Keras, Numpy, and Matplotlib).
  2. Creating a function to load the MNIST dataset.
  3. Creating a model for our OCR application (a CNN consisting of two convolutional layers, a max pooling layer, and a fully connected layer).
  4. Training the model on the MNIST dataset and using it to make predictions.

Helpful links

Edit this code on GitHub