溢出 - reader PyAudio 的阅读速度不够快

Overflow - reader is not reading fast enough PyAudio

当我 运行 遇到问题时,我正在尝试使用 Python 构建语音助手。 我正在使用 porcupine/picovoice 进行唤醒词检测,然后我调用一个函数,它会在我调用它时立即识别我所说的一切。

这是函数:

def recognizevoicecommand():
    try:
        r = sr.Recognizer()
        print("A moment of silence, please...")
        with sr.Microphone() as source:
            time.sleep(2)
            r.adjust_for_ambient_noise(source)
        print("Set minimum energy threshold to {}".format(r.energy_threshold))
        while True:
            print("Say something!")
            with m as source:
                time.sleep(2)
                print("Now!")
                audio = r.listen(source)
            print("Got it! Now to recognize it...")
            try:
    # recognize speech using Google Speech Recognition
                value = r.recognize_google(audio)
                print(value)
                return value
                break
            except sr.UnknownValueError():
                print("OOps")
                break
    except KeyboardInterrupt:
        pass

如果我只是单独调用该函数,它会工作得很好,识别我说的话然后打印出来。

但问题是,一旦我将它与唤醒词检测器一起使用,我就会收到垃圾邮件 溢出 - reader 读取速度不够快请大家默哀... 被打印出来了。

这是唤醒词检测代码,它自己和脚本中都可以正常工作,问题似乎出在识别部分

    porcupine = pvporcupine.create(access_key = accesskey, keywords=['computer'])
    recorder = PvRecorder(device_index = 0, frame_length = porcupine.frame_length)
    recorder.start()

    while True:
        pcm = recorder.read()
         
        result = porcupine.process(pcm)
        if(result >= 0):
            print("Keyword detected")
            print(recognizevoicecommand())
except pvporcupine.PorcupineInvalidArgumentError as e:
    print("One or more arguments provided to Procupine is invalid!")
    raise e
except pvporcupine.PorcupineActivationError as e:
    print("Accesskey denied.")
    raise e
except pvporcupine.PorcupineActivationLimitError as e:
    print("Accesskey has reached it's temporary device limit.")
    raise e
except pvporcupine.PorcupineActivationRefusedError as e:
    print("Accesskey refused.")
    raise e
except pvporcupine.PorcupineActivationThrottledError as e:
    print("Accesskey has been throttled.")
    raise e
except pvporcupine.PorcupineError as e:
    print("Failed to initialize Porcupine.")
    raise e
except KeyboardInterrupt:
    print("Stopping")
finally:
    if porcupine is not None:
        porcupine.delete()
    if recorder is not None:
        recorder.delete()

老实说,我不知道为什么它不起作用。希望能找到解决办法!

我正在处理类似的任务并且 运行 遇到了同样的问题。我发现解决方案是避免使用两种不同的录音方式。

PvRecorder class 将记录的数据表示为有符号整数。

pcm = recorder.read() 
result = porcupine.process(pcm)

据我所知,这与 wav 数据通常存储在音频文件中的方式不同。如果你查看 PvRecorder 如何保存 wav 文件,你可以看到它们使用结构模块:

sp = struct.pack("h" * len(pcm), *pcm)

您可以使用它来构建要传递的音频文件。 我将它与 Vosk 一起使用,效果很好!

if self.rec.AcceptWaveform(sp):
    res = json.loads(self.rec.Result())
    if res["text"] != "":
        print(res["text"])