Re: Speech Recognition
Thank you both. :) taixzo has happened to me this script. For now it does not work, but I'm on it. :rolleyes:
Code:
#!/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()
|