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

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