【问题标题】:How to enhance Tesseract automatic text rotation capabilities for OCR?如何增强 OCR 的 Tesseract 自动文本旋转功能?
【发布时间】:2020-05-27 14:17:42
【问题描述】:

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

很遗憾,Tesseract 有时无法理解 2° 方向和 178° 之间的差异,因此在后一种情况下,输出完全错误。

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

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

【问题讨论】:

    标签: python-imaging-library ocr tesseract python-tesseract


    【解决方案1】:

    我是 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 库是最好的。

    谢谢。

    【讨论】:

    • 您为此代码导入了哪个旋转函数?我尝试从 skimage.transform import rotate 并在使用您的代码时出现此错误: TypeError: Cannot handle this data type: (1, 1, 3),
    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多