下面的代码我尝试在Raspberry Pi 3 Model B上运行它,它的内存容量有点大,我面对代码的问题是它有时运行:
from os import environ, path
import pyaudio
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
MODELDIR = "../../../model"
DATADIR = "../../../test/data"
config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, '3199.lm'))
config.set_string('-dict', path.join(MODELDIR, '3199.dic'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()
in_speech_bf = False
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
if decoder.get_in_speech() != in_speech_bf:
in_speech_bf = decoder.get_in_speech()
if not in_speech_bf:
decoder.end_utt()
result = decoder.hyp().hypstr
print 'Result:', result
if result == 'yes':
print 'Do whatever you want'
decoder.start_utt()
else:
break
decoder.end_utt()
程序不断崩溃并抛出以下异常: OSError:[ - 9985] Errno设备不可用
答案 0 :(得分:1)
首先尝试打开和关闭流。
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
# stream.start_stream()
in_speech_bf = False
decoder.start_utt()
while True:
if stream.is_stopped():
stream.start_stream()
buf = stream.read(1024)
if buf:
stream.stop_stream()
decoder.process_raw(buf, False, False)
如果您仍然遇到问题,请尝试使用~/.asoundrc
pcm.record {
type plug;
slave {
pcm "hw:<CARD>,<DEVICE>"
}
}
找出CAPTURE设备(用于录音的声卡)并记下CARD
号码和DEVICE
号码。在下面的例子中,两者都是0.替换上面插件中的CARD和DEVICE值。
> arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: Devices [USB Device 2345:3x55], device 0: USB Audio [USB Audio]
现在插件看起来像
pcm.record {
type plug;
slave {
pcm "hw:0,0"
}
}
保存~/.asoundrc
文件并重启RPi。
现在使用以下python脚本找出新创建的设备index
。{/ p>
pcm.record
它会输出INDEX(这里是9,但在你的情况下可能会有所不同)
import pyaudio
po = pyaudio.PyAudio()
for index in range(po.get_device_count()):
desc = po.get_device_info_by_index(index)
if desc["name"] == "record":
print "DEVICE: %s INDEX: %s RATE: %s " % (desc["name"], index, int(desc["defaultSampleRate"]))
现在稍微更改主脚本(在DEVICE: record INDEX: 9 RATE: 48000
中插入input_device_index=9
)
p.open()
多数,再次运行您的脚本,看看问题是否已解决。