使用Python和Pocketsphinx进行实时识别

Ton*_*sai 6 python cmusphinx

我最近在python中使用口袋狮身人面像.我已经成功地通过以下示例来识别录制的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 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
Run Code Online (Sandbox Code Playgroud)

问题是如何从麦克风进行实时语音识别?在带有if语句的while循环中,如果从麦克风识别出一个设置字,则可以调用一个函数?

Nik*_*rev 5

实时识别的代码如下所示

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()
Run Code Online (Sandbox Code Playgroud)

您还可以在 pocketsphinx 中使用 gstreamer python 绑定,查看livedemo.py