-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_input.py
31 lines (28 loc) · 986 Bytes
/
user_input.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
import speech_recognition as sr
from random import choice
from speech_engine import speak
from utils import opening_text
from datetime import datetime
def take_user_input():
"""Takes user input, recognizes it using Recognition module and converts it into text"""
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening...')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language='en')
if not 'exit' in query or 'stop' in query:
speak(choice(opening_text))
else:
hour = datetime.now(hour)
if (hour >= 21) and (hour < 6):
speak("Good night sir, take care!")
else:
speak("Have a good day sir!")
exit()
except Exception:
speak("Sorry, I could not understand. Could you please say that again?")
query = 'None'
return query