首页 > 解决方案 > 如何使用语音 gtts 进行用户输入

问题描述

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

这是完整的代码

小提琴没有正确运行代码,所以不要在那里运行它。

问题:

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

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:
        ..............................

我只是得到:

这个

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

if delivery == "DELIVERY": 

对此:

if delivery == "delivery":

它不起作用。即使我按Enter。系统不会将其识别为输入。

任何关于此的解决方案/建议都会很棒。

标签: pythonpython-3.xartificial-intelligencegoogle-text-to-speech

解决方案


推荐阅读