【问题标题】:Detect only horizontal text with Tesseract使用 Tesseract 仅检测水平文本
【发布时间】:2020-12-26 14:20:37
【问题描述】:

我有一个带有一些水平和垂直文本的图像。我正在使用 tesseract OCR 检测文本。这是 tesseract 返回的数组

'text': ['', '', '', '', 'Some', 'other', 'text', 'horizontal', '', '', '', 'JEDIY9A', ']xO]', 'WOPUeI', 'BWOS', 'SI', 'SIUL']

如您所见,它只能正确检测水平文本。那么有没有办法强制 tesseract 只检测水平文本呢?所以稍后我会将图像旋转 90 度并再次传递图像以检测垂直文本(现在是水平的)。

或者有没有简单的解决办法?

【问题讨论】:

    标签: python ocr tesseract python-tesseract


    【解决方案1】:

    阅读有关页面分割的信息,您将在那里。 psm 有一个有效值可以完全满足您的要求....

    Page segmentation modes:
      0    Orientation and script detection (OSD) only.
      1    Automatic page segmentation with OSD.
      2    Automatic page segmentation, but no OSD, or OCR.
      3    Fully automatic page segmentation, but no OSD. (Default)
      4    Assume a single column of text of variable sizes.
      5    Assume a single uniform block of vertically aligned text.
      6    Assume a single uniform block of text.
      7    Treat the image as a single text line.
      8    Treat the image as a single word.
      9    Treat the image as a single word in a circle.
     10    Treat the image as a single character.
     11    Sparse text. Find as much text as possible in no particular order.
     12    Sparse text with OSD.
     13    Raw line. Treat the image as a single text line,
                            bypassing hacks that are Tesseract-specific.
    

    试试 --psm 6 或 12

    或者你可以试试这个答案,这是一个适合你的解决方案How do I detect vertical text with OpenCV for extraction

    【讨论】:

    • 如果您仍然遇到一些问题,请告诉我
    • 对于 psm 6 和 12,它仍然返回检测到的文本数组,类似于我发布的问题。
    【解决方案2】:

    pytesseract 不是旋转不变的。因此,您需要进行额外的预处理才能读取垂直文本。

    例如:将文本顺时针旋转 90 度。

    img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
    

    当你阅读时

    print(pytesseract.image_to_string(gry).split("\n")[0])
    

    结果:

    This is some random text vertical
    

    那么如何同时阅读这两个文本呢?

    • 先阅读横排文字

      • import cv2
        import pytesseract
        
        img = cv2.imread("7IIrb.jpg")
        gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        txt = pytesseract.image_to_string(gry).split("\n")[0]
        print(txt)
        
      • 然后将图像顺时针旋转 90 度

        • gry = cv2.rotate(gry, cv2.ROTATE_90_CLOCKWISE)
          txt = pytesseract.image_to_string(gry).split("\n")[0]
          print(txt)
          
      • 结果:

        Some other text horizontal
        This is some random text vertical
        

    代码:


    import cv2
    import pytesseract
    
    img = cv2.imread("7IIrb.jpg")
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    txt = pytesseract.image_to_string(gry).split("\n")[0]
    print(txt)
    gry = cv2.rotate(gry, cv2.ROTATE_90_CLOCKWISE)
    txt = pytesseract.image_to_string(gry).split("\n")[0]
    print(txt)
    

    【讨论】:

    • 但是如果你有几行文字的大图,这个方法会失败。
    • 你试过了吗?还是你在做假设?
    • 把它贴到问题上,让我看看
    • 用这个image试试你的代码
    • 没有解决 ocr 问题的通用模板。此图像与当前图像不同。因此,您必须修改代码并解决此问题。
    【解决方案3】:

    您可以考虑操纵页面分割模式。但是,不能保证它不会检测到文本的其余部分并错误地识别它。因此,我建议您使用 PSM 6 将图像作为块读取,并将输出作为数据帧返回,如下所示:

    import pytesseract
    from pytesseract import Output
    df = pytesseract.image_to_data(image, output_type=Output.DATAFRAME, config='--psm 6')
    

    查看输出时,您会发现文本的置信度得分和行号。您可以使用其中任何一个来设置阈值,然后根据需要从输出中重新创建一个列表,如下所示:

    import pandas as pd
    filter_text = df[df['conf'] == 90]
    text_list = df['text'].tolist()
    

    您还可以使用“左”列考虑文本之间的间距,以提高准确性。数据框总是有用的。你只需要充分利用它们。 :)

    【讨论】:

      猜你喜欢
      • 2023-02-01
      • 2014-04-21
      • 1970-01-01
      • 2014-02-21
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多