0

I am making a speech recognition program in python where I want that when I say "above", it will continuously run the loop to press the down key until I again don't say above, but now after I included threading to make the speech recognizer run continuously, the whole main loop is not recognizing the res

from speech import *
import queue
import threading
import pyautogui
import keyboard
import time
import sys

def speech_func(res_queue):
    while True:
        res = speech()
        res_queue.put(res)

res_queue = queue.Queue()
prc=threading.Thread(target=speech_func, args=(res_queue,))
prc.start()

flower_active=False

while True:
    res = res_queue.get()
    
    if "above" in res:
        flower_active = not flower_active

    while flower_active:
        keyboard.press_and_release('down')
        time.sleep(0.1)

    if "after" in res:
        pyautogui.moveTo(1010,130)
        pyautogui.click()

    if "before" in res:
        pyautogui.moveTo(890,130)
        pyautogui.click()

    if "music" in res:
        pyautogui.moveTo(1680,130)
        pyautogui.click()
                         
    if "below" in res:
        keyboard.press_and_release('pageup')

    if "ending" in res:
        prc.join()
        sys.exit(0)

If I remove the threading everything works fine except the down arrow which when i say "above" will start running the loop but won't stop even if I say "above" again

5
  • You don't give the while flower_active: loop a chance to end. Commented May 28, 2024 at 17:47
  • I made a different threading for that part, but still the main loop didn't work Commented May 28, 2024 at 17:59
  • you are not using multiprocessing. Commented May 28, 2024 at 18:23
  • you should run while-loop with keyboard.press_and_release('down') in separate thread because while flower_active: blocks all code. Commented May 28, 2024 at 19:09
  • or you should use if instead of while in if flower_active and while True will repeat this part of code. But it may need also check if queue is not empty before get data from queue - this way it will not block all loop. Commented May 28, 2024 at 19:11

1 Answer 1

0

while flower_active: is blocking all code so it can't get next command from queue. It would need to run while flower_active: in separate thread. And it may need other queue to stop loop and exit thread.

You can also try to use if instead of while and then while True will repeat it

while True:

    # ... code ...

    if flower_active:
        keyboard.press_and_release('down')
        time.sleep(0.1)

But there is other problem: res_queue.get() waits for data and it blocks code and it may need to check if not res_queue.empty() before .get()

while True:

    if not res_queue.empty():
        res = res_queue.get()
               
        if "above" in res:
            flower_active = not flower_active

        elif "after" in res:
            pyautogui.moveTo(1010,130)
            pyautogui.click()

        elif "before" in res:
            pyautogui.moveTo(890,130)
            pyautogui.click()

        elif "music" in res:
            pyautogui.moveTo(1680,130)
            pyautogui.click()
                             
        elif "below" in res:
            keyboard.press_and_release('pageup')

        elif "ending" in res:
            prc.join()
            sys.exit(0)
            
    # --- outside of `if not res_queue.empty():`
    
    if flower_active:
        keyboard.press_and_release('down')
        time.sleep(0.1)

I didn't test it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.