来自麦克风的 Python 口袋狮身人面像识别 [英] Python pocketsphinx recognition from the microphone
本文介绍了来自麦克风的 Python 口袋狮身人面像识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在 python 中安装并设置了 pocketsphinx 和 sphinxbase 包.我还为 github 获取了语音识别代码,并根据要求更改了数据和模式目录,但是当我尝试通过python test.py"运行它时仍然无法通过语音进行流式传输代码如下:
I have installed and setup both pocketsphinx and sphinxbase packages in python. I have also taken code of speech recognition for github and changed both data and mode directory as per requirement but still it is unable to stream by voice when I am trying to run it by "python test.py" Here is the code:
#!/usr/bin/env python
import os
import sphinxbase as sb
import pocketsphinx as ps
MODELDIR = '/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data'
DATADIR='/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data'
# Create a decoder with certain model
config = ps.Decoder.default_config()
config.set_string('-hmm', "/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data/en-US/acoustic-model")
config.set_string('-lm', os.path.join(MODELDIR, 'en-US/language-model.lm.bin'))
config.set_string('-dict', os.path.join(MODELDIR, 'en-US/pronounciation-dictionary.dict'))
decoder = ps.Decoder(config)
# Decode streaming data.
decoder.start_utt()
stream = open(os.path.join(DATADIR, 'en-US/goforward.raw'), 'rb')
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
else:
break
decoder.end_utt()
stream.close()
print('Best hypothesis segments:', [seg.word for seg in decoder.seg()])
请告诉我如何执行它.
推荐答案
麦克风的连续识别应该是这样的:
Continuous recognition from microphone should look like this:
#!/usr/bin/python
from os import environ, path
import pyaudio
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
MODELDIR = "../../../model"
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'))
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()
print 'Result:', decoder.hyp().hypstr
decoder.start_utt()
else:
break
decoder.end_utt()
这篇关于来自麦克风的 Python 口袋狮身人面像识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文