-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
710 lines (284 loc) · 15.1 KB
/
main.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import tkinter as tk
from tkinter import *
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
import fnmatch,glob
import subprocess
import pyttsx3
import speech_recognition
import threading
import multiprocessing
from multiprocessing import Queue
from PIL import ImageTk,Image
import webbrowser
#import pygame
import pyscreenshot#need to install pillow before
import datetime
import playsound
from tkinter.filedialog import askopenfile, asksaveasfilename
from cv2 import *
import time
data_list=[ "Hi what's your name?",
'My name is Thursday',
'I am your personal assistant',
'You can ask me anything you want',
'I say no only if no is required',
'My favourite character is Motto Motto from Madagascar 2',
'I was named Thursday as I was born on a Thursday',
'I would eat meat if I could eat',
'I love to learn new stuff, would you like to teach me something',
'I am usually calm, depending on the question',
'Please dont ask me if god exists, I am tired of that question',
"I like lamborghini",
]
bot=ChatBot('Bot')
trainer=ListTrainer(bot)
#for files in os.listdir('data/english/'):
#data=open('data/english/'+files,'r',encoding='utf-8').readlines()
#trainer.train(data)
trainer.train(data_list)
def open_desktop_app(application):#hanging,used for getting the path of the specified file
for root,dirs,files in os.walk('/'):
for file in files:
if glob.fnmatch.fnmatch(file,application):
print(os.path.abspath(file))
path = os.path.abspath(os.path.join(root, file))
global answer
answer = path
#print(path)
#subprocess.Popen(path)
#os.system('brave')
#subprocess.check_call(path,'program')
#vpath = 'C:\\Windows\\System32\\notepad.exe'
#subprocess.run([sys.executable,vpath])
#return path
def first_response():#not working,wanted to say it as a greeting
first_reply='Hi i am Thursday....... I am your personal assistant, what can I do for your'
textarea.insert(END,'Bot: '+str(first_reply)+'\n\n')
pyttsx3.speak(first_reply)
#first_response()
def open_web(name):#opens the link in a web browser
try:
web = 'www.'+str(name)+'.com'
webbrowser.open(web)
global answer
answer = 'opening '+str(name)+'....please wait for a moment'
except Exception as e:
print(e)
answer = 'I am sorry i was unable to open '+str(name)
#trainer.train(data_list)
#engine = pyttsx3.init()
def date_and_time():
#gives the current date and time
now = datetime.datetime.now()
time_now_string = now.strftime("%#d %#B %#Y-%#I:%#M%p")
global answer
answer = "It is "+time_now_string
textarea.insert(END,'Bot: '+answer+'\n\n')
pyttsx3.speak(answer)
def date_today():
#gives the current date
today = datetime.date.today()
day_string = today.strftime("%d %B, %Y")
global answer
answer= "It is "+day_string
textarea.insert(END,'Bot: '+answer+'\n\n')
pyttsx3.speak(answer)
def screen_shot():
#takes the screenshot and saves the file as the current date
root.iconify()#minimizes the tk window
today = datetime.date.today()
now = datetime.datetime.now()
day_string = today.strftime("%d %B, %Y")
time_now_string = now.strftime("%#d %#B %#Y-%#I:%#M%p")
try:
image = pyscreenshot.grab()
image_name ='screenshot '+day_string+'.png'
time.sleep(5)
image.save(image_name)
global answer
answer = 'opening screenshot....please wait for a moment'
image.show(image_name)
except Exception as e:
answer = 'I was not able to save the image due to '+str(e)
def take_pic():
#takes a pic and then helps us to save it in a desired manner
global answer
answer = 'taking your picture smile please....'
textarea.insert(END,'Bot: '+answer+'\n\n')
pyttsx3.speak(answer)
cam = VideoCapture(0,cv2.CAP_DSHOW) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test")
imshow("cam-test",img)
waitKey(0)
#time.sleep(5)
destroyWindow("cam-test")
try:
answer = 'saving picture of yours ....hang on for a moment'
filename = asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("png file","*.png"),))
if filename:
name = filename.split('.')
name1= name[0]+'.png'
print(name1)
#name = name+'.png'
imwrite(name1,img) #save image
imshow(name1,img)
waitKey(0)
#subprocess
destroyWindow('img')
except AttributeError:
answer = 'file not saved..... please select a where to save the file'
song_path = ''
def play_song():
#plays the choosen song from the desired folder,
global song_path
global answer
answer = 'opening your favourite song....select the file please'
textarea.insert(END,'Bot: '+answer+'\n\n')
pyttsx3.speak(answer)
try:
song_path = askopenfile(filetypes = [("mp3 files","*.mp3")])
print(song_path)
#if song_path:
song_path = song_path.name
song = song_path.split('.')
song = song[0]+'.mp3'
song = str(song)
playsound.playsound(song)
except (AttributeError, playsound.PlaysoundException):
answer = "there was some problem i couldn't load the song"
#answer = 'Hi my name is Thursday, I am your personal assistant'
def botReply():
#gives the response from the personal assistant
question=questionField.get()
question=question.capitalize()
response=bot.get_response(question)
textarea.insert(END,'You: '+question+'\n\n')
#textarea.insert(END,'Bot: '+str(response)+'\n\n')
#pyttsx3.speak(response)
opener = question.split(' ')
#print(opener)
try:
answer =''#'Hi I am Thursday, your personal assistant.What can i do for you??'(not working)
if len(opener)>1:
#if opener[0]=='open' or opener[0]=='Open':
if 'open' or 'Open' in opener:
if opener[1]=='application':
#if we give a command like "open application notepad", it opens up notepad.like that many desktop apps
cmd = opener[-1].capitalize()#+'.exe'
#open_desktop_app(cmd)
os.system(cmd)
answer = 'opening the application '+str(cmd)
#opens spotify,notepad,
#if 'open' or "Open" and 'website' in opener:
elif opener[1]=='website':
#Command="open website google,amazon,"etc. calls the function to open websites
web = opener[-1]
open_web(web)
if opener[0]=='take' or opener[0]=='Take':
if opener[-1]=="screenshot" or opener[-1]=='Screenshot':
#Command="Take screenshot" or "take screenshot".It is used for calling the function to take screenshot
screen_shot()
#if 'take' or 'Take' and 'pic' or 'picture' in opener:
if opener[-1]=='pic' or opener[-1]=='picture':
#Command="take pic" or "take picture".It is used for calling the function to take picture from the webcam
take_pic()
#elif 'play' or 'song' or 'Play' in opener:
if opener[0]=='play' or opener[0]=='Play' and opener[-1]=='song':
#calls the function to play the song.Command="play song"
play_song()
if opener[0]=='date' or opener[0]=='Date':
if opener[-1]=='today':
#Command="date today or like date of today"
date_today()
elif opener[-1]=='time':
#command="date and time "
date_and_time()
if opener[0]=='give' or opener[0]=='Give' and opener[1]=='path':
application = opener[-1]
#open_desktop_app(application) #hangs alot
textarea.insert(END,'Bot: '+ answer +'\n\n')
pyttsx3.speak(answer)
except (IndexError,IOError,ValueError,TypeError,Exception):
print('an error has occured')
answer = 'an error has occured now,please be patient'
#answer = 'Opening '+str(opener[-1])+ '.........please wait'
answer = ""#"Carry on don't you have anything else to ask me."
#textarea.insert(END,'Bot: '+ answer +'\n\n')
#pyttsx3.speak(answer)
#except Exception as e:
# print(e)
textarea.insert(END,'Bot: '+str(response)+'\n\n')
pyttsx3.speak(response)
questionField.delete(0,END)
def audio_text():
while True:
speech = speech_recognition.Recognizer()
try:
with speech_recognition.Microphone() as m:
speech.adjust_for_ambient_noise(m,duration=0.2)
audio = speech.listen(m)
query = speech.recognize_google(audio)
#query = query.capitalize()
questionField.delete(0,END)
questionField.insert(0,query)
botReply()
except Exception as e:
print(e)
############################################################### Tkinter Part ###################################################
root=tk.Tk()
root.geometry('500x700')
root.title(' Thursday- Your Personal Assistant')
root.config(bg='indigo')
background_image = PhotoImage(file='wallpaper screenshot red.png')#add the desired image over here wallpaper screenshot3
background_label = Label(root, image = background_image,border=0, height = 1500, width = 1500, padx = 10, pady = 10)
background_label.place(x=0, y=0, relwidth=1,bordermode = INSIDE, relheight = 1, anchor = NW)
pic_logo=PhotoImage(file='logo screenshot1.png')#logo5
pic_logo1 = pic_logo.subsample(5,5)
label_piclogo=Label(root,image=pic_logo1,border=0)
label_piclogo.pack(pady=10)
centerFrame=Frame(root)
centerFrame.pack()
scrollbar=Scrollbar(centerFrame)
scrollbar.pack(side=RIGHT)
#text_image = Image.open('blue.png')
#text_bg_image = ImageTk.PhotoImage(text_image)
textarea=Text(centerFrame,font=('times new roman',20,'bold'),bg='grey',height=10, yscrollcommand=scrollbar.set,wrap='word')
#text_bg = textarea.image_create(text_bg_image)
textarea.pack(side=LEFT)
scrollbar.config(command=textarea.yview)
questionField=Entry(root,font=('verdana',20,'bold'),bg='grey')
questionField.pack(pady=15)
#bottomframe = Frame(root,bg='green')
#bottomframe.pack()
ask_image=PhotoImage(file='circle-cropped.png')
ask_image1 = ask_image.subsample(1,1)
ask_button=Button(root,image=ask_image1,border=0,activebackground='grey',command=botReply,relief='groove')
ask_button.pack(padx=5,pady=2)
#voicepic = PhotoImage(file='mic.png')
#voicepic1 = voicepic.subsample(3,3)
#voice = Button(bottomframe,image=voicepic1,activebackground='black',activeforeground='red',relief='sunken')
#voice.pack(padx=5,pady=2,side=tk.LEFT)
#voice.bind('<Button-1>',audio_text)
def click(event):
ask_button.invoke()
root.bind('<Return>',click)
thread = threading.Thread(target = audio_text)
thread.setDaemon(True)
thread.start()
root.mainloop()
#if __name__ == '__main__':
# multiprocessing.freeze_support()
#process = multiprocessing.Process(target =audio_text)
#process.daemon(True)
#process.start()
#root.mainloop()
#if __name__ == '__main__':
# q = Queue()
# Create a thread and run GUI &
# t1 = multiprocessing.Process(target=audio_text)
# t1.start()
#t2 = multiprocessing.Process(target=,args=(q,))