-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechtotext.py
36 lines (28 loc) · 1.29 KB
/
speechtotext.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
# Import the necessary module from the `speech_recognition` library.
import speech_recognition as sr
def speech_to_text():
"""
Convert speech to text using the microphone.
This function captures audio from the default system microphone and
uses the Google Web Speech API to transcribe it into text.
Returns:
- str: Transcribed text if successful, None otherwise.
"""
# Initialize the recognizer instance from the `speech_recognition` library.
r = sr.Recognizer()
# Use the system's default microphone as the audio source.
with sr.Microphone() as source:
# Adjust for ambient noise in the environment, which helps in improving accuracy of speech recognition.
r.adjust_for_ambient_noise(source)
# Listen to the audio from the microphone.
audio = r.listen(source)
try:
# Transcribe the audio to text using the Google Web Speech API.
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
# Handle the case where the speech is unintelligible.
print("Sorry, I couldn't recognize what you said.")
except sr.RequestError as e:
# Handle the case where the API request fails.
print(f"Could not request results; {e}")