我正在一个项目中,我想将语音转换为文本。所以我为此使用了SpeechRecogniser。
语音识别器在检测到暂停后停止运行,但是我不希望这种情况发生。我希望用户按'q'或'Q'停止语音转文本。
这是我尝试过的,
import speech_recognition as sr
import threading
r = sr.Recognizer()
def disp(text):
print(text)
with sr.Microphone() as source:
transcript = open('transcript.txt', 'w')
print('Start speaking')
while(True):
audio = r.listen(source)
try:
text = r.recognize_google(audio)
transcript.writelines(text)
except:
print('Inaudible. Try again.')
timer = threading.Timer(2.0, disp(text))
timer.start()
q = input()
try:
if q == 'q' or q == 'Q':
print('Ending transcript')
timer.cancel()
break
except NoneType:
continue
如果用户选择在他们停止讲话后2秒钟内退出,则只有我希望它停止该过程。
我得到的错误,
Start speaking
hello this is path
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
q
Ending transcript
提前感谢。
您的错误是如何将函数dist传递给threading.Timer:计时器类采用可调用的函数,并且您发送None(disp()的结果)。
尝试一下:
timer = threading.Timer(2.0, disp, args=(text,))