Python 如何使用语音GTT进行用户输入

Python 如何使用语音GTT进行用户输入,python,python-3.x,artificial-intelligence,google-text-to-speech,Python,Python 3.x,Artificial Intelligence,Google Text To Speech,我正在创建一个带有语音输入的简单比萨饼应用程序。 在创建这篇文章之前,我搜索了很多,没有发现任何有用的东西 fiddle没有正确地运行代码,所以不要在那里运行它 问题: 当我尝试在我的“输入”中使用“命令”(例如 count = 0 def talkToMe(audio): global count print(audio) text_to_speech = gTTS(text=audio, lang='en-us') text_to_speech.save

我正在创建一个带有语音输入的简单比萨饼应用程序。 在创建这篇文章之前,我搜索了很多,没有发现任何有用的东西

fiddle没有正确地运行代码,所以不要在那里运行它

问题:

当我尝试在我的“输入”中使用“命令”例如

count = 0

def talkToMe(audio):

    global count
    print(audio)
    text_to_speech = gTTS(text=audio, lang='en-us')
    text_to_speech.save(f'speech{count%2}.mp3')
    mixer.init()
    mixer.music.load(f'speech{count%2}.mp3')
    mixer.music.play()
    count += 1

def myCommand():


    r = sr.Recognizer()

    with sr.Microphone() as source:
        print('Ready...')
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)

    try:
        command = r.recognize_google(audio).lower()
        print('You said: ' + command + '\n')

    #loop back to continue to listen for commands if unrecognizable speech is received
    except sr.UnknownValueError:
        print('Your last command couldn\'t be heard')
        command = myCommand();

    return command

def assistant(command):

    def pick_or_deli():
        global delivery
        global customer_name 
        global customer_telephone

        delivery = input("pickup - pick up / delivery - delivery:" + command)
        delivery = delivery.upper()
        if delivery == "DELIVERY":
             while running == True:
        ..............................
我只是得到:

不要注意小写的“delivery”。 即使我改变这一行:

if delivery == "DELIVERY": 
为此:

if delivery == "delivery":
它不工作。即使我按enter键。系统也不会将其识别为输入


关于这方面的任何解决方案/建议都很好。

有些东西不太适合这里。在您的代码中,提示消息与图像中的输出不同。您是否尝试过在
之前打印
delivery
的值,如果delivery='delivery':…
?@Sven Krüger感谢您的回复。似乎是这样吗s好像打印没有显示任何内容。我已将这一行
if delivery==“delivery”:
更改为
if delivery:
中的命令,并将delivery指示为:
delivery=['delivery']
。因此,现在它可以识别它并成功跳过此部分,但它还将我的语音输入添加到名称输入:as:
提货或送货?准备好了吗…您再次说:送货名称:送货
,即使我按enter键,它也无法识别它为输入:
名称:送货请输入有效的输入名称:送货
什么您是否通过编写
delivery=input(“pickup…”+command)
来实现目标?是否要将
command
的值传递给函数
input
?因为添加两个字符串只是一个串联。因此,如果您只在控制台上按enter键,
input
的返回值将是一个空字符串。