首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python更快地解读单词

用Python更快地解读单词
EN

Code Review用户
提问于 2020-06-02 17:11:23
回答 1查看 71关注 0票数 1

因此,我目前正在使用python来解读从OCR程序中检索并签入“字典”中的单词。

我现在用来解读单词的代码是:

代码语言:javascript
复制
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() 

我想知道是否有减少查字典所需的时间,因为有更多的单词要通过。

EN

回答 1

Code Review用户

发布于 2020-06-03 20:59:27

正如注释中提到的那样,我们很难告诉您如何更快地获得代码,更多有关代码运行时上下文的信息。但是,根据您在代码中显示的内容,我将进行以下修改:

代码语言:javascript
复制
import numpy as nm 
import pytesseract 
import cv2 
import ctypes
from PIL import ImageGrab 

def im_to_string(): 

    # 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. 
        tes_str = pytesseract.image_to_string( 
                cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY), 
                lang ='eng') 
        check_word(tes_str)

words_dictionary = ['orange', 'marshmellow']
scrambled_words_dictionary = set(sorted(current_word) for current_word in words_dictionary)

def check_word(tes_str):
    if sorted(tes_str) in scrambled_words_dictionary:
        print(tes_str)

im_to_string() 

这是基于这样一种假设,即单词字典不会在屏幕抓取之间发生变化。

下面是我对您的代码所做的一些修改:

  • 我在check_word()之外定义了单词字典,这样每次调用该函数都不需要重新创建它
  • 我创建了一个单词被置乱的集合,因为一个集合被优化以检查成员资格。你也只是对那些乱七八糟的话感到高兴而已。
  • 我还更改了dictionary变量的名称,因为字典在Python中有特定的含义,这对阅读您的代码的人来说可能有点混乱。

如果这不是你想要的,给我们更多的精确信息。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/243273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档