forked from bhojaksmith/J.A.R.V.I.S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJARVIS.PY
239 lines (196 loc) · 7.33 KB
/
JARVIS.PY
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#for ubuntu install "sudo apt install libespeak1"
import os
import speech_recognition as sr
import pyttsx3
import platform
import threading
from pyttsx3 import drivers
import inspect
import time
# print([m[0] for m in inspect.getmembers(pyttsx3, inspect.isclass) if m[1].__module__ == 'pyttsx3'])
def SpeakText(command):
engine = pyttsx3.init() #
engine.say(command)
engine.runAndWait()
SpeakText("Speak")
SpeakText("hey jarvis")
SpeakText("to activate")
language = 'en-IN'
platf=platform.system()
text=''
engine = pyttsx3.init('dummy')
platf=platform.system()
timerMap = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
# While setting timer if speechrecognition picks up numbers as words
r = sr.Recognizer()
r.energy_threshold = 300
# (150-4000) Higher value if it tries to recognize when it shouldn’t, and a lower value if it doesn’t recognize when it should.
r.pause_threshold = 0.5
#Represents the minimum length of silence (in seconds) that will register as the end of a phrase.
#feature functions
def browser():
print("Opening web browser\n")
if(platf=="Linux"):
os.system("gio open https://www.google.com &")
else:
os.system("start https://www.google.com")
def calculator():
print("Opening calculator\n")
if(platf=="Linux"):
os.system("gnome-calculator &")
else:
os.system('calc')
def calendar():
print("Opening calendar\n")
if(platf=="Linux"):
os.system('gnome-calendar &')
else:
os.system("start outlookcal:")
def photos():
print("Opening photos\n")
if(platf=="Linux"):
print("Not Available !")
else:
os.system('start ms-photos:')
def screenshot():
print("Taking screenshot\n")
if(platf=="Linux"):
os.system("gnome-screenshot")
else:
os.system('start ms-screenclip:')
def mail():
print("Opening mail\n")
if(platf=="Linux"):
os.system("thunderbird")
else:
os.system('outlookmail:')
def search():
print('What do you want to search?....\n\n')
SpeakText('What do you want to search')
searchQuery = r.listen(source2)
# The timeout parameter is the maximum number of seconds
# that it will wait for a phrase to start before giving up
# and throwing a TimeoutException exception. If Value not specified, it will wait indefinitely.
print("Processing...")
text = r.recognize_google(searchQuery, language='en-IN')
text = text.lower()
SpeakText('Searching for '+text+' on the web')
#print("Searching for '"+text+"'")
if(platf=="Linux"):
os.system("gio open https://www.google.com/search?q="+text)
else:
os.system("start www.google.com/search?q="+text)
def shutdown():
print('PC shutting down....')
#playsound('caged_goodnight.mp3')
if(platf=="Linux"):
os.system("shutdown")
print("shutdown scheduled after 1 minute...")
else:
os.system("shutdown /s /t 2")
exit()
def timerX():
print("\n\nHow many minutes?\n\n")
SpeakText('How many minutes?')
searchQuery = r.listen(source2)
print("\n\nProcessing...\n\n")
text = r.recognize_google(searchQuery, language='en-IN')
text = text.lower()
print("\n\nDid you say '"+text+"'\n\n")
timerStarted = False
def timerExpired():
print("\n\nTimer Expired\n")
if not timerStarted:
if(text.isdigit()):
print('\n\nSetting a timer for '+ text +' Minutes\n\n')
SpeakText('Setting a timer for '+ text +' Minutes')
timer = threading.Timer(int(text)*60, timerExpired)
timer.start()
else:
try:
print('\n\nSetting a timer for '+ timerMap[text] +' Minutes\n\n')
SpeakText('Setting a timer for '+ timerMap[text] +' Minutes')
timer = threading.Timer(timerMap[text]*60, timerExpired)
timerStarted = True
timer.start()
except KeyError:
print('Please Try again')
SpeakText('Invalid Value , Please Try again')
else:
print('Say Cancel to cancel timer')
SpeakText('Say Cancel to cancel timer')
searchQuery = r.listen(source2)
print("Processing Cancel...")
text = r.recognize_google(searchQuery, language='en-IN')
text = text.lower()
if('cancel' in text):
print("Cancelling timer\n")
timer.cancel()
functionDict = {
'browser':browser,
'open browser':browser,
'open web browser':browser,
'calculator':calculator,
'open calculator':calculator,
'calendar':calendar,
'open calendar':calendar,
'photos':photos,
'open photos':photos,
'good night':shutdown,
'shutdown':shutdown,
'search':search,
'search for':search,
'screenshot':screenshot,
'take a screenshot':screenshot,
'set a timer':timerX,
'set timer':timerX,
'timer':timerX,
'cancel timer':timerX
}
activ=False
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=2)
while(1):
try:
if not activ :
# print("kem palty")
print("Say \"Hey Jarvis\" to speak to Jarvis... ")
audio2 = r.listen(source2)
print("Processing...\n")
text = r.recognize_google(audio2, language='en-IN')
text = text.lower()
print("you said '"+text+"'")
if 'hey jarvis' in text or 'jarvis' in text:
print("Listening now!\n")
#SpeakText("Listening for your command ...")
activ=True
if activ:
print("What do you want to do? ... ")
SpeakText("What do you want to do\n")
time.sleep(3)
print("\nThings you can say \n Open Calculator \n Open web browser \n Shutdown OR Good night \n Set a timer")
print(" Open Calendar \n Take a screenshot \n Search \n Open Photos")
print("see help.txt for all commands")
# try:
query = r.listen(source2)
# except Exception as e:
# print(e)
# query = None
# print(type(query))
if(query!=None):
print("\nProcessing...")
text = r.recognize_google(query, language='en-IN')
print('\nProcessed!\n')
text = text.lower()
#calling function dynamically
try:
functionDict[text]()
except KeyError as e:
print("Did you say '"+text+"'\n\n")
print("Sorry, we dont do that !")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("Couldn't Understand please try again !")
SpeakText('Could not Understand please try again')
print("=======================================================================================")