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.

Blather.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python2
  2. import sys
  3. import signal
  4. import gobject
  5. import os.path
  6. import subprocess
  7. from Recognizer import Recognizer
  8. #where are the files?
  9. conf_dir = os.path.expanduser("~/.config/blather")
  10. lang_dir = os.path.join(conf_dir, "language")
  11. command_file = os.path.join(conf_dir, "commands")
  12. strings_file = os.path.join(conf_dir, "sentences.corpus")
  13. lang_file = os.path.join(lang_dir,'lm')
  14. dic_file = os.path.join(lang_dir,'dic')
  15. #make the lang_dir if it doesn't exist
  16. if not os.path.exists(lang_dir):
  17. os.makedirs(lang_dir)
  18. class Blather:
  19. def __init__(self, args):
  20. self.ui = None
  21. self.continuous_listen = False
  22. self.commands = {}
  23. self.read_commands()
  24. self.recognizer = Recognizer(lang_file, dic_file)
  25. self.recognizer.connect('finished',self.recognizer_finished)
  26. #is there an arg?
  27. if len(args) > 1:
  28. if args[1] == "-qt":
  29. #import the ui from qt
  30. from QtUI import UI
  31. elif args[1] == "-gtk":
  32. from GtkUI import UI
  33. else:
  34. print "no GUI defined"
  35. sys.exit()
  36. self.ui = UI(args)
  37. self.ui.connect("command", self.process_command)
  38. def read_commands(self):
  39. #read the.commands file
  40. file_lines = open(command_file)
  41. strings = open(strings_file, "w")
  42. for line in file_lines:
  43. print line
  44. #trim the white spaces
  45. line = line.strip()
  46. #if the line has length and the first char isn't a hash
  47. if len(line) and line[0]!="#":
  48. #this is a parsible line
  49. (key,value) = line.split(":",1)
  50. print key, value
  51. self.commands[key.strip()] = value.strip()
  52. strings.write( key.strip()+"\n")
  53. #close the strings file
  54. strings.close()
  55. def recognizer_finished(self, recognizer, text):
  56. #is there a matching command?
  57. if self.commands.has_key( text ):
  58. cmd = self.commands[text]
  59. print cmd
  60. subprocess.call(cmd, shell=True)
  61. else:
  62. print "no matching command"
  63. #if there is a UI and we are not continuous listen
  64. if self.ui:
  65. if not self.continuous_listen:
  66. #stop listening
  67. self.recognizer.pause()
  68. #let the UI know that there is a finish
  69. self.ui.finished(text)
  70. def run(self):
  71. if self.ui:
  72. self.ui.run()
  73. else:
  74. blather.recognizer.listen()
  75. def quit(self):
  76. if self.ui:
  77. self.ui.quit()
  78. sys.exit()
  79. def process_command(self, UI, command):
  80. print command
  81. if command == "listen":
  82. self.recognizer.listen()
  83. elif command == "stop":
  84. self.recognizer.pause()
  85. elif command == "continuous_listen":
  86. self.continuous_listen = True
  87. self.recognizer.listen()
  88. elif command == "continuous_stop":
  89. self.continuous_listen = False
  90. self.recognizer.pause()
  91. elif command == "quit":
  92. self.quit()
  93. if __name__ == "__main__":
  94. #make our blather object
  95. blather = Blather(sys.argv)
  96. #init gobject threads
  97. gobject.threads_init()
  98. #we want a main loop
  99. main_loop = gobject.MainLoop()
  100. #handle sigint
  101. signal.signal(signal.SIGINT, signal.SIG_DFL)
  102. #run the blather
  103. blather.run()
  104. #start the main loop
  105. try:
  106. main_loop.run()
  107. except:
  108. print "time to quit"
  109. main_loop.quit()
  110. sys.exit()