xxxxxxxxxx
result = speech_to_text()
print(f"Speech to text: {result}")
xxxxxxxxxx
def speech_to_text():
# Create a Recognizer instance
recognizer = sr.Recognizer()
# Capture audio from the microphone
with sr.Microphone() as source:
print("Speak something...")
audio = recognizer.listen(source)
try:
# Perform speech recognition
text = recognizer.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Sorry, I could not understand.")
except sr.RequestError as e:
print(f"Request error: {e}")