我正在我的 3B+ 上构建个人助理。我现在要做的是使用 Snowboy 检测我的启动词(完美运行),然后在检测到启动词后,使用 SpeechRecognizer 接收语音命令。启动指令检测工作正常,调用 sr.Microphone() 时会发生错误。
示例代码:
import speech_recognition as sr
import snowboydecoder
def detected_callback():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Ready...')
r.adjust_for_ambient_noise(source, duration=.2)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '\n')
except sr.UnknownValueError:
print('Your last command couldn\'t be heard')
comand = None
detector = snowboydecoder.HotwordDetector("SnowboyDependencies/Ancilla.pmdl", sensitivity=.5, audio_gain=1)
detector.start(detected_callback)
我收到以下输出:
INFO:snowboy:Keyword 1 detected at time: 2020-03-24 21:53:35
Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1736
Expression 'AlsaOpen( &alsaApi->baseHostApiRep, params, streamDir, &self->pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1904
Expression 'PaAlsaStreamComponent_Initialize( &self->capture, alsaApi, inParams, StreamDirection_In, NULL != callback )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2171
Expression 'PaAlsaStream_Initialize( stream, alsaHostApi, inputParameters, outputParameters, sampleRate, framesPerBuffer, callback, streamFlags, userData )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2840
Traceback (most recent call last):
File "sttTest.py", line 43, in <module>
detector.start(detected_callback)
File "/home/pi/AncillaFiles/SnowboyDependencies/snowboydecoder.py", line 221, in start
callback()
File "sttTest.py", line 27, in detected_callback
with sr.Microphone(device_index = 2, sample_rate = 44100, chunk_size = 512) as source:
File "/home/pi/.local/lib/python3.7/site-packages/speech_recognition/__init__.py", line 141, in __enter__
input=True, # stream is an input stream
File "/usr/local/lib/python3.7/dist-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9985] Device unavailable
雪人工作正常。程序按预期运行,直到检测到启动指令。我认为这一定与 Snowboy 和 SpeechRecognition 试图使用麦克风有关。另请注意,SpeechRecognition 本身可以正常工作。如果我创建一个只使用 SpeechRecognition 而不是 Snowboy 的程序,它会按预期工作。
我在运行 Raspbian Buster 的 Raspberry Pi 3b+ 上使用 Python3。
如果我可以提供更多信息,请告诉我。