我需要让pytesseract.image_to_string更快

问题描述 投票:1回答:1

我正在捕获屏幕,然后使用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.x numpy python-imaging-library python-tesseract
1个回答
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')
© www.soinside.com 2019 - 2024. All rights reserved.