[英]Pytesseract OCR wrong text recognition
當我使用 Pytesseract 識別此圖像中的文本時,Pytesseract 返回7A51k但此圖像中的文本是7,451k 。
如何使用代碼而不是提供更清晰的源圖像來解決此問題?
我的代碼
import pytesseract as pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D:\\App\\Tesseract-OCR\\tesseract'
img = Image.open("captured\\amount.png")
string = pytesseract.image_to_string(image=img, config="--psm 10")
print(string)
我有一個兩步解決方案
當您讀取閾值圖像時:
7,451k
代碼:
import cv2
from pytesseract import image_to_string
img = cv2.imread("4ARXO.png")
(h, w) = img.shape[:2]
img = cv2.resize(img, (w*3, h*3))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)
如果調整大小后圖像模糊沒有問題,您可以對其進行閾值化,並按照AlexAlex的建議進行反轉:
output:7,451k
import numpy as np
import pytesseract
import cv2
# Read Image
gray = cv2.imread('2.png', 0)
# Resize
gray = cv2.resize(gray, (600,200))
# Inverting
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray
# Thresholding
emp[emp==0] = 255
emp[emp<100] = 0
text = pytesseract.image_to_string(emp, config='outputbase digits')
print(text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.