我需要使pytesseract.image_to_string更快

时间:2019-07-04 16:49:52

标签: python-3.x numpy python-imaging-library python-tesseract

我正在捕获屏幕,然后使用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"""

1 个答案:

答案 0 :(得分:0)

您好,我的解决方案是缩小图像。

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

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')