EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras OCR

Keras OCR

Keras OCR

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 for extracting text from a specific image. An OCR program is a program that extracts and repurposes data from scanned documents and camera images.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

Keras OCR - Installation

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:

required packages

3. We train the weight detector After importing all the modules in this step.

Code:

keras_pipeline = keras_ocr.pipeline.Pipeline()

Output:

training the weight detector

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:

Keras OCR - Images

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:

pipeline recognizer

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:

Keras OCR - Plotting

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:

printing the text from image

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:

Keras OCR - Text extraction

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:

Keras OCR - Single Image

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:

PIP command

Ubuntu system

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 12

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:

Image Size

Keras OCR - Image size 2

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 pretrained

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:

Matplotlib

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 –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict
EVIEWS Training
32+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
MYSQL Certification Course
115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
CLOUD COMPUTING Certification Course
141+ Hours of HD Videos
23 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
SPLUNK Training Program
70+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
KERAS Training
 40+ Hour of HD Videos
10 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more