【问题标题】:How to trigger a def only once and then stopped being able to call again for a amount of time?如何只触发一次def,然后在一段时间内停止再次调用?
【发布时间】:2022-01-05 20:15:20
【问题描述】:

所以我这里有这段代码,可以将声音从转换为文本来制作虚拟助手。但它会在几秒钟内返回相同的检测到的文本几次。哪个触发 def 我有几次,我只想触发一次。 这是代码:

            while True:
            data = q.get()
            if rec.AcceptWaveform(data):
                dicts = rec.Result()
                
            else:
                dicts = rec.PartialResult()

                print(dicts)
                
                main(dicts)

            if dump_fn is not None:
                dump_fn.write(data)

这是定义:

def main(dicts):
new_dicts = ast.literal_eval(dicts)
text = new_dicts.get("partial")
if "what" in text:
    if "time" in text:
            time_get("time")
    elif "day" in text:
            time_get("day")

输出示例:

{
 "partial" : "what time"
}
Time: 13:07:58 pm
{
"partial" : "what time"
}
Time: 13:08:04 pm
{
"partial" : "what time is it"
}

你能告诉我我做错了什么或如何解决它。抱歉英语不好。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    我想问题是你的“while”循环开始一遍又一遍地运行。 我建议您删除它或添加退出条件:
    if ...:
    break

    【讨论】:

      【解决方案2】:

      我很难完全理解您的问题。由于英语可能不是您的第一语言(顺便说一句,它不是我的),您可能会通过谷歌从您的母语翻译您的问题获得更准确的答复。 无论如何,我相信您正在寻找的是时间模块。你可以小鬼

      import time
      
      
      import time
      
      last_time = time.time()
      
      while True:
              dt = time.time() - last_time # dt stands for delta time which stands for time passed.
              
              if dt < 5: #This says as long is dt is smaller than 5 (seconds) just continue the loop and do nothing.
                      continue
              else:
                      #PASTE YOUR CODE HERE
                      last_time = time.time()#this sets the last_time value to the current time.
      

      我希望这会有所帮助!我希望你的编码很棒!

      【讨论】:

      • 非常感谢。我想使用谷歌翻译,但我什至不知道如何用我的语言描述这个问题。
      • 是的,我知道问题所在!
      【解决方案3】:

      其实没有问题。vosk 库是这样工作的。您有部分结果、Result 和 FinalResult 变量。 vosk 会尝试预测每个单词,直到您说完,而不是尝试为您构建完整的句子检查以下代码并查看最终结果,而不是继续听您的声音。

      代码正在使用 rec.Result() ,从 json 转换为 dict,从中获取文本元素并检查唤醒词,这里是“奥斯卡”。

      import json
      import os
      import time
      from vosk import Model, KaldiRecognizer
      
      if not os.path.exists("model"):
          print(
              "Please download the model from https://github.com/alphacep/kaldi-android-demo/releases and unpack as 'model-en' in the current folder.")
          exit(1)
      
      import pyaudio
      
      p = pyaudio.PyAudio()
      stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
      stream.start_stream()
      
      model = Model("model")
      rec = KaldiRecognizer(model, 16000)
      stop_recognize = False
      while stop_recognize is not True:
          record = stream.read(2000, exception_on_overflow=False)
          if len(record) > 0:
              if rec.AcceptWaveform(record):
                  # print(rec.Result())
                  prediction = json.loads(rec.Result())
                  if prediction['text'] == "oscar":
                      print("wake word found")
                  #     run your commands here
                  else:
                      print ("No wake word found",prediction)
      
      
      print(prediction)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        相关资源
        最近更新 更多