-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (48 loc) · 2.18 KB
/
main.py
File metadata and controls
55 lines (48 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import speech_recognition as sr
import webbrowser
import pyttsx3
def speak(text):
ttsEngine = pyttsx3.init() # will convert text to speech
ttsEngine.setProperty("rate", 150)
ttsEngine.setProperty("volume", 1.0)
ttsEngine.say(text)
ttsEngine.runAndWait()
def processCommand(c):
ToDo = c.lower()
if "open google" in ToDo:
speak("Opening google")
webbrowser.open("https://google.com")
elif "open youtube" in ToDo:
speak("Opening youtube")
webbrowser.open("https://youtube.com")
elif "open instagram" in ToDo:
speak("Opening instagram")
webbrowser.open("https://instagram.com")
elif "open facebook" in ToDo:
speak("Opening facebook")
webbrowser.open("https://facebook.com")
if __name__ == "__main__":
speak("initializing Jarvis....")
while True:
recognizer = sr.Recognizer() # will recognize the speech
with sr.Microphone() as source:
print("Listening....")
try:
# recognizer.adjust_for_ambient_noise(source) # Adjust for background noise
audio = recognizer.listen(source, timeout=2, phrase_time_limit=1)
word = recognizer.recognize_google(audio)
if "jarvis" in word.lower() or "hey jarvis" in word.lower() or "hello jarvis" in word.lower():
speak("Yaa?")
print("Jarvis Active!!!!")
with sr.Microphone() as source:
# recognizer.adjust_for_ambient_noise(source)
doCommand = recognizer.listen(source)
cmdTextFormat = recognizer.recognize_google(doCommand)
processCommand(cmdTextFormat)
except sr.WaitTimeoutError:
print("Timeout: No speech detected.")
continue # Continue to the next iteration of the loop
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")