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

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