首页 > 解决方案 > 我需要让 pytesseract.image_to_string 更快

问题描述

我正在捕获屏幕,然后使用 tesseract 从中读取文本以将其转换为字符串问题是它对于我需要的东西来说很慢,我正在做大约 5.6fps 并且我需要更像 10-20。(我没有'不要放我使用的导入,因为你可以在代码中看到它们)

我尝试了我所知道的一切,但没有任何帮助

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

time.sleep(7)

def getDesiredWindow():
    """Returns the top-left and bottom-right of the desired window."""
    print('Click the top left of the desired region.')
    pt1 = detectClick()
    print('First position set!')
    time.sleep(1)
    print('Click the bottom right of the desired region.')
    pt2 = detectClick()
    print('Got the window!')
    return pt1,pt2

def detectClick():
    """Detects and returns the click position"""
    state_left = win32api.GetKeyState(0x01)
    print("Waiting for click...")
    while True:
        a = win32api.GetKeyState(0x01)
        if a != state_left: #button state changed
            state_left = a
            if a < 0:
                print('Detected left click')
                return win32gui.GetCursorPos()


def gettext(pt1,pt2):
    # From the two input points, define the desired box
    box = (pt1[0],pt1[1],pt2[0],pt2[1])
    image = ImageGrab.grab(box)
    return pytesseract.image_to_string(image)
"""this is the part where i need it to be faster"""

标签: python-3.xnumpypython-imaging-librarypython-tesseract

解决方案


嗨,我的解决方案是使图像更小。

是的,它可能会影响 image_to_string 结果并使其不准确,但在我的情况下,由于我的图像宽度为 1500,因此我设法获得了 3 倍的速度。尝试更改 basewidth 并重试:

from PIL import Image

basewidth = 600
img = Image.open('yourimage.png')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
img.save('yourimage.png')

推荐阅读