python-3.x - 我需要让 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"""
解决方案
嗨,我的解决方案是使图像更小。
是的,它可能会影响 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')
推荐阅读
- macos - 如何使用 Homebrew 在我的 Mac 上恢复到 Python3.8?
- javascript - 可以在 if 语句中嵌入 html 吗?
- python - 如何使用 matplotlib 保存图像文件。方法 savefig 不起作用
- python - 在不使用日期时间的情况下将日期转换为一年中的某一天
- .net - 无法在 Internet Information Services V 8.5 中使用 https 设置
- python - 无法使用 twitter bot 回复推文
- python - 无法在 Visual Studio Code 中导入库
- r - 将数据框转换回嵌套列表
- node.js - MongoDB:更新文档并返回更新的文档
- c++ - 如何使用链表修复内存泄漏?