How to enhance Tesseract automatic text rotation capabilities for OCR?

lin*_*llo 6 ocr tesseract python-imaging-library python-tesseract

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

\n\n

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

\n\n

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

\n\n

有哪些自动轮换选项可用,无需依赖第三方脚本,但仍保留在 Python 库中?

\n

小智 5

我是 StackOverflow 的新手,因此请原谅我的任何误导或错误答案。如果有人仍在寻找答案,请使用 pytesseract 的image_to_osd函数会提供有关方向的信息。它仅将方向确定为 0\xc2\xb0,90\xc2\xb0,180\xc2\xb0 或 270\xc2\xb0,即如果文本沿轴对齐,它会准确确定方向。但即使方向不同,它也可以输出这四个角度中的任何一个。

\n

因此,如果您正在处理像 2\xc2\xb0 左右这样的微小角度差异,这应该可以解决问题。所以首先我们对齐文本,然后使用该函数。

\n

这是Python中的代码:

\n
while True:\n    osd_rotated_image = pytesseract.image_to_osd(image)\n\n    # using regex we search for the angle(in string format) of the text\n    angle_rotated_image = re.search(\'(?<=Rotate: )\\d+\', osd_rotated_image).group(0)\n\n    if (angle_rotated_image == \'0\'):\n        image = image\n        # break the loop once we get the correctly deskewed image\n        break\n    elif (angle_rotated_image == \'90\'):\n        image = rotate(image,90,(255,255,255)) # rotate(image,angle,background_color)\n        continue\n    elif (angle_rotated_image == \'180\'):\n        image = rotate(image,180,(255,255,255))\n        continue\n    elif (angle_rotated_image == \'270\'):\n        image = rotate(image,90,(255,255,255))\n        continue    \n
Run Code Online (Sandbox Code Playgroud)\n

并对齐文本倾斜校正python 库是最好的。

\n

谢谢。

\n