R
R
RieldaBot2022-04-15 16:04:09
Python
RieldaBot, 2022-04-15 16:04:09

Python, Speech Recongition | How to implement this?

How to do something similar to argument(arg1, arg2...) but in the voice assistant for example I say test 1 it removes the test and does action 1

Python code:

import speech_recognition
import pyttsx3
import os

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

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)

def speak(text):
    """Used to speak whatever text is passed to it"""
    
    engine.say(text)
    engine.runAndWait()

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

commands_dict = {
    'commands': {
        'test': ['тест']
    }
}

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

def test():
    query = listen_command()
    print(query)
    if 'тест' in query:
        testing = query.replace('тест', '')
        print('test in ' + testing)

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question