簡體   English   中英

Python tesseract 無法從圖像中讀取數字

[英]Python tesseract cannot read numbers from image

我有一個 python 腳本適用於一些帶有數字的圖像,它可以正確讀取它們。 可用的圖像類型在這里: 工作圖像我正在嘗試將腳本與一種僅帶有數字的新型圖像一起使用,但它不起作用。 新的圖像類型在這里:非工作圖像

我的腳本如下:

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
-----------------------

請問有什么幫助嗎? 謝謝。

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

  1. Tesseract 允許您限制可以使用的字符范圍。 僅將其設置為數字。
  2. 使用某種形式的預處理來消除噪音。 Python 枕頭噪聲去除 function,或使用形態打開/關閉。
  3. 在網絡上執行微調訓練。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM