-
Notifications
You must be signed in to change notification settings - Fork 0
/
Casey.py
456 lines (376 loc) · 16.2 KB
/
Casey.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import googletrans
import gtts
import playsound
import pyaudio
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import cv2
import webbrowser
import os
import smtplib
import requests
from bs4 import BeautifulSoup
import pyautogui
import numpy as np
import random
import math
import sys
from twilio.rest import Client
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QTimer, QTime, QDate, Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
from caseyUi import Ui_Casey
import screen_brightness_control as sbc
import wolframalpha
from googletrans import Translator
import pywhatkit
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
from datetime import date
wfapp = wolframalpha.Client("Your-ID")
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
chrome_path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Hello! Good morning! I am Casey. Please tell me how may I help you")
print("Hello! Good morning! I am Casey. Please tell me how may I help you")
elif hour >= 12 and hour < 18:
speak("Hello! Good afternoon! I am Casey. Please tell me how may I help you")
print("Hello! Good afternoon! I am Casey. Please tell me how may I help you")
else:
speak("Hello! Good evening! I am Casey. Please tell me how may I help you")
print("Hello! Good evening! I am Casey. Please tell me how may I help you")
def sendEmail(to, content):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('your-email-id', 'your-password')
server.sendmail('your-email-id', to, content)
server.quit()
except Exception:
speak("Could not send email")
pass
class MainThread(QThread):
def __init__(self):
super(MainThread,self).__init__()
def run(self):
self.runCasey()
def takeCommand(self):
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening")
r.pause_threshold = 1
r.phrase_threshold = 0.290
r.energy_threshold = 368
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
query= query.lower()
print(f"User said: {query}\n")
except Exception as e:
return "none"
return query
def runCasey(self):
wishMe()
while True:
self.query = self.takeCommand()
if 'wikipedia' in self.query:
speak('Searching in Wikipedia...')
print('Searching in Wikipedia...')
self.query = self.query.replace("wikipedia", "")
results = wikipedia.summary(self.query, sentences=2)
speak("According to Wikipedia")
print("According to Wikipedia")
speak(results)
print(results)
elif 'play song' in self.query or 'song' in self.query:
speak("Which song I should play")
song = self.takeCommand()
song_result = "playing ", song
speak(song_result)
print(song_result)
pywhatkit.playonyt(song)
elif 'screenshot' in self.query:
image = pyautogui.screenshot()
digits = [i for i in range(0, 10)]
imageName= ""
for i in range(6):
index = math.floor(random.random() * 10)
imageName += str(digits[index])
image.save("C:\\Users\\vijin\\Pictures\\Screenshots\\"+imageName+".png")
os.startfile("C:\\Users\\vijin\\Pictures\\Screenshots\\"+imageName+".png")
elif 'open youtube' in self.query:
webbrowser.open("youtube.com")
elif 'open google' in self.query:
webbrowser.open("google.com")
elif 'open stackoverflow' in self.query:
webbrowser.open("stackoverflow.com")
elif 'open facebook' in self.query:
webbrowser.open("facebook.com")
elif 'open instagram' in self.query:
webbrowser.open("instagram.com")
elif 'open wikipedia' in self.query:
webbrowser.open("wikipedia.com")
elif 'increase volume' in self.query:
pyautogui.press("volumeup")
speak("Volume increased")
print("Volume increased")
elif 'decrease volume' in self.query:
pyautogui.press("volumedown")
speak("Volume decreased")
print("Volume decreased")
elif 'close chrome' in self.query:
os.system("taskkill /f /im " + "chrome.exe")
speak("Closed Chrome Browser")
print("Closed Chrome")
elif 'take photo' in self.query or 'photo' in self.query:
cam = cv2.VideoCapture(0)
cv2.namedWindow("test")
speak("Press Space Bar to click photo and Escape button for closing the window")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k % 256 == 27:
speak("Closing Camera")
print("Escape hit, closing...")
break
elif k % 256 == 32:
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
speak("Taking Photo")
print("Taking photo")
print("{} written!".format(img_name))
speak("Took Photo")
print("Took Photo")
img_counter += 1
cam.release()
cv2.destroyAllWindows()
elif 'record video' in self.query or 'video' in self.query:
speak("The video will be recorded automatically, press the q button in keyboard to stop recording")
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*"MJPG"), 30, (640, 480))
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
elif 'mute' in self.query:
pyautogui.press("volumemute")
elif 'time' in self.query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
print(strTime)
elif 'date' in self.query:
strDate = str(datetime.date.today())
todayDate = "Today's date is " ,strDate
speak(todayDate)
print(todayDate)
elif 'open visual studio' in self.query:
speak("Opening VS Code")
print("Opening VS code")
codePath = "C:\\Users\\vijin\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)
elif 'open word' in self.query:
speak("Opening Microsoft Word")
print("Opening Microsoft Word")
wordPath = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Office\\Microsoft Office Word 2007"
os.startfile(wordPath)
elif 'open excel' in self.query:
speak("Opening Microsoft Excel")
print("Opening Microsoft Excel")
excelPath = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Office\\Microsoft Office Excel 2007"
os.startfile(excelPath)
elif 'open media player' in self.query:
speak("Opening VLC Media Player")
print("Opening VLC Media Player")
vlcPath= "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\VideoLAN\\VLC media player"
os.startfile(vlcPath)
elif 'email' in self.query:
contacts = {"vijin": "hai@gmail.com", "joker": "joker@gmail.com"}
print("To whom should I send the email")
speak("To whom should I send the email")
to = self.takeCommand()
to = to.replace(" ", "")
print(to)
x = 0
for key, value in contacts.items():
if key == to:
to = value
print("What Message should I send")
speak("What message should I send")
message = self.takeCommand()
sendEmail(to, message)
x = x + 1
if x > 0:
print("Email send successfully")
else:
print("Couldn't find the contact")
elif 'increase brightness' in self.query:
current_brightness = sbc.get_brightness()
if current_brightness > 90:
speak("Brightness is at it's highest level. Can't increase brightness")
print("Brightness is at it's highest level. Can't increase brightness")
else:
new_brightness = current_brightness+10
print(new_brightness)
sbc.set_brightness(new_brightness)
updated_brightness = "Brightness increased to " , new_brightness
print(updated_brightness)
speak(updated_brightness)
elif 'decrease brightness' in self.query:
current_brightness = sbc.get_brightness()
if current_brightness < 10:
speak("Brightness is at its lowest level. Can't decrease brightness")
print("Brightness is at its lowest level. Can't decrease brightness")
else:
new_brightness = current_brightness-10
print(new_brightness)
sbc.set_brightness(new_brightness)
updated_brightness = "Brightness decreased to " , new_brightness
print(updated_brightness)
speak(updated_brightness)
elif 'shut down' in self.query:
speak("shutting down")
print("Shutting down")
os.system('shutdown -s -t 0')
elif 'sleep' in self.query:
speak("Hibernating system")
print("Hibernating system")
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
elif 'weather' in self.query or 'temperature' in self.query:
speak("which city's temperature do you want to look")
city = self.takeCommand()
q = "Temperature of ", city
res = wfapp.query(q)
print(next(res.results).text)
speak(next(res.results).text)
elif 'mathematics' in self.query or 'calculation' in self.query:
speak("Tell me the problem")
question = self.takeCommand()
res = wfapp.query(question)
print(next(res.results).text)
speak(next(res.results).text)
elif 'translate' in self.query:
languages = {"hindi": "hi", "english": "en", "malayalam": "ml", "tamil": "ta"}
dest_lang = ""
x = 0
try:
speak("In which language do you want me to translate")
lang = self.takeCommand()
for key, value in languages.items():
if key == lang:
x = x + 1
dest_lang = value
break
if x > 0:
speak("What do you want me to translate")
text = self.takeCommand()
translator = googletrans.Translator()
result = translator.translate(text, dest_lang)
print(result.text)
translated_audio = gtts.gTTS(result.text, lang=dest_lang)
translated_audio.save('audio.mp3')
playsound.playsound('audio.mp3')
else:
speak("Couldn't find language")
except Exception:
speak("Couldn't translate")
elif 'internet speed' in self.query:
import speedtest
st = speedtest.Speedtest()
dl = st.download()
up = st.upload()
dl_speed = "download speed=", dl, " bits per second"
up_speed = "upload speed=", up, " bits per second"
print(dl_speed)
print(up_speed)
speak(dl_speed)
speak(up_speed)
elif 'joke' in self.query:
import pyjokes
jokes = pyjokes.get_jokes(language="en", category="all")
r1 = random.randint(0, 100)
joke = jokes[r1]
speak(joke)
print(joke, sep="\n")
elif 'send message' in self.query:
account_sid = 'your sid'
auth_token = 'your auth_token'
client = Client(account_sid, auth_token)
speak("What do you want me to send?")
print("What do you want me to send?")
body = self.takeCommand()
message = client.messages \
.create(
body = body,
from_='from number',
to ='to number'
)
print(message.sid)
speak("Message sent")
print("Message sent")
elif 'news' in self.query or 'headlines' in self.query:
news_url = "https://news.google.com/news/rss"
Client = urlopen(news_url)
xml_page = Client.read()
Client.close()
soup_page = soup(xml_page, "xml")
news_list = soup_page.findAll("item")
for i in range(1, 10):
print(news_list[i].title.text)
speak(news_list[i].title.text)
print(news_list[i].pubDate.text)
startExecution = MainThread()
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_Casey()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startTask)
self.ui.pushButton_2.clicked.connect(self.close)
def startTask(self):
self.ui.movie = QtGui.QMovie("background.gif")
self.ui.label.setMovie(self.ui.movie)
self.ui.movie.start()
timer = QTimer(self)
timer.timeout.connect(self.showTime)
timer.start(1000)
startExecution.start()
def showTime(self):
current_time = QTime.currentTime()
current_date = QDate.currentDate()
label_time = current_time.toString('hh:mm:ss')
label_date = current_date.toString(Qt.ISODate)
self.ui.textBrowser_4.setText(label_date)
self.ui.textBrowser.setText(label_time)
app = QApplication(sys.argv)
casey = Main()
casey.show()
sys.exit(app.exec_())