[英]Python loop for Speech to text
How to loop through below code so that out is generated constantly and displayed as output for a long speech through mic.如何循环通过下面的代码,以便不断生成输出并显示为 output 以通过麦克风进行长时间语音。
# import library
import speech_recognition as sr
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
print("Text: " + r.recognize_google(audio_text))
#text1 = r.recognize_google(audio_text, language="te-IN")
#print('Converting audio transcripts into text ...')
print(text)
#print(text1)
except:
print("Sorry, I did not get that")
just add while True
before the code.只需在代码前添加
while True
即可。 It will loop until you stop the script.它将循环直到您停止脚本。
# import library
import speech_recognition as sr
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Reading Microphone as source
# listening the speech and store in audio_text variable
while True
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
print("Text: " + r.recognize_google(audio_text))
#text1 = r.recognize_google(audio_text, language="te-IN")
#print('Converting audio transcripts into text ...')
print(text)
#print(text1)
except:
print("Sorry, I did not get that")
If you want to specify the repeat count, you can try this如果你想指定重复次数,你可以试试这个
for repeat in range(100):
with sr.Microphone() as source:
....
repeat += 1
Your script will stop after 100 try.您的脚本将在 100 次尝试后停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.