skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. You can’t actually make an AI like that with just a bunch of if-elif-else statements. For tasks like those that you want to achieve, you should go for things like Natural Language Processing. Python modules like spacy can you help with that. First you should extract what the user said (which you are doing very well) and then give that to an Natural Language Processing algorithm that can provide you with the intent of the speech and after that you should go ahead with execution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search