hoe to install voice recognition for python code example
Example 1: python speech to text
import speech_recognition as sr
def main():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
print(r.recognize_google(audio))
except Exception as e:
print("Error : " + str(e))
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
if __name__ == "__main__":
main()
Example 2: how to fix speech recognition in python
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
print("You said " + r.recognize(audio))
except LookupError:
print("Could not understand audio")