import speech_recognition as sr
import pyttsx3
# Initialize the recognizer
r = sr.Recognizer()
# using microphone to listen for audio input and if possible turn audio into
string
def record_text():
#loop in case of errors
while(1):
try:
# use microphone as a source of input
with sr.Microphone() as source2:
# Prepare the recognizer to receive input
r.adjust_for_ambient_noise(source2, duration=0.2)
# listens for the user's input
audio2 = r.listen(source2)
# using google to recognize audio
MyText = r.recognize_google(audio2)
# returning the data from MyText
return MyText
except sr.RequestError as e:
print("Could not request results: {0}".format(e))
except sr.UnknownValueError:
print("unknown error occurred")
# take string and output it to a text file to complete speech to text program
def output_text(text):
# allows to access the output.txt file and storing the access in the f
variable
f = open("output.txt", "a")
f.write(text)
f.write("/n")
f.close()
return
# calls the two programs (record_text) and (output_text) on repeat so that it
can run forever
while(1):
text = record_text()
output_text(text)
print("Wrote text")