【问题标题】:Pytesseract consistently reading a particular number incorrectlyPytesseract 始终错误地读取特定数字
【发布时间】:2020-12-07 01:16:56
【问题描述】:

我有这张图片

我正在阅读框中的数字,它显然应该是数字1;但是,后面的代码始终将其读取为数字4。似乎很奇怪,它会出错,因为我正在使用的 pdf 表单中有许多类似的实例,它确实正确。我能做些什么来纠正这种特殊情况而不妨碍正确执行的操作吗?

图像只是可填写的 pdf 表单的一部分。我阅读了 pdf 并将其保存为图像。然后 tesseract 将图像读入文本文件。

这是文本转换为jpg 文件后的样子

image_counter=1
pages = convert_from_path(filename,  poppler_path= dir_path+r'\poppler-0.68.0\bin') 
for page in pages:
    filename = "page_"+str(image_counter)+".jpg"                  
    # Save the image of the page in system 
    page.save(filename, 'JPEG') 
          
    # Increment the counter to update filename 
    image_counter+=1

# Variable to get count of total number of pages 
filelimit = image_counter-1

# Iterate from 1 to total number of pages 
for i in range(1, filelimit + 1): 
    filename = "page_"+str(i)+".jpg"
    # Recognize the text as string in image using pytesserct 
    text = str(pytesseract.image_to_string(Image.open(filename), config='--psm 6 --dpi 300 --oem 3'))
    f.write(text) # f is the file I am writing to 

【问题讨论】:

    标签: python-3.x pdf ocr tesseract


    【解决方案1】:

    带有白名单数字的 Psm 值 11 对我有用:

        >>> image = cv2.imread("1.png",0)
        >>> thresh = cv2.threshold(image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 3 digits')
        >>> data
        ''
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 5 digits')
        >>> data
        '-\n .'
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 6 digits')
        >>> data
        '11'
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 7 digits')
        >>> data
        '11'
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 11 digits')
        >>> data
        '.\n\n1' #whitelist here
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 12 digits')
        >>> data
        '14'
    

    正确的输出(带有白名单数字):

        >>> data = pytesseract.image_to_string(thresh,config= '--psm 11 -c tessedit_char_whitelist=1234567890')
        >>> data
        '1'
        >>> data = pytesseract.image_to_string(thresh,config= '--psm 6 -c tessedit_char_whitelist=1234567890')
        >>> data
        '11'
        >>> data = pytesseract.image_to_string(image,config= '--psm 11 -c tessedit_char_whitelist=1234567890')
        >>> data
        '1'
    

    并且不要忘记在通过之前对图像进行二值化以获得更好的结果。

    【讨论】:

    • 白名单似乎没有帮助。我认为这是因为我只提供了一个更大图像的片段。 --psm11 似乎确实有所帮助,但在文档的其他地方,一个数字以类似于第一个的方式被误读。对图像进行二值化似乎会使事情变得更糟。
    • 哦,我明白了。我猜你有包含大量文本的图像,在这种情况下,白名单没有帮助,抱歉。对于如此大的文本,Tesseract 无法给出 100% 准确的结果。即使您已经做了很多预处理,在某些或其他地方它读错了。二值化取决于图像(不仅仅是 otsu 的),有助于提高准确性。
    • 感谢您与我们联系。谢谢。这些框来自一个 pdf 可填写的表格。我了解到,将这些字体改成较小的字体似乎会有所帮助。
    猜你喜欢
    • 2020-11-07
    • 2023-01-31
    • 2020-10-28
    • 2021-04-14
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    相关资源
    最近更新 更多