Today we are going to make a Voice Assistant Using Python that can behave as Jarvis assistant or Google assistant.
This assistant can talk to you or communicate with you using your voice like Google Assistant or Jarvis.
But, this will not be that advanced, this will totally depend on the if..else.. condition which we will use inside our Program.
There are three requirements–
1. PyAudio — to install pyaudio, go to the terminal and type,
pip install pyaudio
2. SpeechRecognition — to install SpeechRecognition, go to the terminal and type,
pip install speechrecognition
3. Pyttsx3 — to install Pyttsx3, go to terminal and type,
pip install pyttsx3
# importing pyttsx3 import pyttsx3 # importing speech_recognition import speech_recognition as sr # creating take_commands() function which # can take some audio, Recognize and return # if there are not any errors def take_commands(): # initializing speech_recognition r = sr.Recognizer() # opening physical microphone of computer with sr.Microphone() as source: print('Listening') r.pause_threshold = 0.7 # storing audio/sound to audio variable audio = r.listen(source) try: print("Recognizing") # Recognizing audio using google api Query = r.recognize_google(audio) print("the query is printed='", Query, "'") except Exception as e: print(e) print("Say that again sir") # returning none if there are errors return "None" # returning audio as text return Query # creating Speak() function to giving Speaking power # to our voice assistant def Speak(audio): # initializing pyttsx3 module engine = pyttsx3.init() # anything we pass inside engine.say(), # will be spoken by our voice assistant engine.say(audio) engine.runAndWait() # Driver Code if __name__ == '__main__': # using while loop to communicate infinitely while True: command = take_commands() if "exit" in command: Speak("Sure sir! as your wish, bai") break if "insta" in command: Speak("Best python page on instagram is pythonhub") if "learn" in command: Speak("copyassignment website is best to learn python") if "code" in command: Speak("You can get this code from copyassignment website")
We have used three modules of Python here, pyaudio is working in the background and is necessary to be installed.
Here, we have made the take_command() function so that it can be used here as the ear of our voice assistant in Python which ultimately uses the speech_recognition module.
Similarly, we have made the Speak() function so that it can be used here as the mouth of our voice assistant in Python which ultimately uses the pyttsx3 module.