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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/env python2
  2. # This is part of Kaylee
  3. # -- this code is licensed GPLv3
  4. # Copyright 2013 Jezra
  5. # Copyright 2015 Clayton G. Hobbs
  6. from __future__ import print_function
  7. import sys
  8. import signal
  9. import hashlib
  10. import os.path
  11. import subprocess
  12. from optparse import OptionParser
  13. from gi.repository import GObject
  14. try:
  15. import yaml
  16. except:
  17. print("YAML is not supported; unable to use config file")
  18. from recognizer import Recognizer
  19. # Where are the files?
  20. conf_dir = os.path.expanduser("~/.config/blather")
  21. lang_dir = os.path.join(conf_dir, "language")
  22. command_file = os.path.join(conf_dir, "commands.conf")
  23. strings_file = os.path.join(conf_dir, "sentences.corpus")
  24. history_file = os.path.join(conf_dir, "blather.history")
  25. opt_file = os.path.join(conf_dir, "options.yaml")
  26. hash_file = os.path.join(conf_dir, "hash.yaml")
  27. lang_file = os.path.join(lang_dir, 'lm')
  28. dic_file = os.path.join(lang_dir, 'dic')
  29. # Make the lang_dir if it doesn't exist
  30. if not os.path.exists(lang_dir):
  31. os.makedirs(lang_dir)
  32. class Blather:
  33. def __init__(self, opts):
  34. self.ui = None
  35. self.options = {}
  36. ui_continuous_listen = False
  37. self.continuous_listen = False
  38. self.commands = {}
  39. # Read the commands
  40. self.read_commands()
  41. # Load the options file
  42. self.load_options()
  43. # Merge the options with the ones provided by command-line arguments
  44. for k, v in opts.__dict__.items():
  45. self.options[k] = v
  46. if self.options['interface'] != None:
  47. if self.options['interface'] == "g":
  48. from gtkui import UI
  49. elif self.options['interface'] == "gt":
  50. from gtktrayui import UI
  51. else:
  52. print("no GUI defined")
  53. sys.exit()
  54. self.ui = UI(args, self.options['continuous'])
  55. self.ui.connect("command", self.process_command)
  56. # Can we load the icon resource?
  57. icon = self.load_resource("icon.png")
  58. if icon:
  59. self.ui.set_icon_active_asset(icon)
  60. # Can we load the icon_inactive resource?
  61. icon_inactive = self.load_resource("icon_inactive.png")
  62. if icon_inactive:
  63. self.ui.set_icon_inactive_asset(icon_inactive)
  64. if self.options['history']:
  65. self.history = []
  66. # Update the language if necessary
  67. self.update_language()
  68. # Create the recognizer
  69. try:
  70. self.recognizer = Recognizer(lang_file, dic_file, self.options['microphone'])
  71. except Exception as e:
  72. # No recognizer? bummer
  73. print('error making recognizer')
  74. sys.exit()
  75. self.recognizer.connect('finished', self.recognizer_finished)
  76. print("Using Options: ", self.options)
  77. def read_commands(self):
  78. # Read the commands file
  79. file_lines = open(command_file)
  80. strings = open(strings_file, "w")
  81. for line in file_lines:
  82. print(line)
  83. # Trim the white spaces
  84. line = line.strip()
  85. # If the line has length and the first char isn't a hash
  86. if len(line) and line[0]!="#":
  87. # This is a parsible line
  88. (key, value) = line.split(":", 1)
  89. print(key, value)
  90. self.commands[key.strip().lower()] = value.strip()
  91. strings.write( key.strip()+"\n")
  92. # Close the strings file
  93. strings.close()
  94. def load_options(self):
  95. """If possible, load options from the options.yaml file"""
  96. # Is there an opt file?
  97. try:
  98. opt_fh = open(opt_file)
  99. text = opt_fh.read()
  100. self.options = yaml.load(text)
  101. except:
  102. # Do nothing if the options file cannot be loaded
  103. pass
  104. def log_history(self, text):
  105. if self.options['history']:
  106. self.history.append(text)
  107. if len(self.history) > self.options['history']:
  108. # Pop off the first item
  109. self.history.pop(0)
  110. # Open and truncate the blather history file
  111. hfile = open(history_file, "w")
  112. for line in self.history:
  113. hfile.write( line+"\n")
  114. # Close the file
  115. hfile.close()
  116. def update_language(self):
  117. """Update the language if its hash has changed"""
  118. try:
  119. # Load the stored hash from the hash file
  120. try:
  121. with open(hash_file, 'r') as f:
  122. text = f.read()
  123. hashes = yaml.load(text)
  124. stored_hash = hashes['language']
  125. except (IOError, KeyError, TypeError):
  126. # No stored hash
  127. stored_hash = ''
  128. # Calculate the hash the language file has right now
  129. hasher = hashlib.sha256()
  130. with open(strings_file, 'rb') as sfile:
  131. buf = sfile.read()
  132. hasher.update(buf)
  133. new_hash = hasher.hexdigest()
  134. # If the hashes differ
  135. if stored_hash != new_hash:
  136. # Update the language
  137. # FIXME: Do this with Python, not Bash
  138. self.run_command('./language_updater.sh')
  139. # Store the new hash
  140. new_hashes = {'language': new_hash}
  141. with open(hash_file, 'w') as f:
  142. f.write(yaml.dump(new_hashes))
  143. except Exception as e:
  144. # Do nothing if the hash file cannot be loaded
  145. # FIXME: This is kind of bad; maybe YAML should be mandatory.
  146. print('error updating language')
  147. print(e)
  148. pass
  149. def run_command(self, cmd):
  150. """Print the command, then run it"""
  151. print(cmd)
  152. subprocess.call(cmd, shell=True)
  153. def recognizer_finished(self, recognizer, text):
  154. t = text.lower()
  155. # Is there a matching command?
  156. if t in self.commands:
  157. # Run the valid_sentence_command if there is a valid sentence command
  158. if self.options['valid_sentence_command']:
  159. subprocess.call(self.options['valid_sentence_command'], shell=True)
  160. cmd = self.commands[t]
  161. # Should we be passing words?
  162. if self.options['pass_words']:
  163. cmd += " " + t
  164. self.run_command(cmd)
  165. else:
  166. self.run_command(cmd)
  167. self.log_history(text)
  168. else:
  169. # Run the invalid_sentence_command if there is an invalid sentence command
  170. if self.options['invalid_sentence_command']:
  171. subprocess.call(self.options['invalid_sentence_command'], shell=True)
  172. print("no matching command {0}".format(t))
  173. # If there is a UI and we are not continuous listen
  174. if self.ui:
  175. if not self.continuous_listen:
  176. # Stop listening
  177. self.recognizer.pause()
  178. # Let the UI know that there is a finish
  179. self.ui.finished(t)
  180. def run(self):
  181. if self.ui:
  182. self.ui.run()
  183. else:
  184. blather.recognizer.listen()
  185. def quit(self):
  186. sys.exit()
  187. def process_command(self, UI, command):
  188. print(command)
  189. if command == "listen":
  190. self.recognizer.listen()
  191. elif command == "stop":
  192. self.recognizer.pause()
  193. elif command == "continuous_listen":
  194. self.continuous_listen = True
  195. self.recognizer.listen()
  196. elif command == "continuous_stop":
  197. self.continuous_listen = False
  198. self.recognizer.pause()
  199. elif command == "quit":
  200. self.quit()
  201. def load_resource(self, string):
  202. local_data = os.path.join(os.path.dirname(__file__), 'data')
  203. paths = ["/usr/share/blather/", "/usr/local/share/blather", local_data]
  204. for path in paths:
  205. resource = os.path.join(path, string)
  206. if os.path.exists( resource ):
  207. return resource
  208. # If we get this far, no resource was found
  209. return False
  210. if __name__ == "__main__":
  211. parser = OptionParser()
  212. parser.add_option("-i", "--interface", type="string", dest="interface",
  213. action='store',
  214. help="Interface to use (if any). 'g' for GTK or 'gt' for GTK system tray icon")
  215. parser.add_option("-c", "--continuous",
  216. action="store_true", dest="continuous", default=False,
  217. help="starts interface with 'continuous' listen enabled")
  218. parser.add_option("-p", "--pass-words",
  219. action="store_true", dest="pass_words", default=False,
  220. help="passes the recognized words as arguments to the shell command")
  221. parser.add_option("-H", "--history", type="int",
  222. action="store", dest="history",
  223. help="number of commands to store in history file")
  224. parser.add_option("-m", "--microphone", type="int",
  225. action="store", dest="microphone", default=None,
  226. help="Audio input card to use (if other than system default)")
  227. parser.add_option("--valid-sentence-command", type="string", dest="valid_sentence_command",
  228. action='store',
  229. help="command to run when a valid sentence is detected")
  230. parser.add_option( "--invalid-sentence-command", type="string", dest="invalid_sentence_command",
  231. action='store',
  232. help="command to run when an invalid sentence is detected")
  233. (options, args) = parser.parse_args()
  234. # Make our blather object
  235. blather = Blather(options)
  236. # Init gobject threads
  237. GObject.threads_init()
  238. # We want a main loop
  239. main_loop = GObject.MainLoop()
  240. # Handle sigint
  241. signal.signal(signal.SIGINT, signal.SIG_DFL)
  242. # Run the blather
  243. blather.run()
  244. # Start the main loop
  245. try:
  246. main_loop.run()
  247. except:
  248. print("time to quit")
  249. main_loop.quit()
  250. sys.exit()