Search code examples
pythonpython-3.xspeech-recognitionspeech-to-text

How to recognize and execute multiple commands from a phrase in python?


I have created a program that listens our voice using google speech recognition and then execute the program which is scripted for that phrase. Here is the partial code:

import speech_recognition as sr
import os

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en-in')
        print("User said: ",query)

    except Exception as e:
        # print(e)    
        print("Say that again please...")
        os.startfile('JARVIS.py')
        os._exit()
        return "None"
    return query

if __name__ == "__main__":
    while True:
        query = takeCommand().lower()
        command_history()
        # Logic for executing tasks based on query
        if 'chrome' in query:
            os.startfile("C:/............./chrome.exe")
        elif 'photoshop' in query:
            os.startfile("C:/............../photoshop.exe")

It works fine, but what if I want to recognize multiple commands from the voice? For example, if I would say, "open chrome and photoshop", then it will only open chrome because it is listed first, and after that the program will end. But I want to extract every command from the speech, So, how to do it?

Any help will be considered great, and sorry for my bad english


Solution

  • Your program ends at chrome because you are using an if-elif. Try with if...if