我需要让 pytesseract.image_to_string 更快
I need to make pytesseract.image_to_string faster
我正在捕获屏幕,然后使用 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"""
您好,我的解决方案是缩小图像。
是的,它可能会影响 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')
我正在捕获屏幕,然后使用 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"""
您好,我的解决方案是缩小图像。
是的,它可能会影响 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')