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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2017 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. import os.path
  6. import sys
  7. import gi
  8. gi.require_version('Gst', '1.0')
  9. from gi.repository import GObject, Gst
  10. GObject.threads_init()
  11. Gst.init(None)
  12. class Recognizer(GObject.GObject):
  13. __gsignals__ = {
  14. 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_BOOLEAN,
  15. (GObject.TYPE_STRING,))
  16. }
  17. def __init__(self, config):
  18. GObject.GObject.__init__(self)
  19. src = config.options.microphone
  20. if src:
  21. audio_src = 'alsasrc device="hw:{0},0"'.format(src)
  22. else:
  23. audio_src = 'autoaudiosrc'
  24. # Build the pipeline
  25. cmd = (
  26. audio_src +
  27. ' ! audioconvert' +
  28. ' ! audioresample' +
  29. ' ! pocketsphinx lm=' + config.lang_file + ' dict=' +
  30. config.dic_file +
  31. ' ! appsink sync=false'
  32. )
  33. try:
  34. self.pipeline = Gst.parse_launch(cmd)
  35. except Exception as e:
  36. print(e.message)
  37. print("You may need to install gstreamer1.0-pocketsphinx")
  38. raise e
  39. # Process results from the pipeline with self.result()
  40. bus = self.pipeline.get_bus()
  41. bus.add_signal_watch()
  42. bus.connect('message::element', self.result)
  43. def listen(self):
  44. self.pipeline.set_state(Gst.State.PLAYING)
  45. def pause(self):
  46. self.pipeline.set_state(Gst.State.PAUSED)
  47. def result(self, bus, msg):
  48. msg_struct = msg.get_structure()
  49. # Ignore messages that aren't from pocketsphinx
  50. msgtype = msg_struct.get_name()
  51. if msgtype != 'pocketsphinx':
  52. return
  53. # If we have a final command, send it for processing
  54. command = msg_struct.get_string('hypothesis')
  55. if command != '' and msg_struct.get_boolean('final')[1]:
  56. self.emit("finished", command.lower())