Search code examples
pythonpython-3.xwebpython-tesseractpyautogui

I'm trying to get my Python Code to click on a button if a condition isn't matched


Currently it gives a click once the condition is met.

For example:

The code looks for a particular word on my screen. It matches with a set of data I have defined already.

How it should work:

  • If the screen has nothing on it, a click occurs.
  • If the screen has the word "cookie" on it, it beeps and exits.

What is occuring:

  • If the screen has nothing on it, a click occurs.
  • The screen has the word "cookie" on it. It clicks. It beeps and exits.

Sample Output:

  • No common elements.

  • No common elements.

  • No common elements.

  • {'cookie'}

The Third "No common elements", shouldn't have occured. The word cookie appeared after the second click. There's something wrong in how the if-else condition is being accessed. Thoughts?

Code below:

import pytesseract
import numpy as nm
import winsound
import pyautogui
import time
from PIL import ImageGrab

def imToString():
    pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
    while (True):

        source = [
"chocolate", "muffin", "cookie"
                 ]

        cap = ImageGrab.grab(bbox=(748, 626, 916, 646))

        tesstr = pytesseract.image_to_string(
            cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY),
            lang='eng')

        split_words =tesstr.split()

        normalized_input = [word.rstrip('.,!?').lower() for word in split_words]
        source_normalized = [word.lower() for word in source]

        set_a = set(source_normalized)
        set_b = set(normalized_input)
        match = set_a & set_b

        if match:
            print(match)
            frequency = 2500  # Set Frequency To 2500 Hertz
            duration = 1000  # Set Duration To 1000 ms == 1 second
            winsound.Beep(frequency, duration)
            return False
        else:
            print("No common elements")
            time.sleep(1)
            pyautogui.moveTo(826,309)
            pyautogui.click()


imToString()

Any inputs on what mistake I am making?


Solution

  • I cleaned up my code, and used the any() function as a check for the if-else block. Code is working as expected now.