forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice_recognition.py
26 lines (23 loc) · 923 Bytes
/
voice_recognition.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
import speech_recognition as sr
def recognize_speech(timeout=2, phrase_time_limit=5):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source, timeout=timeout, phrase_time_limit=phrase_time_limit)
try:
print("Recognizing...")
query = recognizer.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except sr.WaitTimeoutError:
print("Listening timed out while waiting for phrase to start.")
return "None"
except sr.UnknownValueError:
print("Could not understand audio")
return "None"
except sr.RequestError:
print("Could not request results; check your network connection")
return "None"
except Exception as e:
print(f"Error: {e}")
return "None"
return query.lower()