0

我的脚本只是停止并在之后卡住engine.runandWait()......如果有人知道如何让它继续,我将不胜感激!似乎答案不在脚本本身,因为我尝试了超级简单的脚本......我还尝试了卸载和重新安装portaudiopyaudio并且pyttsx3

这是我的脚本:

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

2 回答 2

0

在 def talk 部分,您可以删除第一行和 while True 语句,删除 if run == False,并在 take 命令部分添加一个 elif 语句,在命令中说 elif 'thank you':exit(你的助手)。试试看

于 2021-07-29T01:58:37.327 回答
0

这是一个简短的 pyttsx 测试程序。

将其保存为“pyttsx_test.py”,然后运行它。如果您的版本工作正常,它将说出代码并将其打印到 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()
于 2021-07-29T03:21:15.097 回答