python - 使用 Python 和 Pocketsphinx 进行实时识别

标签 python cmusphinx

我最近一直在使用 python 处理 pocket sphinx。我已经成功获得 下面的示例可以识别录制的 wav。

#!/usr/bin/env python

import sys,os



def decodeSpeech(hmmd,lmdir,dictp,wavfile):

    """

    Decodes a speech file

    """

    try:

        import pocketsphinx as ps

        import sphinxbase

    except:

        print """Pocket sphinx and sphixbase is not installed

        in your system. Please install it with package manager.

        """

    speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp)

    wavFile = file(wavfile,'rb')

    wavFile.seek(44)

    speechRec.decode_raw(wavFile)

    result = speechRec.get_hyp()



    return result[0]



if __name__ == "__main__":

    hmdir = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/hmm/wsj1"

    lmd = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/lm/wsj/wlist5o.3e-7.vp.tg.lm.DMP"

    dictd = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/lm/wsj/wlist5o.dic"

    wavfile = "/home/jaganadhg/Desktop/Docs_New/kgisl/sa1.wav"

    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)

    print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

    print recognised

    print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

问题是如何通过麦克风进行实时语音识别?在 带有 if 语句的 while 循环,以便如果从麦克风识别出设置的单词 可以调用函数吗?

最佳答案

实时识别的代码看起来像this :

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()

您还可以在 pocketsphinx 中使用 gstreamer python 绑定(bind),检查 livedemo.py

关于python - 使用 Python 和 Pocketsphinx 进行实时识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14307816/

相关文章:

python - 将 pandas DataFrame 的 2 列与行条件相加

python - 在 svg 中设置默认单位(Python svgwrite)

Python/Numpy - 使用变量从二维数组中提取二维子数组

c - Sphinxbase 制作/安装失败

cmusphinx - 如何关闭 pocketsphinx 中的 E_INFO?

java - 使用 CMUSphinx 的 HelloWorld 程序

python - 替换单行中的所有正则表达式匹配

python - Docker 容器无法连接到本地主机上运行的服务器

java - 如何在Sphinx4中评估样本

python - 我们如何使用 pocketsphinx 将 .wav 文件转换为文本?