【问题标题】:Python tesseract cannot read numbers from imagePython tesseract 无法从图像中读取数字
【发布时间】:2021-11-18 10:07:03
【问题描述】:

我有一个适用于某些带有数字的图像的 python 脚本,它可以正确读取它们。 工作的图像类型在这里:Working image 我正在尝试将脚本与仅带有数字的新型图像一起使用,但它不起作用。新的图片类型在这里:Non working image

我的脚本如下:

try:
    from PIL import Image
    from PIL import ImageEnhance
except ImportError:
    import Image
import pytesseract

black = (0,0,0)
white = (255,255,255)
threshold = (160,160,160)

# Open input image in grayscale mode and get its pixels.
img = Image.open("./in/web_search.jpg").convert("LA")

# multiply each pixel by 1.2
out = img.point(lambda i: i * 1.3)

enh = ImageEnhance.Contrast(out)
enh.enhance(1.3).show("30% more contrast")

pixels = out.getdata()

newPixels = []
# Compare each pixel 
for pixel in pixels:
    if pixel < threshold:
        newPixels.append(black)
    else:
        newPixels.append(white)

# Create and save new image.
newImg = Image.new("RGB",out.size)
newImg.putdata(newPixels)
newImg.save("./out/web_search.jpg")
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
print("-----------------------")
print(pytesseract.image_to_string(Image.open('./out/web_search.jpg'), lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist=1234567890 --tessdata-dir="/usr/share/tesseract-ocr/4.00/tessdata/"'))
print("-----------------------")

我的新图像的结果是:

-----------------------
Riemer gaat bee 6 eee
-----------------------

有什么帮助吗? 谢谢。

【问题讨论】:

    标签: python python-tesseract


    【解决方案1】:

    您可能需要做一些工作才能让它接受它。您可以做的一些事情是:

    1. Tesseract 允许您限制可以使用的字符范围。仅将其设置为数字。
    2. 使用某种形式的预处理来消除噪音。无论是 Python Pillow 去噪功能,还是使用形态学开/关。
    3. 在网络上执行微调训练。

    【讨论】:

    • 嗨,对于 1,这不是将字符限制为数字吗? tessedit_char_whitelist=1234567890
    • 哦。奇怪的是这不起作用。尝试在字符周围加上引号?或者尝试更改系统上的 tesseract 配置文件?我不太确定。但显然它被忽略了。
    • 这是语法:print(pytesseract.image_to_string(Image.open('./out/web_search.jpg'), lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist="1234567890" --tessdata-dir="/usr/share/tesseract-ocr/4.00/tessdata/"')) 我在 1234567890 附近添加了引号,但我不确定这是否是您的建议。
    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多