<p>实时识别的代码看起来像<a href="https://github.com/cmusphinx/pocketsphinx/blob/master/swig/python/test/continuous_test.py" rel="nofollow">this</a>:</p>
<pre><code>config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)
import pyaudio
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()
print 'Result:', decoder.hyp().hypstr
decoder.start_utt()
else:
break
decoder.end_utt()
</code></pre>
<p>您还可以在pocketspinx中使用gstreamer python绑定,检查<a href="https://github.com/cmusphinx/pocketsphinx/blob/master/src/gst-plugin/livedemo.py" rel="nofollow">livedemo.py</a></p>