我试图让 PyTesseract OCR 从这个简单且裁剪良好的图像中读取数字,但由于某种原因,它无法做到这一点。
from PIL import Image
import pytesseract as p
def obtain_balance(a):
im = Image.open(a)
width,height = im.size
a = 300*5 - 120
# print(width,height)
left = 155+a
top = 5
right = 360+a
bottom = 120
m1 = im.crop((left, top, right, bottom))
text = p.image_to_string(m1,lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789').split()
print(text)
m1.show()
return text
obtain_balance('cur.jpg')
输出 :
[]
最佳答案
执行 OCR 时,重要的是预先处理图像,以便 所需的前景文本为黑色,背景为白色 .为此,我们可以使用 OpenCV 对图像进行 Otsu 阈值处理并获得二值图像。然后我们应用轻微的高斯模糊来平滑图像,然后将其放入 Pytesseract。我们使用 --psm 6
配置将图像视为单个统一的文本块。见 here更多配置选项。
这是 Pytesseract 的预处理图像和结果
PRACTICE ACCOUNT
$9,047.26~ i
代码
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
关于python - PyTesseract OCR 无法从简单图像中读取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237973/