OCR 在 Python 中的单列数字?

问题描述 投票:0回答:0

我正在尝试在 Python 中使用 OCR 从 PDF 表格中提取结构化数据。我的代码适用于字符串,而且当字符串与数字混合时似乎也适用。但是当列只包含数字时,结果很差。我要提取的数据的示例图像在这里:Example Image

我已经尝试在 Python 中使用 Google Cloud Vision OCR 代理和 pytesseract。

**这是基本的 GCV 代码(请注意,运行需要 Google Cloud Vision 凭证和 layoutparser 包):

import layoutparser as lp
ocr_agent = lp.GCVAgent.with_credential("my_credential_here",languages = ['en'])
image = cv2.imread('filename',0)
res = ocr_agent.detect(image, return_response=True)
texts  = ocr_agent.gather_text_annotations(res)
layout = ocr_agent.gather_full_text_annotation(res, agg_level=lp.GCVFeatureType.WORD)

输出为:Output

我可以使用其他代码处理行分割(块)——但文本元素显然没有正确进行 OCR。

**我也尝试过使用 Pytesseract:

pytesseract.pytesseract.tesseract_cmd = 'my_path/tesseract.exe'
image = cv2.imread('filename')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
imagedata = pytesseract.image_to_string(cv2.cvtColor(thresh, cv2.COLOR_BGR2RGB), config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')

输出为:Output

我已经为 Pytesseract 尝试了各种 PSM 配置,以及各种预处理步骤,但未能改善结果。

这在 Python 中似乎是一个非常简单的 OCR 任务,但它给我带来了很多麻烦!任何帮助将非常感激。谢谢!

python ocr python-tesseract google-cloud-vision
© www.soinside.com 2019 - 2024. All rights reserved.