因此,我目前正在使用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() 我想知道是否有减少查字典所需的时间,因为有更多的单词要通过。
发布于 2020-06-03 20:59:27
正如注释中提到的那样,我们很难告诉您如何更快地获得代码,更多有关代码运行时上下文的信息。但是,根据您在代码中显示的内容,我将进行以下修改:
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中有特定的含义,这对阅读您的代码的人来说可能有点混乱。如果这不是你想要的,给我们更多的精确信息。
https://codereview.stackexchange.com/questions/243273
复制相似问题