Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Recognizer.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # This is part of Blather
  2. # -- this code is licensed GPLv3
  3. # Copyright 2013 Jezra
  4. import gi
  5. gi.require_version('Gst', '1.0')
  6. from gi.repository import GObject, Gst
  7. GObject.threads_init()
  8. Gst.init(None)
  9. import os.path
  10. import sys
  11. # Define some global variables
  12. this_dir = os.path.dirname( os.path.abspath(__file__) )
  13. class Recognizer(GObject.GObject):
  14. __gsignals__ = {
  15. 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
  16. }
  17. def __init__(self, language_file, dictionary_file, src = None):
  18. GObject.GObject.__init__(self)
  19. self.commands = {}
  20. if src:
  21. audio_src = 'alsasrc device="hw:%d,0"' % (src)
  22. else:
  23. audio_src = 'autoaudiosrc'
  24. # Build the pipeline
  25. cmd = audio_src+' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false'
  26. try:
  27. self.pipeline=Gst.parse_launch( cmd )
  28. except Exception, e:
  29. print e.message
  30. print "You may need to install gstreamer1.0-pocketsphinx"
  31. raise e
  32. bus = self.pipeline.get_bus()
  33. bus.add_signal_watch()
  34. # Get the Auto Speech Recognition piece
  35. asr=self.pipeline.get_by_name('asr')
  36. bus.connect('message::element', self.result)
  37. asr.set_property('lm', language_file)
  38. asr.set_property('dict', dictionary_file)
  39. asr.set_property('configured', True)
  40. def listen(self):
  41. self.pipeline.set_state(Gst.State.PLAYING)
  42. def pause(self):
  43. self.pipeline.set_state(Gst.State.PAUSED)
  44. def result(self, bus, msg):
  45. msg_struct = msg.get_structure()
  46. # Ignore messages that aren't from pocketsphinx
  47. msgtype = msg_struct.get_name()
  48. if msgtype != 'pocketsphinx':
  49. return
  50. # If we have a final command, send it for processing
  51. command = msg_struct.get_string('hypothesis')
  52. if command != '' and msg_struct.get_boolean('final')[1]:
  53. self.emit("finished", command)