-
Notifications
You must be signed in to change notification settings - Fork 1
/
proton.py
253 lines (214 loc) · 7.47 KB
/
proton.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import pyttsx3
import speech_recognition as sr
from datetime import date
import time
import webbrowser
import datetime
from pynput.keyboard import Key, Controller
import pyautogui
import sys
import os
from os import listdir
from os.path import isfile, join
import smtplib
import wikipedia
# import Gesture_Controller
import AiVirtualMouseProject
#import Gesture_Controller_Gloved as Gesture_Controller
import app
from threading import Thread
# -------------Object Initialization---------------
today = date.today()
r = sr.Recognizer()
keyboard = Controller()
engine = pyttsx3.init('sapi5')
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
# ----------------Variables------------------------
file_exp_status = False
files =[]
path = ''
is_awake = True #Bot status
# ------------------Functions----------------------
def reply(audio):
app.ChatBot.addAppMsg(audio)
print(audio)
engine.say(audio)
engine.runAndWait()
def wish():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
reply("Good Morning!")
elif hour>=12 and hour<18:
reply("Good Afternoon!")
else:
reply("Good Evening!")
reply("I am Proton, how may I help you?")
# Set Microphone parameters
with sr.Microphone() as source:
r.energy_threshold = 500
r.dynamic_energy_threshold = False
# Audio to String
def record_audio():
with sr.Microphone() as source:
r.pause_threshold = 0.8
voice_data = ''
audio = r.listen(source, phrase_time_limit=5)
try:
voice_data = r.recognize_google(audio)
except sr.RequestError:
reply('Sorry my Service is down. Plz check your Internet connection')
except sr.UnknownValueError:
print('cant recognize')
pass
return voice_data.lower()
# Executes Commands (input: string)
def respond(voice_data):
global file_exp_status, files, is_awake, path
print(voice_data)
voice_data.replace('proton','')
app.eel.addUserMsg(voice_data)
if is_awake==False:
if 'wake up' in voice_data:
is_awake = True
wish()
# STATIC CONTROLS
elif 'hello' in voice_data:
wish()
elif 'what is your name' in voice_data:
reply('My name is Proton!')
elif 'date' in voice_data:
reply(today.strftime("%B %d, %Y"))
elif 'time' in voice_data:
reply(str(datetime.datetime.now()).split(" ")[1].split('.')[0])
elif 'search' in voice_data:
reply('Searching for ' + voice_data.split('search')[1])
url = 'https://google.com/search?q=' + voice_data.split('search')[1]
try:
webbrowser.get().open(url)
reply('This is what I found Sir')
except:
reply('Please check your Internet')
elif 'location' in voice_data:
reply('Which place are you looking for ?')
temp_audio = record_audio()
app.eel.addUserMsg(temp_audio)
reply('Locating...')
url = 'https://google.nl/maps/place/' + temp_audio + '/&'
try:
webbrowser.get().open(url)
reply('This is what I found Sir')
except:
reply('Please check your Internet')
elif ('bye' in voice_data) or ('by' in voice_data):
reply("Good bye Sir! Have a nice day.")
is_awake = False
elif ('exit' in voice_data) or ('terminate' in voice_data):
if AiVirtualMouseProject.gc_mode:
AiVirtualMouseProject.gc_mode = 0
app.ChatBot.close()
#sys.exit() always raises SystemExit, Handle it in main loop
sys.exit()
# DYNAMIC CONTROLS
elif 'launch app' in voice_data:
# if AiVirtualMouseProject.gc_mode:
# reply('Gesture recognition is already active')
# else:
# gc = AiVirtualMouseProject.Gesture_Controller()
# t = Thread(target = gc.start)
# t.start()
reply('Launched Successfully')
AiVirtualMouseProject.Gesture_Controller()
elif ('stop gesture recognition' in voice_data) or ('top gesture recognition' in voice_data):
if AiVirtualMouseProject.gc_mode:
AiVirtualMouseProject.gc_mode = 0
reply('Gesture recognition stopped')
else:
reply('Gesture recognition is already inactive')
elif 'copy' in voice_data:
with keyboard.pressed(Key.ctrl):
keyboard.press('c')
keyboard.release('c')
reply('Copied')
elif 'page' in voice_data or 'pest' in voice_data or 'paste' in voice_data:
with keyboard.pressed(Key.ctrl):
keyboard.press('v')
keyboard.release('v')
reply('Pasted')
# File Navigation (Default Folder set to C://)
elif 'list' in voice_data:
counter = 0
path = 'C://'
files = listdir(path)
filestr = ""
for f in files:
counter+=1
print(str(counter) + ': ' + f)
filestr += str(counter) + ': ' + f + '<br>'
file_exp_status = True
reply('These are the files in your root directory')
app.ChatBot.addAppMsg(filestr)
elif file_exp_status == True:
counter = 0
if 'open' in voice_data:
if isfile(join(path,files[int(voice_data.split(' ')[-1])-1])):
os.startfile(path + files[int(voice_data.split(' ')[-1])-1])
file_exp_status = False
else:
try:
path = path + files[int(voice_data.split(' ')[-1])-1] + '//'
files = listdir(path)
filestr = ""
for f in files:
counter+=1
filestr += str(counter) + ': ' + f + '<br>'
print(str(counter) + ': ' + f)
reply('Opened Successfully')
app.ChatBot.addAppMsg(filestr)
except:
reply('You do not have permission to access this folder')
if 'back' in voice_data:
filestr = ""
if path == 'C://':
reply('Sorry, this is the root directory')
else:
a = path.split('//')[:-2]
path = '//'.join(a)
path += '//'
files = listdir(path)
for f in files:
counter+=1
filestr += str(counter) + ': ' + f + '<br>'
print(str(counter) + ': ' + f)
reply('ok')
app.ChatBot.addAppMsg(filestr)
else:
reply('I am not functioned to do this !')
# ------------------Driver Code--------------------
t1 = Thread(target = app.ChatBot.start)
t1.start()
# Lock main thread until Chatbot has started
while not app.ChatBot.started:
time.sleep(0.5)
wish()
voice_data = None
while True:
if app.ChatBot.isUserInput():
#take input from GUI
voice_data = app.ChatBot.popUserInput()
else:
#take input from Voice
voice_data = record_audio()
#process voice_data
if 'proton' in voice_data:
try:
#Handle sys.exit()
respond(voice_data)
except SystemExit:
reply("Exit Successfull")
break
except:
#some other exception got raised
print("EXCEPTION raised while closing.")
break