【问题标题】:Can't extract text from an image with python OCR pytesseract无法使用 python OCR pytesseract 从图像中提取文本
【发布时间】:2021-07-07 01:01:28
【问题描述】:

我正在尝试从一些图像中提取文本。它适用于数百个其他图像,但在某些情况下它找不到任何文本。为了优化提取阶段的图像,所有图像都转换为黑白图像。他们所有的背景都是白色的,其他的都是黑色的,例如图标、文本等。

例如,它适用于下面的图像并成功地在图像中找到“睡眠定时器”文本。我不确定它是否相关,但下面带有“睡眠定时器”文本的图片大小为 320 × 351

但是对于下面的图像,它根本找不到任何文本。这张的图片尺寸是 161 × 320。

由于找不到原因,我尝试调整图像大小,但没有成功。

这是我的代码:

from pytesseract import Output
import pytesseract
import cv2

image = cv2.imread('imagePath')

rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pytesseract.image_to_data(rgb, output_type=Output.DICT)

for i in range(0, len(results["text"])):

    text = results["text"][i]
    conf = int(results["conf"][i])

    print("Confidence: {}".format(conf))
    print("Text: {}".format(text))
    print("")

【问题讨论】:

    标签: python opencv ocr python-tesseract text-extraction


    【解决方案1】:

    我测试过它对我有用:

    import pytesseract
    
    print(pytesseract.image_to_string('../images/grmgrm.jfif'))
    results = pytesseract.image_to_data('../images/grmgrm.jfif', output_type=pytesseract.Output.DICT)
    print(results)
    

    您是否遇到错误?向我们展示您遇到的错误。

    【讨论】:

    • 您好,感谢您的回答,我没有收到任何错误。它输出置信度等于-1,并且没有我在问题中添加的屏幕截图中的文本。当我再次尝试使用您的代码时,我也收到了相同的结果:{'level': [1], 'page_num': [1], 'block_num': [0], 'par_num': [0], 'line_num ':[0],'word_num':[0],'left':[0],'top':[0],'width':[161],'height':[320],'conf': ['-1'], '文本': ['']}
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 2020-04-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    相关资源
    最近更新 更多