Return string using Keras OCR

Viewed 841

I am using an example of Keras OCR to detect text from image. Using the example code provided in the official documentation I received a good accuracy using the pretrained weights. I intent to use the OCR string for comparing some patterns detected in the text. To be able to create an app, I am using Flask.

I would like to print the string received from the OCR line wise. At the moment the output returns an image with the text (individual words). I want to be only able to print the string received from the OCR line wise. How can I achieve this?

Code:

import matplotlib.pyplot as plt

import keras_ocr

# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()

# Get a set of three example images
images = [
    keras_ocr.tools.read(url) for url in [
        'https://upload.wikimedia.org/wikipedia/commons/b/b4/EUBanana-500x112.jpg',
         'abc.jpg'
    ]
]

# Each list of predictions in prediction_groups is a list of
# (word, box) tuples.
prediction_groups = pipeline.recognize(images)

# Plot the predictions
fig, axs = plt.subplots(nrows=len(images), figsize=(90, 90))
for ax, image, predictions in zip(axs, images, prediction_groups):
    keras_ocr.tools.drawAnnotations(image=image, predictions=predictions, ax=ax)
1 Answers

I dont know you have solved it or not. But after struggling for hours I have come up with following solution. I ran into your question while searching the answer for same issue.

for i in prediction_groups:
    for y in i:
        print(y[0])
Related