我正在我的3B+上构建一个个人助理。我现在要做的就是用雪花公子来检测我的热词(工作得很完美),然后在检测到热词之后,用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雪童工作得很好。程序按预期运行,直到检测到hotword为止。我想这一定与雪童和SpeechRecognition试图使用麦克风的事实有关。还要注意的是,SpeechRecognition本身工作得很好。如果我创建了一个只使用SpeechRecognition而不使用雪人的程序,它会按预期工作。
我在运行Raspbian Buster的Raspberry Pi 3b+上使用Python3。
如果我能提供更多信息,请让我知道。
发布于 2020-03-26 04:08:21
解决方案是在初始化麦克风Ex之前终止snowboy:
import speech_recognition as sr
import snowboydecoder
def detected_callback():
detector.terminate() #change here
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)发布于 2020-04-11 15:55:46
问题是,(雪人和SpeechRec)都试图连接到你的麦克风。并且第二个连接尝试将被拒绝。
几天前我也遇到了同样的问题。来自@Mezex的解决方案很好,在我的情况下,只需很长时间就可以关闭雪人男孩并打开SpeechRec (大约2到3秒,直到它准备好收听)
我的解决方案是ALSA。有一个名为dsnoop的插件,它类似于设备文件,可以被多个程序调用。
文件/etc/asound.config决定如何处理您的孔操作系统的声音。如果将其更改为:
defaults.pcm.rate_converter "samplerate"
pcm.!default {
type asym
playback.pcm "playback"
capture.pcm "capture"
}
pcm.playback {
type plug
slave.pcm "dmixed"
}
pcm.capture {
type plug
slave.pcm "array"
}
pcm.dmixed {
type dmix
slave.pcm "hw:name_of_your_speaker"
ipc_key 555555
}
pcm.array {
type dsnoop
slave {
pcm "hw:name_of_your_Mic"
channels 2
}
ipc_key 666666
}并删除您的~/asoundrc文件,它应该可以工作。
您将通过arecord -l In my case it is "wm8960soundcard"获得麦克风的名称
默认情况下会调用pcm.!default。它指的是caputre的输入流(麦克风),后者指的是array。这是通过可由多个任务同时调用的type dsnoop实现的。
看看https://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html,里面列出了所有的插件
我知道这有点复杂,但对我来说这是最好的解决方案。希望这能帮上忙。
https://stackoverflow.com/questions/60839994
复制相似问题