![](/img/trans.png)
[英]Why does pytesseract fail to recognise digits from image with darker background?
[英]Why pytesseract does not recognise single digits?
我正在一個網站上執行 ocr,特別是在這兩個圖像上:
我對 OCR 相當陌生,我使用以下內容:
from PIL import Image
import pytesseract
my_image = '....png'
text = pytesseract.image_to_string(Image.open(my_image))
在第二張圖片中,它可以識別除單個數字 3、4、5、6 之外的所有內容。
在第一張圖像中,它也無法識別單個數字。
我通過調整圖像大小、反轉它們和使用閾值來預處理圖像。
這是一種標准字體,所以我知道還有其他方法可以做到這一點,但在一定程度上它對我有用,所以我想在進入更高級的東西之前保持簡單。
對於這兩個圖像,您可以
對於第一張圖像,您可以選擇圖像的一部分:
結果將是:
62001
33000
代碼:
import cv2
import pytesseract
img1 = cv2.imread("lNKH4.png") # "FX2in.png"
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*2, h*2))
gry1 = gry1[30:(h*2), w+50:w*2]
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt1 = pytesseract.image_to_string(thr1, config="--psm 6 digits")
print(txt1)
cv2.imshow("thr1", thr1)
cv2.waitKey(0)
對於第二張圖片:
結果將是:
2
3 1.28 4.50 9.00
4 2.00 3.75 3.00
5 3.50 4.33 1.72
6 7.00 6.00 1.28
相同的代碼,只需刪除以下行:
gry1 = gry1[30:(h*2), w+50:w*2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.