【问题标题】:Unable to read white text on black background using pytesseract无法使用 pytesseract 读取黑色背景上的白色文本
【发布时间】:2020-12-08 15:25:27
【问题描述】:

我正在尝试使用 pytesseract 读取并获取黑色背景上白色文本的位置,但没有任何成功。这是我正在处理的图像示例。

代码如下:

import cv2
import pytesseract
from pytesseract import Output

img = cv2.imread("ocr_example.png")
img = cv2.bitwise_not(img)
_, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)


custom_config = r'--oem 3 --psm 6'
d = pytesseract.image_to_data(binary, output_type=Output.DICT, config=custom_config)

print(d["text"])

这是找到的文本的输出:

['', '', '', '', '家', '地址', '', '使用', '当前', '位置', '', '>', '', '无法'、'到'、'查找'、'位置']

如果我将黑色背景上的白色文本保存到它自己的文件中并扫描,则可以毫无问题地找到文本。但是我需要获取整个图像上文本的位置。

我已尝试在 https://nanonets.com/blog/ocr-with-tesseract/ 等网站上使用许多预处理建议,但似乎没有任何效果。我不介意进行第二次搜索,只找到丢失的文本。

【问题讨论】:

    标签: ocr python-tesseract


    【解决方案1】:
    • 问题是您将页面分割模式设置为从给定图像中识别单个统一的文本块。 (6)

    • 更改为识别可变大小的单列文本。 (4)

    • 来源pytesseract-ocr-multiple-config-options

    • 您只需将--psm 6 更改为--psm 4

    • 现在阅读

    Home Address
    
    Enter address here
    
    Use Current Location
    
    Unable to find location
    
     
    
    DISMISS SAVE
    

    代码:


    import cv2
    import pytesseract
    
    img = cv2.imread("hm-adrs.png")
    img = cv2.bitwise_not(img)
    _, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
    txt = pytesseract.image_to_string(binary, config="--oem 3 --psm 4")
    print(txt)
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 2017-04-23
    • 2011-01-16
    • 2021-12-03
    • 1970-01-01
    • 2023-03-22
    • 2010-11-22
    • 1970-01-01
    相关资源
    最近更新 更多