Introduction to Keras OCR
Keras OCR provides out-of-the-box OCR models and will define an end-to-end pipeline for building new OCR models. The OCR acronym stands for optical character recognition, also known as text recognition. It is a popular technique to read and extract text from image or document file formats. OCR technology is widely used in various applications, such as digitizing printed documents and automating data entry tasks.
Key Takeaways
- The keras ocr system will use a hardware combination, such as an optical scanner and software capable of image processing.
- Optical character recognition is used to identify and digitize the numerical and alphabetical characters presented in the image.
What is Keras OCR?
For text extraction, OCR tools use multiple machine algorithms for pattern recognition to determine the layout and presence of text image files. This tool is trained to recognize characters, shapes, and numbers to recognize image text.
We can reconstruct the text which was extracted in a machine-readable format. By using the same, we can select the extracted text and edit the same in regular text. Using a simpler sense, OCR converts digital data into the image format into editable words by processing documents. This tool is advanced using implementation techniques like intelligent character recognition, which identifies languages.
How to Use Keras OCR?
1. In the first step, we check the Installation of keras by importing the same module using the following import keyword.
Code:
import keras_ocr
Output:
2. After checking the Installation, we import the required packages we need while using keras ocr.
Code:
import keras_ocr
import matplotlib.pyplot as plt
Output:
3. We train the weight detector After importing all the modules in this step.
Code:
keras_pipeline = keras_ocr.pipeline.Pipeline()
Output:
4. After defining the weight detector now, in this step, we are using images for testing the capability.
Code:
img = [
keras_ocr.tools.read (img) for img in ['https://cdn.educba.com/root/cat.JPG',
'https://cdn.educba.com/root/Test.JPG',]
]
Output:
5. After loading the images in this step, we use a pipeline recognizer on the image for making the predictions.
Code:
pred = keras_pipeline.recognize (img)
Output:
6. After running the pipeline recognizer, we are now plotting the model predictions in this step.
Code:
fig, axs = plt.subplots(nrows = len(img), figsize = (10, 20))
for ax, image, predictions in zip (axs, img, pred):
keras_ocr.tools.drawAnnotations(image = image,
predictions = predictions,
ax = ax)
Output:
7. After defining the image in the below steps, we print the text from an image.
Code:
pred_img = pred[1]
for text, box in pred_img:
print (text)
Output:
How to Use Text Extraction in Keras OCR?
Text extraction is possible by importing the OCR module. We can extract the text from an image as shown below; we can see that we are extracting the text from the Test2 image.
Code:
import keras_ocr
import matplotlib.pyplot as plt
ocr_keras = keras_ocr.pipeline.Pipeline()
ocr_img_keras = [
keras_ocr.tools.read(ocr_img_keras) for ocr_img_keras in ['https://cdn.educba.com/root/Test.JPG',
'https://cdn.educba.com/root/Test2.JPG',]
]
ocr_pred = ocr_keras.recognize (ocr_img_keras)
fig, axs = plt.subplots(nrows=len(ocr_img_keras), figsize=(10, 20))
for ax, image, predictions in zip(axs, ocr_img_keras, ocr_pred):
keras_ocr.tools.drawAnnotations(image=image,
predictions=predictions,
ax=ax)
ocr_img = ocr_pred [1]
for text, box in ocr_img:
print(text)
Output:
In the below example, we are using a single image to extract the text from an image. We are using a test image for the same.
Code:
import keras_ocr
import matplotlib.pyplot as plt
ocr_keras = keras_ocr.pipeline.Pipeline()
ocr_img_keras = [
keras_ocr.tools.read(ocr_img_keras) for ocr_img_keras in ['https://cdn.educba.com/root/Test.JPG', 'https://cdn.educba.com/root/Test.JPG',]
]
ocr_pred = ocr_keras.recognize(ocr_img_keras)
fig, axs = plt.subplots(nrows=len(ocr_img_keras), figsize=(10, 20))
for ax, image, predictions in zip(axs, ocr_img_keras, ocr_pred):
keras_ocr.tools.drawAnnotations(image=image,
predictions=predictions,
ax=ax)
ocr_img = ocr_pred [1]
for text, box in ocr_img:
print(text)
Output:
Installing the Keras OCR
We are installing using the pip command. To install it, we must install the same using the pip install command. In the below example, we are installing it on the Ubuntu system.
Code:
python3 -m pip install keras-ocr
Output:
In the above example, we can see that installing it also installs the required packages. To validate the Installation, we need to import the package of ocr. We are importing the ocr module by using the import command as follows.
Code:
import keras_ocr
Output:
Keras OCR Models
Keras ocr defines the ocr models. In the below example, we are defining the pretrained model as follows. We can see that we have imported the required model for the models.
We can see that we have defined the image size in width and height format.
Code:
import os
…..
import sklearn.model_selection
import tensorflow as tf
import keras_ocr
keras_dataset = keras_ocr.datasets.get_icdar_2013_detector_dataset(
cache_dir = '.',
skip_illegible = False
)
train, validation = sklearn.model_selection.train_test_split(
keras_dataset, train_size = 0.7, random_state = 42
)
augm = imgaug.augmenters.Sequential([
imgaug.augmenters.Affine(
scale = (1.0, 1.2),
rotate = (-5, 5)
),
imgaug.augmenters.GaussianBlur(sigma = (0, 0.7)),
imgaug.augmenters.Multiply((0.5, 1.7), per_channel = 0.3)
])
tig = keras_ocr.datasets.get_detector_image_generator(
labels = train,
width = 10,
height = 10
)
vig = keras_ocr.datasets.get_detector_image_generator(
labels = validation,
width = 10,
height = 10
)
img, keras_line, confidence = next(tig)
keras_plt = keras_ocr.tools.drawBoxes(image = img, boxes = keras_line, boxes_format = 'lines')
plt.imshow(keras_plt)
Output:
In the example below, we define the keras pretrained model as follows. We are importing the keras model.
Code:
import matplotlib.pyplot as plt
import keras_ocr
pip_ocr = keras_ocr.pipeline.Pipeline()
img = [
keras_ocr.tools.read(url) for url in [
'Test.JPG']
]
pred = pip_ocr.recognize (img)
fig, axs = plt.subplots(nrows=len(img), figsize=(20, 20))
for ax, image, predictions in zip(axs, img, pred):
keras_ocr.tools.drawAnnotations(image=image, predictions=predictions, ax=ax)
Output:
Keras OCR Program
In the below example, we are importing the OCR and matplotlib library. We are extracting the text from the image using the Test.JPG image.
Code:
import matplotlib.pyplot as plt
import keras_ocr
ocr_keras = keras_ocr.pipeline.Pipeline()
img_ocr = [
keras_ocr.tools.read(img_ocr) for img_ocr in ['Test.JPG', 'Test.JPG',]
]
predict = ocr_keras.recognize(img_ocr)
fig, axs = plt.subplots(nrows=len(img_ocr), figsize=(10, 20))
for ax, image, predictions in zip(axs, img_ocr, predict):
keras_ocr.tools.drawAnnotations(image=image,
predictions=predictions,
ax=ax)
img = predict [1]
for keras_text, box in img:
print(keras_text)
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of Keras ocr?
Answer: It extracts the text from the image we used in the code. We extracted all of the code from the images that we used.
Q2. Which libraries do we need to import while using Keras ocr?
Answer: We need to import the keras_ocr and matplotlib library. We are importing libraries by using the import keywords.
Q3. What is text extraction in Keras ocr?
Answer: Text extraction is simply extracting text from an image using the keras ocr code.
Conclusion
It provides out-of-the-box OCR models and will define an end-to-end pipeline to build new OCR models. OCR converts digital data into image format into editable Word by processing documents.
Recommended Articles
This is a guide to Keras OCR. Here we discuss the introduction, and how to use it, Installation, models, and programs. You may also have a look at the following articles to learn more –