0% found this document useful (0 votes)
4 views2 pages

Python Text To Spesdfssech

The document contains a Python script that uses the SpeechRecognition and pyttsx3 libraries to convert spoken audio into text. It continuously listens for audio input from the microphone, recognizes the speech using Google's API, and appends the recognized text to an 'output.txt' file. The script includes error handling for request errors and unknown speech recognition errors.

Uploaded by

gabetgronniger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Text To Spesdfssech

The document contains a Python script that uses the SpeechRecognition and pyttsx3 libraries to convert spoken audio into text. It continuously listens for audio input from the microphone, recognizes the speech using Google's API, and appends the recognized text to an 'output.txt' file. The script includes error handling for request errors and unknown speech recognition errors.

Uploaded by

gabetgronniger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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")

You might also like