Skip to content

Instantly share code, notes, and snippets.

@psavarmattas
Created January 28, 2021 05:47
Show Gist options
  • Save psavarmattas/ec3644db2457ece42887689bde48dd88 to your computer and use it in GitHub Desktop.
Save psavarmattas/ec3644db2457ece42887689bde48dd88 to your computer and use it in GitHub Desktop.
Text to Speech & Listener for Virtual Assistants in Python
import speech_recognition as sr
import pyttsx3
import datetime
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', 150)
def talk(text):
engine.say(text)
engine.runAndWait()
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
command = r.recognize_google(audio, language='en-in')
print(f"User said: {command}\n")
except Exception as e:
print("Say that again please...")
return "None"
return command
if __name__ == "__main__":
while True:
command = take_command().lower()
if 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
talk('Current time is ' + time)
elif 'stop' in command:
break
else:
talk('Please say the command again.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment