Python voice to text script
I picked this up at Reddit today, but I added a twist to it. The script will not save the recognized speech to a file that user can designate. Hope you enjoy it! Thank for your for the author who shared the script and those who keep working on tghe project.
import speech_recognition as sr
def save_speech(file_name):
with open(file_name, "a") as f:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak something:")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
f.write(text + "\n")
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def main():
file_name = input("Enter a file name to save the speech: ")
save_speech(file_name)
if __name__ == "__main__":
main()
Comments
Post a Comment