【发布时间】:2020-04-11 11:50:54
【问题描述】:
我正在做一个项目,我想将语音转换为文本。所以我为此使用 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
提前致谢。
【问题讨论】:
标签: python input speech-recognition speech-to-text