import speech_recognition as sr
import os
import pyttsx3
import pyaudio
import pygame
speech = sr.Recognizer()
greeting_dict = {'hello':'hello','hi':'hi'}
try:
engine = pyttsx3.init()
except ImportError:
print("Requested driver is not found")
except RuntimeError:
print("Driver fails to initialize")
voices = engine.getProperty("voices")
engine.setProperty("voice","HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0")
rate = engine.getProperty("rate")
engine.setProperty("rate",rate)
def speak_text_cmd(cmd):
engine.say(cmd)
engine.runAndWait()
def read_voice_cmd():
voice_text = ''
print ("Listening...")
with sr.Microphone() as source:
audio = speech.listen(source=source,timeout=10,phrase_time_limit=5)
try:
voice_text = speech.recognize_google(audio)
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print("Network error.")
except sr.WaitTimeoutError:
pass
return voice_text
if __name__ == "__main__":
speak_text_cmd("Hello, Darrell")
#VoiceCommands:
while True:
voice_note = read_voice_cmd()
print("cmd : {}".format(voice_note))
if "hello Drago" in voice_note:
speak_text_cmd("Hello, Darrell.")
continue
elif "open" in voice_note:
speak_text_cmd("OK, Darrell ,on it.")
os.system('explorer C:\\ {}'.format(voice_note.replace('Open ','' )))
continue
elif "bye" in voice_note:
speak_text_cmd("Bye, Darrell.")
exit()