Today we are going to make a Voice Assistant Using Python which can behave as Jarvis like 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 much 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 terminal and type,
pip install pyaudio
2. SpeechRecognition — to install SpeechRecognition, go to terminal and type,
pip install speechrecognition
3. Pyttsx3 — to install Pyttsx3, go to terminal and type,
pip install pyttsx3
Code
# 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")
Explanation:
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.
Output
Watch the video for more:
Also Read:
Create Language Translator Using Python
Covid-19 Tracker Application Using Python
YouTube Video Downloader Application Using Python
GUI Application To See wifi password in Python
Get WiFi Passwords With Python
this code does not work in pyhton 3.8?
We have’nt checked but it should work
sudo apt-get install espeak
install espeak on your device then it work fine.
-thank you
ERROR: Command errored out with exit status 1:
command: ‘c:\users\risha\pycharmprojects\shortner\venv\scripts\python.exe’ -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘”‘”‘C:\\Users\\risha\\AppData
\\Local\\Temp\\pip-install-fsk2jhzj\\pyaudio\\setup.py’”‘”‘; __file__=’”‘”‘C:\\Users\\risha\\AppData\\Local\\Temp\\pip-install-fsk2jhzj\\pyaudio\\setup.py’”‘”‘;f=get
attr(tokenize, ‘”‘”‘open’”‘”‘, open)(__file__);code=f.read().replace(‘”‘”‘\r\n’”‘”‘, ‘”‘”‘\n’”‘”‘);f.close();exec(compile(code, __file__, ‘”‘”‘exec’”‘”‘))’ install –
-record ‘C:\Users\risha\AppData\Local\Temp\pip-record-dzj167aq\install-record.txt’ –single-version-externally-managed –compile –install-headers ‘c:\users\risha\py
charmprojects\shortner\venv\include\site\python3.9\pyaudio’
cwd: C:\Users\risha\AppData\Local\Temp\pip-install-fsk2jhzj\pyaudio\
Complete output (9 lines):
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.9
copying src\pyaudio.py -> build\lib.win-amd64-3.9
running build_ext
building ‘_portaudio’ extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/
—————————————-
ERROR: Command errored out with exit status 1: ‘c:\users\risha\pycharmprojects\shortner\venv\scripts\python.exe’ -u -c ‘import sys, setuptools, tokenize; sys.argv[0]
= ‘”‘”‘C:\\Users\\risha\\AppData\\Local\\Temp\\pip-install-fsk2jhzj\\pyaudio\\setup.py’”‘”‘; __file__=’”‘”‘C:\\Users\\risha\\AppData\\Local\\Temp\\pip-install-fsk2j
hzj\\pyaudio\\setup.py’”‘”‘;f=getattr(tokenize, ‘”‘”‘open’”‘”‘, open)(__file__);code=f.read().replace(‘”‘”‘\r\n’”‘”‘, ‘”‘”‘\n’”‘”‘);f.close();exec(compile(code, __fi
le__, ‘”‘”‘exec’”‘”‘))’ install –record ‘C:\Users\risha\AppData\Local\Temp\pip-record-dzj167aq\install-record.txt’ –single-version-externally-managed –compile –i
nstall-headers ‘c:\users\risha\pycharmprojects\shortner\venv\include\site\python3.9\pyaudio’ Check the logs for full command output.
How can I install pyaudio and others. It is a bit difficult
Yes it is bit difficult, Kindly help us
You can install pyaudio by Unofficial binary of python download pyaudio same as python version then install by opening command prompt in the folder where it downloaded and write pip install then pyaudio file name
The best way to be successful in life is to follow the advice we give to others.
Yes it is bit difficult, Kindly help us
The way you write make it especially trouble-free to read. And the design you use, wow. Its a really good combination. And I am wondering what is the name of the theme you use?
I think youve created some actually interesting points. Not as well many people would basically think about this the way you just did. Im truly impressed that theres so very much about this topic thats been uncovered and you did it so nicely, with so a lot class. Superior one you, man! Actually excellent things here.