3
\$\begingroup\$

So I'm currently using python to unscramble words that are retrieved from an OCR program (pytesseract).

The current code I am using to unscramble words is this:

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() 

I wanna know if there is anyway to reduce the time it takes to:

  • Scan/Process the image.
  • Look through the 'dictionary', as there are many more words to go through. Or another more efficient alternative.

Thanks

\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

I can't speak to the image processing, but you can easily make the word check faster by preprocessing into an actual dict:

from collections import defaultdict

def build_dict(words):
    sorted_words = defaultdict(set)
    for word in words:
        sorted_words[''.join(sorted(word))].add(word)
    return sorted_words

def check_word(word, sorted_words):
    return sorted_words.get(''.join(sorted(word)), {})

This way, you create a dictionary that maps the sorted version of the word to a set of all the words that it could be. You then check whether your scramble set exists, and if found returns all the possible words it could be.

In use:

>>> sorted_words = build_dict(['orange', 'yellow', 'taco', 'coat'])
>>> check_word('blue', sorted_words)
{}
>>> check_word('rngaeo', sorted_words)
{'orange'}
>>> check_word('octa', sorted_words)
{'coat', 'taco'}
\$\endgroup\$
0
2
\$\begingroup\$

You are sorting your dictionary each time you use it, do it once and store it.

Also, you are sorting your scrambled word more than once, only do it once:

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
    sorted_scrambled = sorted(scrambled) # Only do this 1 time
    if sorted_scrambled in dictionary:
        print(dictionary[sorted_scrambled]

# ... Create your dictionary somewhere else and pass it in:
dictionary ={sorted('orange'): 'orange',sorted('marshmellow'):  'marshmellow'}

imToString(dictionary) 
\$\endgroup\$
3
  • \$\begingroup\$ I've tried this, and it's giving me an error: unhashable type: 'list' \$\endgroup\$
    – Adam
    Jun 2, 2020 at 17:03
  • \$\begingroup\$ We need more of an error than that - what's the stack trace, etc... \$\endgroup\$
    – mikeb
    Jun 3, 2020 at 13:49
  • \$\begingroup\$ If you want code that is verified to be correct, mock your pytesseract stuff and post an example: stackoverflow.com/help/minimal-reproducible-example \$\endgroup\$
    – mikeb
    Jun 3, 2020 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy