raise RuntimeError('run loop already started') RuntimeError: run loop already started Exception in thread PredictFunction:

Viewed 14

I am running the script in python that uses multithreading, listening and prediction is happening in different threads. But I am getting the above error continously in loop. The complete function is written below.

import threading
import time
import sounddevice as sd
import librosa
import numpy as np
from tensorflow.keras.models import load_model
import pyttsx3

#### SETTING UP TEXT TO SPEECH ###
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0')

def speak(audio):
    engine.say(audio)
    engine.startLoop(False)
    engine.runAndWait()
    engine.endLoop()

##### CONSTANTS ################
fs = 48000
seconds = 2

model = load_model("saved_model/WWD.h5")

##### LISTENING THREAD #########
def listener():
    while True:
        myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
        sd.wait()
        mfcc = librosa.feature.mfcc(y=myrecording.ravel(), sr=fs, n_mfcc=40)
        mfcc_processed = np.mean(mfcc.T, axis=0)
        prediction_thread(mfcc_processed)
        time.sleep(0.001)


def voice_thread():
    listen_thread = threading.Thread(target=listener, name="ListeningFunction")
    listen_thread.start()

##### PREDICTION THREAD #############
def prediction(y):
    prediction = model.predict(np.expand_dims(y, axis=0))
    if prediction[:, 1] > 0.999999:
        if engine._inLoop:
            engine.endLoop()

        speak("Hello, What can I do for you?")


    time.sleep(0.1)

def prediction_thread(y):
    pred_thread = threading.Thread(target=prediction, name="PredictFunction", args=(y,))
    pred_thread.start()

voice_thread()

complete traceback in loop

Exception in thread PredictFunction: Traceback (most recent call last): File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "RunParallely.py", line 49, in prediction speak("Hello, What can I do for you?") File "RunParallely.py", line 18, in speak engine.runAndWait() File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/site-packages/pyttsx3/engine.py", line 177, in runAndWait raise RuntimeError('run loop already started') RuntimeError: run loop already started

Exception in thread PredictFunction: Traceback (most recent call last): File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "RunParallely.py", line 49, in prediction speak("Hello, What can I do for you?") File "RunParallely.py", line 18, in speak engine.runAndWait() File "/home/sumit/anaconda3/envs/speechenv/lib/python3.7/site-packages/pyttsx3/engine.py", line 177, in runAndWait raise RuntimeError('run loop already started') RuntimeError: run loop already started

0 Answers
Related