python-imaging-library - 如何增强 OCR 的 Tesseract 自动文本旋转功能?
问题描述
我有一组 PIL 图像,其中一些页面正确旋转,而其他页面旋转接近 180°。这意味着自动方向检测可能会失败,因为不是 178° 度而是识别 2° 度方向。
不幸的是,Tesseract 有时无法理解 2° 方向和 178° 之间的差异,因此在后一种情况下,输出是完全错误的。
一个简单的im.rotate(180)
自动修复此问题,但步骤是手动的,我希望 tesseract 自动理解文本是否颠倒。查看一些方法,他们需要 Hough 变换来理解文档中的普遍方向。但是,在这种情况下,由于这些扫描文档的特殊方向,它们可能会失败。
有哪些自动轮换选项可用,无需依赖第三方脚本,而是保留在 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
在我看来,对齐文本去歪斜python库是最好的。
谢谢你。
推荐阅读
- keras - 使用 Keras 预训练的 InceptionV3/Xception 模型时的错误预测
- vba - 更改 MS Word 中某些文本的字体颜色
- grails - Grails:如何为领域类编写特征?
- html - Css 网格和图像叠加
- java - JavaFX 从另一个控制器调用方法
- linux - 如何在 bash 中插入引号而不在它们的文字文本中进行变量扩展?
- python - 如何更快地将 pandas 2d MultiIndex 重塑为 numpy 3d?
- python - 从子进程 python 多线程调用中以提升的权限运行 bash 脚本
- tensorflow - tf.data.Dataset 的自定义批次
- php - Cron Jobs 未遵循 WHM/Server 时区