Somewhat fancy voice command recognition software
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TTS.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python2
  2. import gst
  3. import subprocess
  4. import os.path
  5. import time
  6. import gobject
  7. #define some global variables
  8. this_dir = os.path.dirname( os.path.abspath(__file__) )
  9. lang_dir = os.path.join(this_dir, "language")
  10. command_file = os.path.join(this_dir, "commands")
  11. strings_file = os.path.join(this_dir, "sentences.corpus")
  12. class TTS(gobject.GObject):
  13. __gsignals__ = {
  14. 'finished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_BOOLEAN,))
  15. }
  16. def __init__(self):
  17. gobject.GObject.__init__(self)
  18. self.commands = {}
  19. #build the pipeline
  20. cmd = 'autoaudiosrc ! audioconvert ! audioresample ! vader name=vad ! pocketsphinx name=asr ! appsink sync=false'
  21. self.pipeline=gst.parse_launch( cmd )
  22. #get the Auto Speech Recognition piece
  23. asr=self.pipeline.get_by_name('asr')
  24. asr.connect('result', self.result)
  25. asr.set_property('lm', os.path.join(lang_dir, 'lm'))
  26. asr.set_property('dict', os.path.join(lang_dir, 'dic'))
  27. asr.set_property('configured', True)
  28. #get the Voice Activity DEtectoR
  29. self.vad = self.pipeline.get_by_name('vad')
  30. self.vad.set_property('auto-threshold',True)
  31. self.read_commands()
  32. #init gobject threads
  33. gobject.threads_init()
  34. def listen(self):
  35. self.pipeline.set_state(gst.STATE_PLAYING)
  36. def pause(self):
  37. self.vad.set_property('silent', True)
  38. self.pipeline.set_state(gst.STATE_PAUSED)
  39. def result(self, asr, text, uttid):
  40. self.emit("finished", True)
  41. print text
  42. #is there a matching command?
  43. if self.commands.has_key( text ):
  44. cmd = self.commands[text]
  45. print cmd
  46. subprocess.call(cmd, shell=True)
  47. else:
  48. print "no matching command"
  49. #emit finished
  50. def read_commands(self):
  51. #read the.commands file
  52. file_lines = open(command_file)
  53. strings = open(strings_file, "w")
  54. for line in file_lines:
  55. #trim the white spaces
  56. line = line.strip()
  57. #if the line has length and the first char isn't a hash
  58. if len(line) and line[0]!="#":
  59. #this is a parsible line
  60. (key,value) = line.split(":",1)
  61. print key, value
  62. self.commands[key.strip()] = value.strip()
  63. strings.write( key.strip()+"\n")
  64. #close the strings file
  65. strings.close()
  66. if __name__ == "__main__":
  67. b = Blather()
  68. b.listen()
  69. main_loop = gobject.MainLoop()
  70. #start the main loop
  71. try:
  72. main_loop.run()
  73. except:
  74. main_loop.quit()