Pytesseract 真的很慢

问题描述 投票:0回答:2

所以我尝试从 MS Teams 读出文本并使用该文本在键盘上进行输入。 现在,我使用线程模块,让一个线程用于输入,一个线程用于 image_to_string。以下是 image_to_string 的函数。

def imToString():
    global message
    print("Image getting read")
    pytesseract.pytesseract.tesseract_cmd ='C:\\Users\\gornicec\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
    while(True):
        print("preIMGgrab")
        
        cap = ImageGrab.grab(bbox=(177, 850, 283, 881))   
        grayCap = cv2.cvtColor(np.array(cap), cv2.COLOR_BGR2GRAY)
        
        print("postIMGgrab")
        t = time.perf_counter()
        print("preMSG" + str(t))

        message = pytesseract.image_to_string(
                grayCap,
                lang ='deu',config='--psm 6')
   
        print(str(message) + "was read" + str(time.perf_counter() - t))

我不知道怎么做,但读取 1000 像素大的图像大约需要 8 秒。我需要这个时间最长为 2 秒。我将在最后添加整个代码。如果有任何方法可以使其更快或以不同的方式进行,请告诉我。

完整代码:

import numpy as np
import time
import pytesseract
from win32gui import GetWindowText, GetForegroundWindow
import win32api
import cv2
import pyautogui
from PIL import ImageGrab
import threading
from ahk import AHK
import keyboard

message = ""
ahk = AHK(executable_path='C:\\Program Files\\AutoHotkey\\AutoHotkey.exe')

def Controls():
    global message
    while True:
        booleanVal = True
        if booleanVal:
            #imToString()
            print("message")
            #print("rechts" in message.lower())
            #print(f'LÄNGE: {len(message)}')
            if "vorne" in message.lower():
                # Control(message, 'w')
                ahk.key_press('w')
                #message = ""

            if "hinten" in message.lower():
                # Control(message, 's')
                ahk.key_press('s')
                #message = ""

            if "links" in message.lower():
                # Control(message, 'a')
                ahk.key_press('a')
                #message = ""

            if "rechts" in message.lower():
                # Control(message, 'd')
                #print("HAHAHA")
                ahk.key_press('d')
                #message = ""

            if "greif" in message.lower():
                ahk.key_press('space')
                #message = ""
            time.sleep(0.5)

#IMGTOSTRING---

controls = threading.Thread(target=Controls)
controls.start()
grab = threading.Thread(target=imToString)
grab.start()

python python-3.x performance tesseract python-tesseract
2个回答
0
投票

pytesseract 不适合大量图像或已经在内存中的图像,它将它们写入文件,然后将文件路径传递给 tesseract cli,如果你想提高脚本的性能,请尝试使用直接工作的库与 tesseract api.

像这样:https://pypi.org/project/tess-py-api/


0
投票

主要是由于使用多处理时的开销...... 尝试将其添加到您的脚本中:

import os
os.environ['OMP_THREAD_LIMIT'] = '1'
© www.soinside.com 2019 - 2024. All rights reserved.