-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
439 lines (385 loc) · 14.7 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
# importing neccessary python libraries used for system.
from win32com.client import Dispatch
import pyttsx3
import datetime
from bs4 import BeautifulSoup
import speech_recognition as sr
import pyaudio
import numpy as np
import wikipedia
import requests
import webbrowser
import sports
import os
import pywhatkit
import smtplib
import cv2
import wolframalpha
import sys
import winsound
import pyautogui
from email.mime.text import MIMEText
from pycricbuzz import Cricbuzz
import speedtest
import random
# setting up the voices and engine to accept commands for system from user.
engine=pyttsx3.init("sapi5")
voices=engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
engine.setProperty("rate",130)
def speak(text): # speak function/module of system..help in converting text to voice.
engine.say(text)
engine.runAndWait()
def acceptcommands(): # module for accepting voice input from user and recognizes with google_recognize. and converting into text string.
c = sr.Recognizer()
with sr.Microphone() as source:
speak("Listening---")
c.pause_threshold=0.5
c.energy_threshold= 100
audio=c.listen(source)
try:
print("recognizing")
query=c.recognize_google(audio,language="en-in")
speak(query)
print(f"user said: {query}")
except Exception as error:
# speak("can't recognize..please speak again...")
return "NONE"
return query
contacts={"xyz":["+9191xxxxxxxx","user_mail@gmail.com"]} #sample contact list for sending message,emails.
def accept_wake_commands(wake): # capture the wake up command from user.
c = sr.Recognizer()
with sr.Microphone() as source:
speak("I am at sleep mode sir")
c.pause_threshold=0.5
c.energy_threshold= 100
audio=c.listen(source)
try:
print("sleeping")
query=c.recognize_google(audio,language="en-in")
speak(query)
print(f"user said: {query}")
except Exception as error:
pass
return "NONE"
return query
access=False
def face_lock(): # security module ...unlock the system by recognizes the correct user face id only.
count=2
base = cv2.imread("opencv_frame_base.png") # original user face id...(unlock system by comparing new faces from this base image).
try:
hsv_base = cv2.cvtColor(base, cv2.COLOR_BGR2HSV)
except:
count=1
speak("Welcome sir")
speak("I am your voice assistant")
speak("give me your face id for security")
if(count==2):
speak("Show your face for access")
cam = cv2.VideoCapture(0)
cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
speak("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
speak("Escape hit for closing...")
break
elif k%256 == 32:
# SPACE pressed
img_name = "opencv_frame_{}.png".format(img_counter) # accept present user face id..
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
else:
c=0
while(c<2):
cam = cv2.VideoCapture(0)
cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
speak("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k%256 ==27:
# ESC pressed
speak("Escape hit for closing...")
break
elif k%256 == 32:
# SPACE pressed
if(c==0):
img_name = "opencv_frame_base.png".format(img_counter) # accept base face id..in case it's not present in directory.
cv2.imwrite(img_name, frame)
c+=1
else:
img_name = "opencv_frame_{}.png".format(img_counter) # accept present user face id..
cv2.imwrite(img_name, frame)
img_counter += 1
c+=1
# cam.release()
cv2.destroyAllWindows()
# function for comapring new face id and base user face id.
base = cv2.imread("opencv_frame_base.png")
test = cv2.imread("opencv_frame_0.png")
hsv_base = cv2.cvtColor(base, cv2.COLOR_BGR2HSV)
hsv_test = cv2.cvtColor(test, cv2.COLOR_BGR2HSV)
h_bins = 50
s_bins = 60
histSize = [h_bins, s_bins]
h_ranges = [0, 180]
s_ranges = [0, 256]
ranges = h_ranges + s_ranges
channels = [0, 1]
hist_base = cv2.calcHist([hsv_base], channels, None, histSize, ranges, accumulate=False)
cv2.normalize(hist_base, hist_base, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
hist_test = cv2.calcHist([hsv_test], channels, None, histSize, ranges, accumulate=False)
cv2.normalize(hist_test, hist_test, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
compare_method = cv2.HISTCMP_CORREL
base_test = cv2.compareHist(hist_base, hist_test, compare_method)
if(base_test>=0.8): # unlock only when >=80% of images are same.
access= True
speak("Access granted")
wish()
Task_execution() # task execution will start from this function.
else:
speak("Access denied")
cv2.waitKey(0)
def temp(query): # temperature module..tells the temperature of place present in query(user voice input).
url= f"https://www.google.com/search?q={query}"
try:
r=requests.get(url)
data=BeautifulSoup(r.text,"html.parser")
temp=data.find("div",class_="BNeawe").text
speak(temp)
except:
speak("There is some error..please speak again")
def wolfram(query): # for maths and science
api_id ="TT4H6R-U4KUUXQUXK"
requester=wolframalpha.Client(api_id)
requested=requester.query(query)
try:
ans=next(requested.results).text
return ans
except:
Speak("Sorry, sir not got any answer")
def calculation(query): # for maths calculations.
query=str(query)
query=query.replace("jarvis","")
query=query.replace("plus","+")
query=query.replace("multiply","*")
query=query.replace("into","+")
query=query.replace("minus","-")
query=query.replace("divide","/")
query=str(query)
try:
result=wolfram(query)
speak(f"answer is {result}")
except:
speak("Sorry sir, did not got the answer")
def My_location(): # to got user location.
ip_ad=requests.get('https://api.ipify.org').text
url= 'https://get.geojs.io/v1/ip/geo/'+ip_ad+".json" # revel user location.
r=requests.get(url)
r=r.json()
city=r['city']
country=r["country"]
speak(f"Sir,you are in {city,country}")
def wish(): # gretting module...wish user according to time.
hour=int(datetime.datetime.now().hour)
if(hour>=0 and hour<=12):
speak("Good morning Sir")
elif(hour >=12 and hour<=16):
speak("Good afternoon Sir")
elif(hour>=16 and hour<=19):
speak("Good evening Sir")
else:
speak("Good night Sir")
speak("Myself jarvis!,Your personal voice assistant. How may i help you.")
def date(): # date function...show current date.
today=datetime.datetime.now()
format = '%I:%M %p'
date = today.strftime(format)
speak(date)
def whatsapp_message(): # communication module...help in sending whatsapp message to person mention in query.
speak("Tell me the name of Person!.")
name=acceptcommands().lower()
if(name in contacts):
speak("Tell me the message...")
message=acceptcommands()
phone_no=contacts[name][0]
try:
pywhatkit.sendwhatmsg_instantly(phone_no, message)
speak("message sent")
except:
speak("error in sending message..please try again")
else:
speak("Tell me the phone number")
phone_no=(acceptcommands())
phone_no="+91"+phone_no
speak("Tell me the message...")
message=acceptcommands()
try:
pywhatkit.sendwhatmsg_instantly(phone_no, message)
speak("message sent")
except:
speak("error in sending message")
def send_mail(): # communication module...help in sending mail to person mention in query.
server=smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
jarvis_mail_id="user_mail_id@gmail.com"
jarvis_id_password="id_password"
server.login(jarvis_mail_id,jarvis_id_password)
speak("Tell me the name of Person!.")
name=acceptcommands().lower()
if(name in contacts):
speak("Tell me the subject...")
subject=acceptcommands()
speak("Tell me the message...")
message=acceptcommands()
email_receiver=contacts[name][1]
try:
server.sendmail(jarvis_mail_id,[email_receiver],message)
speak("Mail sent to"+name)
except:
speak("Error in sending mail..try again")
server.close()
else:
speak("Tell me the message...")
message=acceptcommands()
speak("Tell me the recievers gmail id")
email_receiver=acceptcommands()+"@gmail.com"
try:
server.sendmail(jarvis_mail_id,[email_receiver],message)
speak("Mail sent")
except:
speak("Error in sending mail..try again")
server.close()
def speedtest(): #to get the user internet speed.
speak("Checking internet speed")
speed=speedtest.Speedtest()
upload=speed.upload()
download=speed.download()
upload=int(int(upload)/8000)
download=int(int(download)/8000)
speak(f"Sir,your downloading speed is{download} and uploading speed is{upload}")
def screen_shot(): # for clicking sceenshots
ss=pyautogui.screenshot()
ss.save("D:\\ss.png")
speak("Screenshot is taken sir")
def wikipedia_search(query): # web search module..help in scrapping or browse anything on wikipedia.
speak("searching wikipedia")
query=query.replace("wikipedia","")
try:
result= wikipedia.summary(query,sentences=2)
speak(result)
except:
speak("There is some error..please speak again sir.")
def google_search(query): # web search module..help in search anything on google.
speak("searching wikipedia")
query=query.replace("wikipedia","")
try:
result= wikipedia.summary(query,sentences=2)
speak(result)
except:
speak("There is some error..please speak again sir.")
query=query.replace("jarvis","")
query=query.replace("google search","")
try:
pywhatkit.search(query)
speak("Done Sir")
except:
speak("there is some error..please speak again..")
def open_site(query): # function help in opening any site.
speak("okay sir..launching")
query=query.replace("jarvis","")
query=query.replace("website","")
site=query.replace("open","").lower()
site=site.replace(" ","")
try:
open_site="https://www."+site+".com/"
webbrowser.open(open_site)
speak("Launched sir")
except:
speak("There is some error..please speak again sir.")
def chat_bot_module(): # chat bot function...user can chat according to inputs.
command1=["hello","wake up","hey","suno na","hi","utho","you there"]
reply1=["Hello sir,Welcome Back!","Always for you sir","How can i Help you"]
command2=["bye","go and sleep"]
reply2=["bye sir","nice meeting you"]
speak("okay sir..chat bot activated start chatting")
text=acceptcommands().lower()
try:
def chatterbot(text):
for word in text.split():
if word in command1:
speak(random.choice(reply1))
elif word in command2:
speak(random.choice(reply2))
else:
speak("hmm sir.")
except:
speak("Sir..please chat again")
def Task_execution(): # task execution scenario...accept the voice input as query and execute the task according to tht query.
wake =True
while(1):
if(wake==False): # did work only when jarvis is awake.
wake_up=accept_wake_commands(wake)
if(("wake" in wake_up or "wake up" in wake_up)):
speak("Yes sir..i am online now")
wake =True
if(wake==True):
query =acceptcommands().lower()
if("wikipedia" in query):
wikipedia_search(query)
elif("open google" in query):
webbrowser.open("www.google.com")
elif ("open youtube" in query):
webbrowser.open("www.youtube.com")
elif ("youtube search" in query):
query=query.replace("youtube search","")
query=query.replace("jarvis","")
c="https://www.youtube.com/results?search_query=" +query
webbrowser.open(c)
elif("temperature" in query):
query=query.replace("what is","")
temp=wolfram(query)
speak(temp)
elif("date and time" in query):
query=query.replace("what is","")
date()
elif("location" in query):
My_location()
elif("wish me" in query):
wish()
elif("google search" in query):
google_search(query)
elif("open website" in query or "website" in query or "open facebook" in query or "open instagram" in query):
open_site(query)
elif("send whatsapp message" in query):
whatsapp_message()
elif("send mail" in query):
send_mail()
elif("chat bot" in query or "chatting" in query or "chat" in query):
chat_bot_module()
elif("sleep" in query):
speak("sleeping mode on.")
wake=False
elif("internet speed" in query or "speed" in query):
speedtest()
elif("minus" in query or "add" in query or "calculation" in query or "plus" in query or "calculate" in query or "-" in query or "+" in query):
calculation(query)
else:
speak("sorry sir")
speak("i have no task assigned to this query")
speak("please speak again")
if __name__=="__main__": # main file will start from this..
face_lock()