Extract text from images using keras-ocr in Python

This post explains how to use keras-ocr for getting text from images. keras-ocr provides out-of-the-box OCR models and an end-to-end training pipeline to build new OCR models. This post demos how to extract text from below two images using keras-ocr.

Install keras-ocr
   
pip install keras-ocr
 
Import keras-ocr and download pretrained weights for the detector and recognizer
  
import keras_ocr 
pipeline = keras_ocr.pipeline.Pipeline()
 
Read images from urls to image object
   
images = [
  keras_ocr.tools.read(url) for url in [
      'https://storage.googleapis.com/gcptutorials.com/examples/keras-ocr-img-1.jpg',        
      'https://storage.googleapis.com/gcptutorials.com/examples/keras-ocr-img-2.png'
  ]
]
 
Check image objects for images
   
print(images[0])
print(images[1])
 
Run the pipeline recognizer on images
   
prediction_groups = pipeline.recognize(images)
 
Extract text from First Image
   
predicted_image_1 = prediction_groups[0]
for text, box in predicted_image_1:
  print(text)    
 
Extract text form Second Image
   
predicted_image_2 = prediction_groups[1]
for text, box in predicted_image_2:
  print(text)
 
Complete code snippet to extract text with keras-ocr in Python
   
import keras_ocr 

pipeline = keras_ocr.pipeline.Pipeline()

images = [
  keras_ocr.tools.read(url) for url in [
      'https://storage.googleapis.com/gcptutorials.com/examples/keras-ocr-img-1.jpg',        
      'https://storage.googleapis.com/gcptutorials.com/examples/keras-ocr-img-2.png'
  ]
]

print(images[0])
print(images[1])

prediction_groups = pipeline.recognize(images)

predicted_image_1 = prediction_groups[0]
for text, box in predicted_image_1:
  print(text)

predicted_image_2 = prediction_groups[1]
for text, box in predicted_image_2:
  print(text)

 

Categories: TensorFlow

Similar Articles