-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechRec.py
31 lines (27 loc) · 1.09 KB
/
speechRec.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
def recognize_speech():
# Initialize the recognizer
recognizer = sr.Recognizer()
# Use the default microphone as the audio source
with sr.Microphone() as source:
print("Say something:")
recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
audio = recognizer.listen(source, timeout=5) # Listen for up to 5 seconds
try:
print("Recognizing...")
# Recognize speech using Google Web Speech API
text = recognizer.recognize_google(audio)
print("You said: {}".format(text))
return text
except sr.UnknownValueError:
print("Could not understand audio.")
return None
except sr.RequestError as e:
print("Error with the speech recognition service; {0}".format(e))
return None
if __name__ == "__main__":
print("Program is running")
recognized_text = recognize_speech()
if recognized_text:
# Now you can use the recognized text as needed in your program
print("Processed Text:", recognized_text)