【发布时间】:2020-04-22 05:17:29
【问题描述】:
我在 Tesseract OCR 上苦苦挣扎。 我有一个血液检查图像,它有一个带有压痕的表格。尽管 tesseract 可以很好地识别字符,但其结构并未保留在最终输出中。例如,查看缩进的“Emocromo con formula”(英文翻译:blood count with formula)下面的行。我想保留那个缩进。
我阅读了其他相关讨论,并找到了preserve_interword_spaces=1 选项。结果稍微好一点,但正如你所见,它并不完美。
有什么建议吗?
更新:
我尝试了 Tesseract v5.0,结果是一样的。
代码:
Tesseract 版本为 4.0.0.20190314
from PIL import Image
import pytesseract
# Preserve interword spaces is set to 1, oem = 1 is LSTM,
# PSM = 1 is Automatic page segmentation with OSD - Orientation and script detection
custom_config = r'-c preserve_interword_spaces=1 --oem 1 --psm 1 -l eng+ita'
# default_config = r'-c -l eng+ita'
extracted_text = pytesseract.image_to_string(Image.open('referto-1.jpg'), config=custom_config)
print(extracted_text)
# saving to a txt file
with open("referto.txt", "w") as text_file:
text_file.write(extracted_text)
比较结果:
GITHUB:
如果您想自己尝试一下,我已经创建了一个GitHub 存储库。
感谢您的帮助和时间
【问题讨论】:
-
“使用 Tesseract 保留原始文本缩进/结构”:
tesseract无法保留原始结构 . Edit你的问题并解释你想用 ocred 数据做什么? -
@stovfl 保存与原始文件结构相同的 txt 或 pdf。例如,查看缩进的“Emocromo con formula”(英文翻译:blood count with formula)下面的行。我想保留那个缩进。
-
“保存具有相同结构的 txt 或 pdf”:我假设您想要 开箱即用 解决方案?通常,您需要每个字符或字符组、图形和线/网格元素的
coords。将Creating Snapshots 的输出添加到您的 GitHub 复制中。 -
@stovfl “我想你想要一个开箱即用的解决方案?”最好,如果有的话。保存到 Pdf 很简单,我做到了,相反,保存到具有相同缩进的 txt 文件并不像我想象的那么容易。
-
“如果有的话最好”:我不知道。 "to Pdf ...我做到了":你如何获得缩进/制表符值? "to a txt file":视情况而定,纯文本只能使用
\t和<space>。 Textviewer 决定选项卡是否扩展为2, 4 or 8 <spaces。仅使用Monospaced字体不会扭曲表格。意味着在一个 Textviewer 中查看 Table 显示正常,而在另一个则不会。
标签: python computer-vision ocr tesseract python-tesseract