我正在使用 Keras OCR 的示例来检测图像中的文本。使用官方文档中提供的示例代码,我使用预训练的权重获得了很好的准确性。 我打算使用 OCR 字符串来比较文本中检测到的一些模式。为了能够创建应用程序,我使用 Flask。
我想按行打印从 OCR 收到的字符串。目前,输出返回带有文本(单个单词)的图像。我希望只能逐行打印从 OCR 接收到的字符串。我怎样才能实现这个目标?
代码:
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)
不知道你解决了没有。但经过几个小时的努力,我想出了以下解决方案。我在搜索同一问题的答案时遇到了您的问题。
for i in prediction_groups:
for y in i:
print(y[0])
您最常使用 DataFrame 对象,因为 Prediction_groups 返回它。检查以下代码:
import pandas as pd
.
.
.
df = pd.DataFrame(prediction_groups[0], columns=['text', 'bbox'])
words = df['text'].tolist()
sentence = ' '.join(words)
sentence 是预测的字符串。