1

My script just stop and get stuck after engine.runandWait()... If someone has any idea of how to make it continue I would appreciate ! It seems that the answer isn't in the script itself because I tried scripts that are super simple... I also tried to uninstall and reinstall portaudio, pyaudio and pyttsx3

Here is my script :

import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import time
import pyjokes

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.setProperty('voices', "french")

def talk(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()


def take_command():
    command = ''

    try:
        with sr.Microphone() as source:
            print("...")
            voice = listener.listen(source)
            command = listener.recognize_google(voice,language = 'fr-FR')
            command = command.lower()
    except:
        talk("Je me mets en veille")
        pass

    return command

def run_jeff(run):
    command = take_command()
    if 'youtube' in command:
        command = command.replace('youtube','')
        command = command.replace('ouvre','')
        pywhatkit.playonyt(command)

    elif "stop" in command:
        talk("Je vais dodo")
        run = False

    elif 'bonjour' in command or 'salut' in command :
        talk('Bonjour, comment allez-vous ?')
        talk(info)

    elif 'blague' in command :
        talk(pyjokes.get_joke())



    else :
        talk("Pouvez-vous répétez je n'ai pas compris ?")
    print(command)

run = True

while True:
    run_jeff(run)
    if run == False:
        exit()
    
Reema Q Khan
  • 878
  • 1
  • 7
  • 20
  • `engine = pyttsx3.init()` should only need to be called once, so you should try removing it from `talk` function. That will preserve your property settings. – Derek Jul 29 '21 at 02:57

4 Answers4

0

In the def talk section, you can remove the first line and in the while True statement, delete if run == False, and add a elif statement to the take command sections saying elif 'thank you' in command: exit(the name of your assistant). Try it

0

Here is a short pyttsx testing program.

Save it as 'pyttsx_test.py' then run it. If your version is working correctly it will speak the code and print it to shell.

"""pyttsx_test.py
I am a text to speech test
"""

import pyttsx3

engine = pyttsx3.Engine()
engine.setProperty( "rate", 200 )
engine.setProperty( "volume", 1.0 )
engine.say( 'say something' )
engine.runAndWait()

with open( 'pyttsx_test.py', mode='rt') as files:
    lines = files.readlines()

lines = [ x.strip(' ') for x in lines ] # remove un-necessary spaces
lines = [ x for x in lines if x!='\n'] # remove empty lines

for a in lines:
    print( a, end ='' )
    engine.say( a )
    engine.runAndWait()
Derek
  • 1,916
  • 2
  • 5
  • 15
0

You should change your input device using device_index. (default=0)

with sr.Microphone(device_index=0) as source:

You can see all your devices using:

for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print(f'{index}, {name}')
Lenoch
  • 11
  • 1
  • 3
0

Every time you initiate the engine or "init()" you have to set property again. You duplicated your lines and used it wrong. if you wish it to work delete thoes lines -

engine = pyttsx3.init()
voices = engine.setProperty('voices', "french")

def talk(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

and replace them with this -

def talk(text):
    engine = pyttsx3.init()
    voices = engine.setProperty('voices', "french")
    engine.say(text)
    engine.runAndWait()