Somewhat fancy voice command recognition software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Blather.py 3.0KB

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