![]() |
2012-08-14
, 20:21
|
Posts: 83 |
Thanked: 64 times |
Joined on May 2012
@ Spain
|
#1
|
![]() |
2012-08-14
, 21:22
|
|
Posts: 2,473 |
Thanked: 12,265 times |
Joined on Oct 2009
@ Jerusalem, PS/IL
|
#2
|
![]() |
2012-08-14
, 21:28
|
|
Posts: 1,455 |
Thanked: 3,309 times |
Joined on Dec 2009
@ Rochester, NY
|
#3
|
![]() |
2012-08-15
, 07:51
|
Posts: 83 |
Thanked: 64 times |
Joined on May 2012
@ Spain
|
#4
|
#!/usr/bin/python import math import gobject import pygst pygst.require('0.10') gobject.threads_init() import gst class Example: def __init__(self): self.init_gst() def init_gst(self): """Initialize the speech components""" self.pipeline = gst.parse_launch('pulsesrc ! audioconvert ! audioresample ' + '! vader name=vad auto-threshold=true ' + '! pocketsphinx name=asr ! fakesink') asr = self.pipeline.get_by_name('asr') asr.connect('partial_result', self.asr_partial_result) asr.connect('result', self.asr_result) asr.set_property('configured', True) bus = self.pipeline.get_bus() bus.add_signal_watch() bus.connect('message::application', self.application_message) self.pipeline.set_state(gst.STATE_PAUSED) def asr_partial_result(self, asr, text, uttid): """Forward partial result signals on the bus to the main thread.""" struct = gst.Structure('partial_result') struct.set_value('hyp', text) struct.set_value('uttid', uttid) asr.post_message(gst.message_new_application(asr, struct)) def asr_result(self, asr, text, uttid): """Forward result signals on the bus to the main thread.""" struct = gst.Structure('result') struct.set_value('hyp', text) struct.set_value('uttid', uttid) asr.post_message(gst.message_new_application(asr, struct)) def application_message(self, bus, msg): """Receive application messages from the bus.""" msgtype = msg.structure.get_name() if msgtype == 'partial_result': self.partial_result(msg.structure['hyp'], msg.structure['uttid']) elif msgtype == 'result': self.final_result(msg.structure['hyp'], msg.structure['uttid']) self.pipeline.set_state(gst.STATE_PAUSED) def partial_result(self, hyp, uttid): """Delete any previous selection, insert text and select it.""" # All this stuff appears as one single action print "Partial Result:", hyp def final_result(self, hyp, uttid): """Insert the final result.""" # All this stuff appears as one single action if hyp in ('start', 'power', 'red', 'green', 'yellow', 'blue') print "Final Result:", hyp else: print "Not a recognized word." if __name__=='__main__': app = Example() loop = gobject.MainLoop() gobject.threads_init() loop.run()