即使使用配置,tesseract也根本不准确

问题描述

我的代码

for index,img in enumerate(data): # data is list of base64 decoded strings
    b64 = base64.b64decode(bytes(img[22:],encoding='utf-8'))
    raw = BytesIO(b64)
    im = Image.open(raw).convert('LA')
    pixels = im.load()
    width,height = im.size
    for x in range(width):
        for y in range(height):
            if pixels[x,y][0] > 100: pixels[x,y] = (255,255)
            else: pixels[x,y] = (0,255)
    print(PyTesseract.image_to_string(im,config='tessedit_char_whitelist=1234567890plus?'))

我的图片

enter image description here

输出
Te Ys
⠀ 我可以做些什么来做得更好,我尝试在配置密钥⠀p

中使用从0到13的每个psm和-c标志

解决方法

您需要反转图像。这样就可以了。

import pytesseract
import cv2

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

image = cv2.imread('addition.png',0)
image = 255 - image

for psm in range(6,13+1):
    config = '--oem 3 --psm %d' % psm
    txt = pytesseract.image_to_string(image,config = config,lang='eng')
    print('psm ',psm,':',txt)

在所有psm值上都能获得良好结果

psm  6 : 18 plus 16?
psm  7 : 18 plus 16?
psm  8 : 18 plus 16?
psm  9 : 18 plus 16?
psm  10 : 18 plus 16?
psm  11 : 18 plus 16?
psm  12 : 18 plus 16?
psm  13 : 18 plus 16?