【问题标题】:How to get timed input in python?如何在python中获得定时输入?
【发布时间】: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


    【解决方案1】:

    您的错误是如何将函数 dist 传递给 threading.Timer: Timer 类采用一个可调用的函数,而您发送 None(disp() 的结果)。

    试试这个:

            timer = threading.Timer(2.0, disp, args=(text,))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2020-05-18
      • 2011-08-25
      • 2015-07-25
      • 2014-04-12
      • 2021-02-04
      相关资源
      最近更新 更多