-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice_assistant.py
99 lines (77 loc) · 2.89 KB
/
voice_assistant.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
import speech_recognition as sr
import os
import sys
import pyttsx3
import pyaudio
#import multiprocessing
import time
from playsound import playsound
from PyQt5.QtCore import QTimer
class VoiceAssistant:
def __init__(self):
self.eng = pyttsx3.init() #initialize an instance
self.voice = self.eng.getProperty('voices') #get the available voices
self.eng.setProperty('voice', self.voice[33].id) #0 basic male #7 reddit male #10 female #17 aussie
# 26 female (good) 33 female (siri)
self.defaultResponse = ['goodnight', 'good morning', 'yes']
self.eng.say("Welcome to Sleep Apnea Guardian. My name is Apnea!")
self.eng.runAndWait()
self.usrname = self.username()
self.eng.say("It's time for bed "+self.usrname)
self.eng.runAndWait()
query = ''
while self.defaultResponse[0] not in query:
query = self.takeCommand().lower()
if 'goodnight' in query:
self.eng.say("Sweet Dreams.")
self.eng.runAndWait()
self.eng.say("Sleep monitoring activated")
self.eng.runAndWait()
def takeCommand(self):
r = sr.Recognizer()
r.energy_threshold = 3000
with sr.Microphone() as source:
print("Listening...")
playsound('beep.m4a')
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"{query}\n")
except Exception as e:
print(e)
print("Unable to Recognize your voice.")
return ''
return query
def username(self):
self.eng.say("What's your name?")
usrname = self.takeCommand()
return usrname
def listen_for_morning(self):
query = ''
while self.defaultResponse[1] not in query:
query = self.takeCommand().lower()
if 'good morning' in query:
self.eng.say("Good Morning "+self.usrname + "!")
self.eng.runAndWait()
self.eng.say("I made a graph of your sleep last night, would you like to see it?")
self.eng.runAndWait()
query = self.takeCommand().lower()
if 'yes' in query:
self.eng.say('Here you go!')
self.eng.runAndWait()
return True
def username(self):
self.eng.say("What's your name?")
self.eng.runAndWait()
usrname = self.takeCommand()
return usrname
if __name__ == "__main__":
for i in range(48):
print(i)
eng = pyttsx3.init()
voice = eng.getProperty('voices') #get the available voices
eng.setProperty('voice', voice[i].id) #0 basic male #7 reddit male #10 female #17 aussie
eng.say("Welcome to Sleep Apnea Detector. My name is Apnea!")
eng.runAndWait()