-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoice_work.py
76 lines (62 loc) · 2.13 KB
/
voice_work.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
import pyttsx3
import datetime
import speech_recognition as sr
import platform
class voice:
def __init__(self) -> None:
if platform.system() == 'Linux':
self.engine = pyttsx3.init('nsss')
elif platform.system() == 'Windows':
self.engine = pyttsx3.init('sapi5')
else:
raise Exception("Unsupported platform")
voices = self.engine.getProperty('voices')
# print(voices[3].id)
self.engine.setProperty('voice', voices[1].id)
def speak(self,audio):
self.engine.say(audio)
self.engine.runAndWait()
def printandspeak(self,result):
print(result + "\n")
self.speak(result)
def wishMe(self):
hour = int(datetime.datetime.now().hour)
if 0 <= hour < 12:
self.speak("Good Morning!")
elif 12 <= hour < 18:
self.speak("Good Afternoon!")
else:
self.speak("Good Evening!")
self.speak("I am shawna Sir. Please tell me how may I help you")
def takeCommand(self):
# It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
command_user = r.recognize_google(audio, language='en-in')
print(f"User said: {command_user}\n")
return command_user
except Exception:
# print(e)
print("Say that again please...")
self.takeCommand()
return "None"
def take_command_start(self):
r = sr.Recognizer()
with sr.Microphone() as source:
print("...")
# r.pause_threshold = 1
audio = r.listen(source)
try:
# print("checking...")
command_user = r.recognize_google(audio, language='en-in')
# print(f"User said: {command_user}\n")
return command_user
except Exception:
# print(e)
self.take_command_start()
return "None"