如何在python中获得定时输入?
我正在做一个项目,我想把语音转换成文本。所以我用语音识别器来做这个 语音识别器在检测到暂停后停止工作,但我不希望发生这种情况。我希望用户按“q”或“q”停止语音转换为文本 这是我尝试过的如何在python中获得定时输入?,python,input,speech-recognition,speech-to-text,Python,Input,Speech Recognition,Speech To Text,我正在做一个项目,我想把语音转换成文本。所以我用语音识别器来做这个 语音识别器在检测到暂停后停止工作,但我不希望发生这种情况。我希望用户按“q”或“q”停止语音转换为文本 这是我尝试过的 import speech_recognition as sr import threading r = sr.Recognizer() def disp(text): print(text) with sr.Microphone() as source: transcript = op
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传递到线程。计时器: Timer类接受一个可调用的函数,但不发送任何函数(disp()的结果) 试试这个:
timer = threading.Timer(2.0, disp, args=(text,))