The following code I tried to run it on Raspberry Pi 3 Model B which has a little big of capacity on it's memory, the problem that I'm facing with the code is that it runs sometimes:
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()
The program keep crashing and throws the following exception: OSError: [-9985] Errno Device unavailable