如何在 python 中将 pocketsphinx (5prealpha) 与 gstreamer-1.0 一起使用?

How to use pocketsphinx (5prealpha) with gstreamer-1.0 in python?

我正在尝试创建一个小的 Python 脚本,它将通过网络接收音频流,通过 pocketspinx 将其馈送以将语音转换为文本,并且 运行 一些命令取决于输出口袋狮身人面像。

我已经在 Ubuntu 15.10 虚拟机上安装了 sphinxbase 和 pocketsphinx (5prealpha),并且能够在 Python 中正确处理示例音频文件(pocketsphinx 安装的一部分)的内容。所以我有理由相信我的 sphinx 安装工作正常。不幸的是,测试 python 脚本无法处理连续音频并使用原生 pocketsphinx API。根据 cmusphinx 网站,我应该使用 gstreamer 进行连续翻译。不幸的是,Python 中有关如何将 pocketsphinx 与 gstreamer 一起使用的信息相当有限。根据我能找到的示例,我拼凑了以下脚本。

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)

def element_message( bus, msg ):
         msgtype = msg.get_structure().get_name()
         if msgtype != 'pocketsphinx':
                 return
         print "hypothesis= '%s'  confidence=%s\n" % (msg.get_structure().get_value('hypothesis'), msg.get_structure().get_value('confidence'))

pipeline = Gst.parse_launch('udpsrc port=3000 name=src caps=application/x-rtp ! rtppcmadepay name=rtpp ! alawdec name=decoder ! queue ! pocketsphinx name=asr ! fakesink')

asr = pipeline.get_by_name("asr")
asr.set_property("configured", "true")

bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::element', element_message)

pipeline.set_state(Gst.State.PLAYING)

# enter into a mainloop
loop = GObject.MainLoop()
loop.run()

发送方看起来像:

import gobject, pygst
pygst.require("0.10")
import gst

pipeline = gst.parse_launch('alsasrc ! audioconvert ! audioresample ! alawenc ! rtppcmapay ! udpsink  port=3000 host=192.168.13.120')
pipeline.set_state(gst.STATE_PLAYING)
loop = gobject.MainLoop()
loop.run()

这应该从网络接收一个 udp 流,将其输入 pocketsphinx 并将输出打印到终端。如果我用 'wavenc ! filesink' 替换 'queue ! pocketsphinx ! fakesink' 部分,我会得到一个内容正确的有效音频文件,所以我知道网络发送部分工作正常。 (我的测试机器上没有音频,所以我无法使用本地音频源进行测试)。

当我启动脚本时,我看到 pocketspinx 配置经过,但随后脚本似乎不再执行任何操作。 当我使用 GST_DEBUG=*:4 启动脚本时,我看到以下输出:

0:00:04.789157687  2220      0x86fff70 INFO               GST_EVENT gstevent.c:760:gst_event_new_segment: creating segment event time segment start=0:00:00.000000000, offset=0:00:00.000000000, stop=99:99:99.999999999, rate=1.000000, applied_rate=1.000000, flags=0x00, time=0:00:00.000000000, base=0:00:00.000000000, position 0:00:00.000000000, duration 99:99:99.999999999
0:00:04.789616981  2220      0x86fff70 INFO                 basesrc gstbasesrc.c:2838:gst_base_src_loop:<src> marking pending DISCONT
0:00:04.789995780  2220      0x86fff70 INFO               GST_EVENT gstevent.c:760:gst_event_new_segment: creating segment event time segment start=0:00:00.000000000, offset=0:00:00.000000000, stop=99:99:99.999999999, rate=1.000000, applied_rate=1.000000, flags=0x00, time=0:00:00.000000000, base=0:00:00.000000000, position 0:00:04.079311489, duration 99:99:99.999999999
0:00:04.790420834  2220      0x86fff70 INFO               GST_EVENT gstevent.c:679:gst_event_new_caps: creating caps event audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)8000, channels=(int)1
0:00:04.790851965  2220      0x86fff70 WARN                GST_PADS gstpad.c:3989:gst_pad_peer_query:<decoder:src> could not send sticky events
0:00:04.791258320  2220      0x86fff70 WARN                 basesrc gstbasesrc.c:2943:gst_base_src_loop:<src> error: Internal data flow error.
0:00:04.791572605  2220      0x86fff70 WARN                 basesrc gstbasesrc.c:2943:gst_base_src_loop:<src> error: streaming task paused, reason not-negotiated (-4)
0:00:04.791917073  2220      0x86fff70 INFO        GST_ERROR_SYSTEM gstelement.c:1837:gst_element_message_full:<src> posting message: Internal data flow error.
0:00:04.792305347  2220      0x86fff70 INFO        GST_ERROR_SYSTEM gstelement.c:1860:gst_element_message_full:<src> posted error message: Internal data flow error.
0:00:04.792633841  2220      0x86fff70 INFO                    task gsttask.c:315:gst_task_func:<src:src> Task going to paused

根据我在谷歌上找到的信息和示例,我不明白哪里出了问题。

非常感谢任何帮助。

妮可

Gstreamer 元素需要 16000 khz 音频,您正在尝试通过 8000。您必须修改 pocketsphinx 源以在 pocketsphinx 元素中启用 8000。您需要更新元素规格率、pocketsphinx 的采样率配置参数和声学模型。

或者,您需要通过网络发送宽带音频。在那种情况下,你不应该使用 alaw 编解码器。

事实证明,要正确设置 gstreamer 管道需要 fiddle 一点点。正如 Nikolay 指出的那样,pocketsphinx 5 默认需要 16000kHz 音频,不幸的是,使用 gstreamer 通过网络发送 16000kHz 并不简单(对我而言)。因此,如果您碰巧正在寻找类似的东西,那么最终对我有用的是:

发送端:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)

pipeline = Gst.parse_launch('alsasrc ! audioconvert ! audio/x-raw,channels=1,depth=16,width=16,rate=16000 ! rtpL16pay  ! udpsink port=3000 host=192.168.13.120')    
pipeline.set_state(Gst.State.PLAYING)

loop = GObject.MainLoop()
loop.run()

接收方:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)

def element_message( bus, msg ):
    msgtype = msg.get_structure().get_name()
    print "hypothesis= '%s'  confidence=%s\n" % (msg.get_structure().get_value('hypothesis'),msg.get_structure().get_value('confidence'))

pipeline = Gst.parse_launch('udpsrc port=3000 name=src ! application/x-rtp,media=(string)audio, clock-rate=(int)16000, width=16, height=16, encoding-name=(string)L16, encoding-params=(string)1, channels=(int)1, channel-positions=(int)1, payload=(int)96 ! rtpL16depay ! audioconvert ! pocketsphinx name=asr ! fakesink')
asr = pipeline.get_by_name("asr")
asr.set_property("dict", "/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict")
asr.set_property("lm","/usr/local/share/pocketsphinx/model/en-us/en-us.lm.bin")
asr.set_property("hmm","/usr/local/share/pocketsphinx/model/en-us/en-us/")
asr.set_property("configured", "true")

bus = pipeline.get_bus(
bus.add_signal_watch()
bus.connect('message::element', element_message)

pipeline.set_state(Gst.State.PLAYING)

loop = GObject.MainLoop()