R
R
RieldaBot2022-03-09 15:18:29
Python
RieldaBot, 2022-03-09 15:18:29

Speech Recognition python | How to answer by name?

How to make the assistant answer only when they say his name, for example: when you say hello - he does not answer, when you say hello Zhora - then he answers hello.

my code:

import speech_recognition

sr = speech_recognition.Recognizer()
sr.pause_threshold = 0.5

commands_dict = {
    'commands': {
        'greeting': ['привет', 'приветствую']
    }
}

def listen_command():    
    try:
        with speech_recognition.Microphone() as mic:
            sr.adjust_for_ambient_noise(source=mic, duration=0.5)
            audio = sr.listen(source=mic)
            query = sr.recognize_google(audio_data=audio, language='ru-RU').lower()
            
        return query
    except speech_recognition.UnknownValueError:
        return 'Не понял что ты сказал :/'

def greeting():
    return 'Приветики!'

def main():
    query = listen_command()
    
    for k, v in commands_dict['commands'].items():
        if query in v:
            print(globals()[k]())
        
if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kadabrov, 2022-03-09
@RieldaBot

if the first word is Zhora then ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question