-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.py
33 lines (22 loc) · 984 Bytes
/
listen.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
import speech_recognition as sr
def listen_to_microphone():
# Initialize recognizer
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Adjusting for ambient noise, please wait...")
recognizer.adjust_for_ambient_noise(source)
print("Listening... Speak into the microphone")
# Listen for the first phrase and extract it into audio data
audio_data = recognizer.listen(source)
try:
# Use Google Web Speech API to recognize speech
print("Recognizing speech...")
text = recognizer.recognize_google(audio_data)
print(f"You said: {text}")
return text
except sr.UnknownValueError:
print("Sorry, I could not understand the audio.")
except sr.RequestError as e:
print(f"Error from Google Speech Recognition service: {e}")
if __name__ == '__main__':
listen_to_microphone()