Создать цикл речевого распознавания - PullRequest
0 голосов
/ 03 октября 2019

Я создаю голосового помощника в python, и я хотел бы знать, как я могу сделать петлю, чтобы каждый раз, когда помощник отвечал, включался микрофон, чтобы я мог спросить его снова

#Use Google TTS for say something
def speak(text):
    tts = gTTS(text=text, lang="es", slow=False)
    filename = "voiceww.mp3"
    tts.save(filename)
    playsound.playsound(filename)

#Use mic for get my voice
def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print('Di algo')
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)
        said = ""

#Print the answer in console
        try:
            #print('Listo')
            said = r.recognize_google(audio, language='es')
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said
#Here the function activates
text = get_audio()

#This is a example question
if 'hello computer' in text:
    speak('Hi! How are you?')

#This is where I want to turn on the microphone again, process my voice and say the answer and so on to infinity

1 Ответ

0 голосов
/ 07 октября 2019
#Use Google TTS for say something
def speak(text):
    tts = gTTS(text=text, lang="es", slow=False)
    filename = "voiceww.mp3"
    tts.save(filename)
    playsound.playsound(filename)

#Use mic for get my voice
def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print('Di algo')
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)
        said = ""

#Print the answer in console
        try:
            #print('Listo')
            said = r.recognize_google(audio, language='es')
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

Вот как вы держите микрофон включенным:

while True:
    # Here the function activates
    text = get_audio().lower
    # These are examples
    if 'hello computer' in text:
        speak('Hi! How are you?')
    elif "good morning" in text:
        speak("Hey good morning")
...