![](/img/trans.png)
[英]Problem With Char Recognition pytesseract/tesseract-OCR/ Won't Recognize
[英]issues with char recognition in pytesseract/tesseract-OCR. Recognize '+' as '4'
如標題中所述,我無法識別字符,腳本將“+”識別為“4”。 我怎樣才能提高准確性。 下面的圖像示例以及代碼。
我嘗試使用 # 中的代碼進行優化。 但我沒有成功
img = cv2.imread("imgbug.png")
img = cv2.resize(img, None, fx=1.98, fy=1.98, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
#img=cv2.threshold(cv2.GaussianBlur(img, (1, 1), 0), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
#img=cv2.threshold(cv2.bilateralFilter(img, 5, 75, 75), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cmd = pytesseract.image_to_string(img)
xx = pytesseract.image_to_boxes(img)
cv2.imshow('',img)
print(cmd)
print(xx)
My output is:
34+3=8
9+4=16
2+2=4
3 15 67 28 84 0
4 26 67 35 84 0
+ 29 67 44 82 0
3 45 67 58 84 0
= 59 71 73 80 0
8 75 67 88 84 0
9 16 39 28 56 0
+ 30 39 44 54 0
4 45 39 57 56 0
= 59 43 73 52 0
1 75 39 83 56 0
6 85 39 97 56 0
2 16 12 28 28 0
+ 30 12 44 26 0
2 45 12 58 28 0
= 59 15 73 24 0
4 75 12 87 28 0
嘗試將其更改為灰度,然后設置閾值。
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
像這樣的東西
這樣會提高識別率,但我懷疑問題出在庫本身。 如果這不起作用,我建議使用不同的庫或者嘗試類似 google vision
一種解決方案是將threshold
應用於輸入圖像。
您有三個輸入圖像:
img_lst = ["pfB7MnL.png", "0amEBuu.png", "6FHilyj.png"]
如果對每個輸入圖像應用閾值:
for img_nm in img_lst:
img = cv2.imread(img_nm)
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 127, 255, cv2.THRESH_BINARY_INV)[1]
結果將是:
當你閱讀:
txt = pytesseract.image_to_string(thr)
print(txt)
結果將是:
2+9=14
9+1=6
9+2=11
3+3=8
9+4=16
2+2=4
1+5=10
7+7=14
9+6=16
代碼:
import pytesseract
import cv2
img_lst = ["pfB7MnL.png", "0amEBuu.png", "6FHilyj.png"]
for img_nm in img_lst:
img = cv2.imread(img_nm)
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 127, 255, cv2.THRESH_BINARY_INV)[1]
txt = pytesseract.image_to_string(thr)
print(txt)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.