![](/img/trans.png)
[英]Why does pytesseract fail to recognize digits in this simple image?
[英]Why does pytesseract not recognize correctly?
好的,所以我一直在嘗試將圖像更改為任何有效的圖像,但我似乎找不到正確的設置..
如您所見,圖片已經很簡單了,但它仍然無法從圖像中識別“1 BB”。有什么提示嗎?
img = Image.fromarray(img)
imp_arr = np.asarray(img)
imp_arr = (np.floor(imp_arr / 140.0) * 255.0).astype('uint8')
img = Image.fromarray(imp_arr, mode='L')
width, height = img.size
img = img.resize((width*3, height*3), Image.BICUBIC)
width, height = img.size
img = img.resize((width*2, height*2), Image.HAMMING)
width, height = img.size
img = img.resize((int(width*0.3), int(height*0.3)), Image.BICUBIC)
img = ImageEnhance.Brightness(img).enhance(0.7)
img = ImageEnhance.Sharpness(img).enhance(2)
img = ImageEnhance.Contrast(img).enhance(2)
amount = pytesseract.image_to_string(img, config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
這只是一個示例,我嘗試正確調整它以將正確的文本轉換為字符串。 有時它可以工作,有時它會打印出亂碼。 問題是……它每次都需要工作,特別是對於像這張這樣清晰的圖片。 有沒有一個策划者對這個問題有一個簡單的解決方案? 先感謝您。
安裝 Tesseract OCR、Pillow 和 pytesseract 后,我將您的圖像保存為igor.png
並運行以下代碼,我在pytesseract 的文檔中找到了這些代碼:
#!/usr/bin/env python
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open("igor.png")))
它打印預期的結果:
1BB
如果我通過將字母B
添加到tessedit_char_whitelist
來更正您的初始代碼,它也可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.