Python – Get Today’s Current Day using Speech Recognition
We often don’t remember the day of the date due to our load of work we are doing. So, here is a Python program with the help of which we can find the day of the date with just a simple chat with our laptop or mobile.
Modules Needed
- DateTime: This is a Library in Python with the help of which we can manipulate the date and time. It comes preinstalled with python so we don’t have to install.
- pyttsx3: This is text to speech conversion library. It helps in communicating with the user. This module does not come built-in with Python. To install it type the below command in the terminal.
pip install pyttsx3
- SpeechRecognition: It helps in Speech Recognition. Speech recognition is the process of converting audio into text. This module does not come built-in with Python. To install it type the below command in the terminal.
pip install SpeechRecognition
Now Let’s Code The program for telling date with the help of Speech Recognition.
Step 1: Make the method for taking the commands. So that our program can take our commands.
Python3
import datetime
import pyttsx3
import speech_recognition as sr
def take_commands():
r = sr.Recognizer()
with sr.Microphone() as source:
print ( 'Listening' )
r.pause_threshold = 0.7
audio = r.listen(source)
try :
print ( "Recognizing" )
Query = r.recognize_google(audio, language = 'en-in' )
print ( "the query is printed='" , Query, "'" )
except Exception as e:
print (e)
print ( "Say that again sir" )
return "None"
return Query
|
Step 2: Make the Speak method so that our Program can speak to us.
Python3
def Speak(audio):
engine = pyttsx3.init()
voices = engine.getProperty( 'voices' )
engine.setProperty( 'voice' , voices[ 1 ]. id )
engine.say(audio)
engine.runAndWait()
|
Step 3: Tell the day method
Python3
def tellDay():
day = datetime.datetime.today().weekday() + 1
Day_dict = { 1 : 'Monday' , 2 : 'Tuesday' , 3 : 'Wednesday' ,
4 : 'Thursday' , 5 : 'Friday' , 6 : 'Saturday' ,
7 : 'Sunday' }
if day in Day_dict.keys():
day_of_the_week = Day_dict[day]
print (day_of_the_week)
Speak( "The day is " + day_of_the_week)
|
Step 4: The main method which will help us to execute the program
Python3
if __name__ = = '__main__' :
command = take_commands()
if "day" in command:
tellDay()
|
Whole Program:
Python3
import datetime
import pyttsx3
import speech_recognition as sr
def take_commands():
r = sr.Recognizer()
with sr.Microphone() as source:
print ( 'Listening' )
r.pause_threshold = 0.7
audio = r.listen(source)
try :
print ( "Recognizing" )
Query = r.recognize_google(audio, language = 'en-in' )
print ( "the query is printed='" , Query, "'" )
except Exception as e:
print (e)
print ( "Say that again sir" )
return "None"
return Query
def Speak(audio):
engine = pyttsx3.init()
voices = engine.getProperty( 'voices' )
engine.setProperty( 'voice' , voices[ 1 ]. id )
engine.say(audio)
engine.runAndWait()
def tellDay():
day = datetime.datetime.today().weekday() + 1
Day_dict = { 1 : 'Monday' , 2 : 'Tuesday' , 3 : 'Wednesday' ,
4 : 'Thursday' , 5 : 'Friday' , 6 : 'Saturday' ,
7 : 'Sunday' }
if day in Day_dict.keys():
day_of_the_week = Day_dict[day]
print (day_of_the_week)
Speak( "The day is " + day_of_the_week)
if __name__ = = '__main__' :
command = take_commands()
if "day" in command:
tellDay()
|
Output:
Listening
Recognizing
the query is printed=' today's day '
Wednesday
Note: A voice generated output is also created.
Last Updated :
09 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...