在Python中更快地解读单词

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

因此,我目前正在使用python解密从OCR程序检索到的单词。

我正在使用的当前代码解读单词是:

import numpy as nm 
import pytesseract 
import cv2 
import ctypes
from PIL import ImageGrab 

def imToString(): 

    # Path of tesseract executable 
    pytesseract.pytesseract.tesseract_cmd =r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
    while(True): 

        # ImageGrab-To capture the screen image in a loop. 
        # Bbox used to capture a specific area. 
        cap = ImageGrab.grab(bbox =(687, 224, 1104, 240))


        # Converted the image to monochrome for it to be easily 
        # read by the OCR and obtained the output String. 
        tesstr = pytesseract.image_to_string( 
                cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY), 
                lang ='eng') 
        checkWord(tesstr)

def checkWord(tesstr):

    dictionary =['orange', 'marshmellow']

    scrambled = tesstr
    for word in dictionary:
        if sorted(word) == sorted(scrambled):
            print(word)


imToString() 

我想知道是否还有[[减少所需的时间:

    扫描/处理图像。
  • 浏览“字典”,因为还有更多的单词要经过。或其他更有效的选择。
  • 谢谢
  • python sorting processing-efficiency
    1个回答
    2
    投票
    您每次使用字典时都要对其进行排序,请对其进行一次整理并存储:

    import numpy as nm import pytesseract import cv2 import ctypes from PIL import ImageGrab def imToString(dictionary): # Path of tesseract executable pytesseract.pytesseract.tesseract_cmd =r'C:\Program Files (x86)\Tesseract-OCR\tesseract' while(True): # ImageGrab-To capture the screen image in a loop. # Bbox used to capture a specific area. cap = ImageGrab.grab(bbox =(687, 224, 1104, 240)) # Converted the image to monochrome for it to be easily # read by the OCR and obtained the output String. tesstr = pytesseract.image_to_string( cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY), lang ='eng') checkWord(tesstr, dictionary) def checkWord(tesstr, dictionary): scrambled = tesstr for word in dictionary: if sorted(word) == sorted(scrambled): print(word) # ... Create your dictionary somewhere else and pass it in: dictionary =[sorted('orange'), sorted('marshmellow')] imToString(dictionary)

    © www.soinside.com 2019 - 2024. All rights reserved.