繁体   English   中英

如何在python中使用pocketphinx(5prealpha)和gstreamer-1.0?

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

我正在尝试创建一个小的Python脚本,它将通过网络接收音频流,通过pocketspinx将其转换为将语音转换为文本并根据pocketsphinx的输出运行一些命令。

我已经在Ubuntu 15.10 vm上安装了sphinxbase和pocketsphinx(5prealpha),并且能够在Python中正确处理示例audiofile(pocketphinx安装的一部分)的内容。 所以我有理由相信我的sphinx安装工作正常。 不幸的是,测试python脚本无法处理连续音频并使用本机pocketsphinx API。 根据cmusphinx网站,我应该使用gstreamer进行连续翻译。 不幸的是,有关如何在Python中使用pocketphinx与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并将输出打印到终端。 如果我更换'队列! pocketsphinx! fakesink'部分由'wavenc! filesink',我得到一个有正确内容的有效音频文件,所以我知道网络发送部分工作正常。 (我的测试机器上没有音频,因此我无法使用本地音频源进行测试)。

当我启动脚本时,我看到pocketpinx配置通过但是脚本似乎根本不再做任何事情了。 当我用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的samprate配置参数和声学模型。

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

原来是让gstreamer管道正确设置的小提琴。 正如Nikolay所说,pocketphinx 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM