(to make program systematic we create it as functions)
import pyttsx3 (for converting text to speech)
import speech_recognition as sr (for convering speech to text)
import webbrowser (for searching something on internet)
import datetime (to get realtime date and time)
import pyjokes ( to listen jokes)
import os (
import time
def sptext(): ( for converting it from Speech to text)
recognizer(creatring object named reconginzer )=sr.Recognizer()(calling recognizer from module)
with sr.Microphone() as source(for using microphone we use “with” from the module sr):
print("Listening...") (showing to speak)
recognizer.adjust_for_ambient_noise(source)(to cut down noise from the sound)
audio = recognizer.listen(source) (to listen sound from the source)
try: (if not listen properly then stopping from throwing error we use this as exception handling )
print("recognizing...")
data = recognizer.recognize_google(audio) (we are using google recognizer to recognize audio
storing it in data named object)
print(data)
return data
except sr.UnknownValueError: (for printing error we use this)
print("Not Understanding")
sptext() (for converting text to speech)
def speechtx(x): (creating function for text to speech conversion)
engine = pyttsx3.init() (creating engine named variable to convert text to speech)
voices = engine.getProperty('voices') (getting the property from engine voice)
engine.setProperty('voice',voices[0].id) ( for selecting single voice we use ‘voice’)
rate = engine.getProperty('rate') (creating variable rate to set speed of voice)
engine.setProperty('rate',150) (for setting speed of voice to 150)
engine.say(x) (for getting date from engine to ‘x’)
engine.runAndWait()
if __name__ == '__main__': (for creating name of the voice assistant)
if "hey laxmi" in sptext().lower(): (if we say hey laxmi then it will start work)
while True: (if we get hey laxmi then we get data)
data1=sptext().lower() (for converting the text into lower case)
if "your name" in data1:
name = " my name is laxmi" (for saying the name of the assistant that we give it laxmi)
speechtx(name)
elif "old are you" in data1: (forgetting the age of the voice assistant)
age="i am two years old" (for printing the age of the above asked question)
speechtx(age)
elif 'time' in data1:
time = datetime.datetime.now().strftime("%I%M%p") (for getting time )
speechtx(time)
elif 'youtube' in data1:
webbrowser.open("https://www.youtube.com/") (for opening youtube)
elif 'web' in data1:
webbrowser.open("https://shivalikcollege.edu.in") (for opening shivalik college website)
elif 'flipkart' in data1:
webbrowser.open("https://flipkart.com") (for opening flipkart website)
elif 'amazon' in data1:
webbrowser.open("https://amazon.com") (for opening amazon website)
elif 'joke' in data1:
joke_1 = pyjokes.get_joke(language="en",category="neutral") (for listening joke)
speechtx(joke_1)
elif 'play song' in data1: (for playing music)
add = "C:\music" (music stored in the c drive of the computer)
listsong = os.listdir(add) (for adding list of the song)
print(listsong)
os.startfile(os.path.join(add,listsong[1])) (using os we can play the song from the system)
elif "exit" in data1: (for exit we say created exit command )
speechtx("thank you")
break
time.sleep(10)
#else:
# print("thanks") (if not getting hey laxmi then voice assistant will not work and print
thankyou)