gpt4 book ai didi

python-imaging-library - 如何为 OCR 增强 Tesseract 自动文本旋转功能?

转载 作者:行者123 更新时间:2023-12-04 09:49:00 40 4
gpt4 key购买 nike

我有一组 PIL 图像,其中一些页面正确旋转,而其他页面旋转接近 180°。这意味着自动方向检测可能会失败,因为 178° 度无法识别 2° 度方向。

不幸的是,Tesseract 有时无法理解 2° 方向和 178° 之间的区别,因此在后一种情况下,输出完全错误。

一个简单的im.rotate(180)自动修复这个,但步骤是手动的,我希望tesseract自动理解文本是否颠倒。
查看一些方法,他们需要霍夫变换来理解文档中的普遍方向。但是,在这种情况下,由于这些扫描文档的特殊方向,它们可能会失败。

有哪些自动旋转选项可用,无需依赖第三方脚本,而是留在 Python 库中?

最佳答案

我是 StackOverflow 的新手,所以请原谅我的任何误导或不正确的答案。如果有人仍在寻找答案,pytesseract 的 image_to_osd 函数会提供有关方向的信息。它仅将方向确定为 0°、90°、180° 或 270°,即如果文本与轴对齐,它会准确确定方向。但即使是不同的方向,它也会输出这四个角度中的任何一个。
因此,如果您正在处理 2° 左右的微小角度差异,这应该可以解决问题。所以首先我们对齐文本,然后使用该函数。
这是python中的代码:

while True:
osd_rotated_image = pytesseract.image_to_osd(image)

# using regex we search for the angle(in string format) of the text
angle_rotated_image = re.search('(?<=Rotate: )\d+', osd_rotated_image).group(0)

if (angle_rotated_image == '0'):
image = image
# break the loop once we get the correctly deskewed image
break
elif (angle_rotated_image == '90'):
image = rotate(image,90,(255,255,255)) # rotate(image,angle,background_color)
continue
elif (angle_rotated_image == '180'):
image = rotate(image,180,(255,255,255))
continue
elif (angle_rotated_image == '270'):
image = rotate(image,90,(255,255,255))
continue
并对齐文本 deskew在我看来,python 库是最好的。
谢谢你。

关于python-imaging-library - 如何为 OCR 增强 Tesseract 自动文本旋转功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62045344/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com