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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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().lower()] = value.strip()
  52. strings.write( key.strip()+"\n")
  53. #close the strings file
  54. strings.close()
  55. def recognizer_finished(self, recognizer, text):
  56. t = text.lower()
  57. #is there a matching command?
  58. if self.commands.has_key( t ):
  59. cmd = self.commands[t]
  60. print cmd
  61. subprocess.call(cmd, shell=True)
  62. else:
  63. print "no matching command"
  64. #if there is a UI and we are not continuous listen
  65. if self.ui:
  66. if not self.continuous_listen:
  67. #stop listening
  68. self.recognizer.pause()
  69. #let the UI know that there is a finish
  70. self.ui.finished(t)
  71. def run(self):
  72. if self.ui:
  73. self.ui.run()
  74. else:
  75. blather.recognizer.listen()
  76. def quit(self):
  77. if self.ui:
  78. self.ui.quit()
  79. sys.exit()
  80. def process_command(self, UI, command):
  81. print command
  82. if command == "listen":
  83. self.recognizer.listen()
  84. elif command == "stop":
  85. self.recognizer.pause()
  86. elif command == "continuous_listen":
  87. self.continuous_listen = True
  88. self.recognizer.listen()
  89. elif command == "continuous_stop":
  90. self.continuous_listen = False
  91. self.recognizer.pause()
  92. elif command == "quit":
  93. self.quit()
  94. if __name__ == "__main__":
  95. #make our blather object
  96. blather = Blather(sys.argv)
  97. #init gobject threads
  98. gobject.threads_init()
  99. #we want a main loop
  100. main_loop = gobject.MainLoop()
  101. #handle sigint
  102. signal.signal(signal.SIGINT, signal.SIG_DFL)
  103. #run the blather
  104. blather.run()
  105. #start the main loop
  106. try:
  107. main_loop.run()
  108. except:
  109. print "time to quit"
  110. main_loop.quit()
  111. sys.exit()