Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. sphinx.py is a wrapper for pocketsphinx.
  5. parameters:
  6. ~lm - filename of language model
  7. ~dict - filename of dictionary
  8. ~mic_name - set the pulsesrc device name for the microphone input.
  9. e.g. a Logitech G35 Headset has the following device name: alsa_input.usb-Logitech_Logitech_G35_Headset-00-Headset_1.analog-mono
  10. To list audio device info on your machine, in a terminal type: pacmd list-sources
  11. publications:
  12. ~output (std_msgs/String) - text output
  13. services:
  14. ~start (std_srvs/Empty) - start speech recognition
  15. ~stop (std_srvs/Empty) - stop speech recognition
  16. """
  17.  
  18. import roslib; roslib.load_manifest('speech_listener')
  19. import rospy
  20.  
  21. import pygtk
  22. pygtk.require('2.0')
  23. import gtk
  24.  
  25. import gobject
  26. import pygst
  27. pygst.require('0.10')
  28. gobject.threads_init()
  29. import gst
  30.  
  31. from std_msgs.msg import String
  32. from std_srvs.srv import *
  33. import os
  34. import commands
  35.  
  36. class recognizer(object):
  37. """ GStreamer based speech recognizer. """
  38.  
  39. def __init__(self):
  40. # Start node
  41. rospy.init_node("sphinx")
  42.  
  43. self._device_name_param = "~mic_name" # Find the name of your microphone by typing pacmd list-sources in the terminal
  44. self._hmm_param = "~hmm"
  45. self._jsgf_param = "~jsgf"
  46. self._lm_param = "~lm"
  47. self._dic_param = "~dict"
  48.  
  49. # Configure mics with gstreamer launch config
  50. if rospy.has_param(self._device_name_param):
  51. self.device_name = rospy.get_param(self._device_name_param)
  52. self.device_index = self.pulse_index_from_name(self.device_name)
  53. self.launch_config = "pulsesrc device=" + str(self.device_index)
  54. rospy.loginfo("Using: pulsesrc device=%s name=%s", self.device_index, self.device_name)
  55. elif rospy.has_param('~source'):
  56. # common sources: 'alsasrc'
  57. self.launch_config = rospy.get_param('~source')
  58. else:
  59. self.launch_config = 'gconfaudiosrc'
  60.  
  61. rospy.loginfo("Launch config: %s", self.launch_config)
  62.  
  63. self.launch_config += " ! audioconvert ! audioresample " \
  64. + '! vader name=vad auto-threshold=true ' \
  65. + '! pocketsphinx name=asr ! fakesink'
  66.  
  67. # Configure ROS settings
  68. self.started = False
  69. rospy.on_shutdown(self.shutdown)
  70. self.pub = rospy.Publisher('~output', String)
  71. rospy.Service("~start", Empty, self.start)
  72. rospy.Service("~stop", Empty, self.stop)
  73.  
  74. if rospy.has_param(self._hmm_param) and (
  75. (rospy.has_param(self._lm_param) and rospy.has_param(self._dic_param)) or
  76. rospy.has_param(self._jsgf_param)
  77. ):
  78. self.start_recognizer()
  79. else:
  80. rospy.logwarn("((lm and dic) or jsgf) and hmm parameters need to be set to start recognizer.")
  81.  
  82. def start_recognizer(self):
  83. rospy.loginfo("Starting recognizer... ")
  84.  
  85. self.pipeline = gst.parse_launch(self.launch_config)
  86. self.asr = self.pipeline.get_by_name('asr')
  87. self.asr.connect('partial_result', self.asr_partial_result)
  88. self.asr.connect('result', self.asr_result)
  89.  
  90. # Configure language model
  91. if rospy.has_param(self._hmm_param):
  92. hmm = rospy.get_param(self._hmm_param)
  93. else:
  94. rospy.logerr('Recognizer not started. Please specify a speech model file.')
  95. return
  96.  
  97. if rospy.has_param(self._jsgf_param):
  98. jsgf = rospy.get_param(self._jsgf_param)
  99. else:
  100. if rospy.has_param(self._lm_param):
  101. lm = rospy.get_param(self._lm_param)
  102. else:
  103. rospy.logerr('Recognizer not started. Please specify a language model file.')
  104. return
  105.  
  106. if rospy.has_param(self._dic_param):
  107. dic = rospy.get_param(self._dic_param)
  108. else:
  109. rospy.logerr('Recognizer not started. Please specify a dictionary.')
  110. return
  111.  
  112. self.asr.set_property('hmm', hmm)
  113. self.asr.set_property('dict', dic)
  114. self.asr.set_property('lm', lm)
  115. self.asr.set_property('dsratio', 1)
  116.  
  117. self.asr.set_property('configured', True)
  118.  
  119. self.bus = self.pipeline.get_bus()
  120. self.bus.add_signal_watch()
  121. self.bus_id = self.bus.connect('message::application', self.application_message)
  122. self.pipeline.set_state(gst.STATE_PLAYING)
  123. self.started = True
  124.  
  125. def pulse_index_from_name(self, name):
  126. output = commands.getstatusoutput("pacmd list-sources | grep -B 1 'name: <" + name + ">' | grep -o -P '(?<=index: )[0-9]*'")
  127.  
  128. if len(output) == 2:
  129. return output[1]
  130. else:
  131. raise Exception("Error. pulse index doesn't exist for name: " + name)
  132.  
  133. def stop_recognizer(self):
  134. if self.started:
  135. self.pipeline.set_state(gst.STATE_NULL)
  136. self.pipeline.remove(self.asr)
  137. self.bus.disconnect(self.bus_id)
  138. self.started = False
  139.  
  140. def shutdown(self):
  141. """ Delete any remaining parameters so they don't affect next launch """
  142. for param in [self._device_name_param, self._lm_param, self._dic_param, self._hmm_param, self._jsgf_param]:
  143. if rospy.has_param(param):
  144. rospy.delete_param(param)
  145.  
  146. """ Shutdown the GTK thread. """
  147. gtk.main_quit()
  148.  
  149. def start(self, req):
  150. self.start_recognizer()
  151. rospy.loginfo("recognizer started")
  152. return EmptyResponse()
  153.  
  154. def stop(self, req):
  155. self.stop_recognizer()
  156. rospy.loginfo("recognizer stopped")
  157. return EmptyResponse()
  158.  
  159. def asr_partial_result(self, asr, text, uttid):
  160. """ Forward partial result signals on the bus to the main thread. """
  161. struct = gst.Structure('partial_result')
  162. struct.set_value('hyp', text)
  163. struct.set_value('uttid', uttid)
  164. asr.post_message(gst.message_new_application(asr, struct))
  165.  
  166. def asr_result(self, asr, text, uttid):
  167. """ Forward result signals on the bus to the main thread. """
  168. struct = gst.Structure('result')
  169. struct.set_value('hyp', text)
  170. struct.set_value('uttid', uttid)
  171. asr.post_message(gst.message_new_application(asr, struct))
  172.  
  173. def application_message(self, bus, msg):
  174. """ Receive application messages from the bus. """
  175. msgtype = msg.structure.get_name()
  176. if msgtype == 'partial_result':
  177. self.partial_result(msg.structure['hyp'], msg.structure['uttid'])
  178. if msgtype == 'result':
  179. self.final_result(msg.structure['hyp'], msg.structure['uttid'])
  180.  
  181. def partial_result(self, hyp, uttid):
  182. """ Delete any previous selection, insert text and select it. """
  183. rospy.logdebug("Partial: " + hyp)
  184.  
  185. def final_result(self, hyp, uttid):
  186. """ Insert the final result. """
  187. msg = String()
  188. msg.data = str(hyp.lower())
  189. rospy.loginfo(msg.data)
  190. self.pub.publish(msg)
  191.  
  192. if __name__ == "__main__":
  193. start = recognizer()
  194. gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement