-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_recognition_tool.py
83 lines (70 loc) · 3.02 KB
/
voice_recognition_tool.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
import speech_recognition as sr
import re
import webbrowser
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def main():
# enter the name of usb microphone that you found
# using lsusb
# the following name is only used as an example
#mic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"
mic_name = "Microphone (Realtek High Defini"
# Sample rate is how often values are recorded
sample_rate = 48000
# Chunk is like a buffer. It stores 2048 samples (bytes of data)
# here.
# it is advisable to use powers of 2 such as 1024 or 2048
chunk_size = 2048
# Initialize the recognizer
r = sr.Recognizer()
# generate a list of all audio cards/microphones
mic_list = sr.Microphone.list_microphone_names()
# the following loop aims to set the device ID of the mic that
# we specifically want to use to avoid ambiguity.
for i, microphone_name in enumerate(mic_list):
if microphone_name == mic_name:
device_id = i
# use the microphone as source for input. Here, we also specify
# which device ID to specifically look for incase the microphone
# is not working, an error will pop up saying "device_id undefined"
with sr.Microphone(device_index=device_id, sample_rate=sample_rate,
chunk_size=chunk_size) as source:
# wait for a second to let the recognizer adjust the
# energy threshold based on the surrounding noise level
r.adjust_for_ambient_noise(source)
print("Say Something")
# listens for the user's input
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("you said: " + text)
if re.search(r'chrome|browser|google', text, re.IGNORECASE):
webbrowser.open('www.google.com', new=1)
elif re.search(r'youtube', text, re.IGNORECASE):
webbrowser.open('www.youtube.com', new=1)
elif re.search(r'mail', text, re.IGNORECASE):
print("Sending email")
mail_notification('test', subject='test', fromAddress='[email protected]', toAddress='[email protected]')
# error occurs when google could not understand what was said
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service;{0}".format(e))
def mail_notification(body, subject=None, fromAddress=None, toAddress=None):
msg = MIMEMultipart()
msg['From'] = fromAddress
msg['To'] = ','.join(toAddress)
msg['Subject'] = subject
msg.attach(MIMEText(body))
mailServer = None
try:
mailServer = SMTP('localhost')
mailServer.sendmail(fromAddress, toAddress, msg.as_string())
except Exception as e:
print(str(e))
finally:
if mailServer != None:
mailServer.quit()
if __name__ == '__main__':
main()