Somewhat fancy voice command recognition software
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

recognizer.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = f'alsasrc device="hw:{src},0"'
  22. else:
  23. audio_src = 'autoaudiosrc'
  24. # Build the pipeline
  25. cmd = (
  26. f'{audio_src}'
  27. f' ! audioconvert'
  28. f' ! audioresample'
  29. f' ! pocketsphinx lm={config.lang_file} dict={config.dic_file}'
  30. f' ! appsink sync=false'
  31. )
  32. try:
  33. self.pipeline = Gst.parse_launch(cmd)
  34. except Exception as e:
  35. print(e.message)
  36. print("You may need to install gstreamer1.0-pocketsphinx")
  37. raise e
  38. # Process results from the pipeline with self.result()
  39. bus = self.pipeline.get_bus()
  40. bus.add_signal_watch()
  41. bus.connect('message::element', self.result)
  42. def listen(self):
  43. self.pipeline.set_state(Gst.State.PLAYING)
  44. def pause(self):
  45. self.pipeline.set_state(Gst.State.PAUSED)
  46. def result(self, bus, msg):
  47. msg_struct = msg.get_structure()
  48. # Ignore messages that aren't from pocketsphinx
  49. msgtype = msg_struct.get_name()
  50. if msgtype != 'pocketsphinx':
  51. return
  52. # If we have a final command, send it for processing
  53. command = msg_struct.get_string('hypothesis')
  54. if command != '' and msg_struct.get_boolean('final')[1]:
  55. self.emit("finished", command.lower())