How to make JARVIS in Python?

Hello and welcome in this post I am going to talk about how to make jarvis in python. I am going to provide you complete source code of this project. Also I am going to explain line by line chords and then the complete code will be given to you. There are no limitation of this voice assistant. All the voice commands are going to be run by this voice assistant need to be programmed by its programmer. So let’s get started and find out how to make JARVIS in python.

There are few things that are required before starting coding. Some of the Python packages need to be installed and these packages are mentioned below along with their installation command.

SpeechRecognition = pip install SpeechRecognition
pyttsx3 = pip install pyttsx3

Speech Recognition: – This module is required to capture speech or voice from users source. I mean this package will handle all the voice input needs with minimal coding.

pyttsx3: – Which package is required to convert voice into text. We need to show the user what he or she had spoke. So we will need to convert speech into text and pyttsx3 will help us to do this.

How to make JARVIS In Python: –
Now the main part comes in this section we will start coding our Jarvis. Let’s get started and look to step by step coding guide for making Jarvis. At the end of this post we will also give you complete source code of this project.

Importing Modules: – Let’s first get started by importing models add important some important packages below code will help us to do this.

import wikipedia

import os

from sys import exec_prefix

from urllib.parse import quote

import pyttsx3

import speech_recognition as sr

import datetime

import webbrowser

Setup pyttsx3 & Engine: –
Let’s first now set up or pyttsx and engines to to set up voice. With below code you can setup it.

engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices')

engine.setProperty('voice', voices[1].id)

engine.setProperty('rate', 150)

Above we are making a engine variable to make instance of pyttsx3 with sapi5. Let me tell you spai5 is Microsoft default speech API. Next we are setting property to engine to set up voice speed rate. voice[0] will be male voice and voice[1] will be female voice. (‘rate’,150) define how fast or slow it will speak.

Set up speak function: –
Now we are going to setup speak function so that our program can speak.

def speak(audio):

    engine.say(audio)

    engine.runAndWait()

Setup Speech Recognition: –
Now we have to set up speech recognition. So that our program can understand and recognise our voice. As you know that we have already imported speech recognition module we will use it to set up a source from where input voice will come.

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        r.adjust_for_ambient_noise(source)

        r.energy_threshold = 1000

        print("Listening...")

        audio = r.listen(source)

    try:

        print("Recognizing...")

        query = r.recognize_google(audio, language='en-in')

        print(f"You Said:{query}\n")

    except Exception as e:

        print("Say Again Please...")

        return "None"

    return query

Above we are creating function named takeCommand() and in it we are making a variable for the recognizer. We are set up in our microphone as source. Then we are setting up adjust_for_ambient_noise property so that it can work in loud rooms or noisy rooms also. Next we are listening from variable are which is instance of recognizer and saving it as audio.

Then we will need to add try expect block because there can be two things that can happen we might get error or we can successfully print what user has said below code will help us to do this.

try:

        print("Recognizing...")

        query = r.recognize_google(audio, language='en-in')

        print(f"You Said:{query}\n")

    except Exception as e:

        print("Say Again Please...")

        return "None"

    return query
Now we had done everything next step is to call our function in main function so that it can be executed whenever the script execute.

if __name__ == "__main__":



    while True:

        query = takeCommand().lower()

        if 'hello jarvis' in query:

            speak("Hello Sir")

        elif 'what is your name' in query:

            print("My Name is jarvis")

            speak("My Name is jarvis")

Here in main command we are making a variable query to take command and comparing query with specific word. Whenever our program find a matching word spoken by user and it is available in the command list it will get executed. Like as of above code if you speak “hello jarvis” is will speak and prrint “Hello Sir”. You can ad as many elif block you want and add new functionalities.

Complete Jarvis Source Code: –
Below is the complete source code of this project.
If you like my post please give some time and follow me on instagram

import wikipedia

import os

from sys import exec_prefix

from urllib.parse import quote

import pyttsx3

import speech_recognition as sr

engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices')

engine.setProperty('voice', voices[1].id)

engine.setProperty('rate', 150)

def speak(audio):

    engine.say(audio)

    engine.runAndWait()

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        r.adjust_for_ambient_noise(source)

        r.energy_threshold = 1000

        print("Listening...")

        audio = r.listen(source)

    try:

        print("Recognizing...")

        query = r.recognize_google(audio, language='en-in')

        print(f"You Said:{query}\n")

    except Exception as e:

        print("Say Again Please...")

        return "None"

    return query

if __name__ == "__main__":

    while True:

        query = takeCommand().lower()

        if 'hello jain' in query:

            speak("Hello Sir")

        elif 'what is your name' in query:

            print("My Name is jane")

            speak("My Name is jane")

13