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.

kaylee.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. # This is part of Kaylee
  3. # -- this code is licensed GPLv3
  4. # Copyright 2015-2016 Clayton G. Hobbs
  5. # Portions Copyright 2013 Jezra
  6. from __future__ import print_function
  7. import sys
  8. import signal
  9. import hashlib
  10. import os.path
  11. import subprocess
  12. from gi.repository import GObject, GLib
  13. import json
  14. from recognizer import Recognizer
  15. from config import Config
  16. from languageupdater import LanguageUpdater
  17. from numberparser import NumberParser
  18. class Kaylee:
  19. def __init__(self):
  20. self.ui = None
  21. self.options = {}
  22. ui_continuous_listen = False
  23. self.continuous_listen = False
  24. self.commands = {}
  25. # Load configuration
  26. self.config = Config()
  27. self.options = vars(self.config.options)
  28. self.commands = self.options['commands']
  29. # Create number parser for later use
  30. self.number_parser = NumberParser()
  31. # Create the strings file
  32. self.create_strings_file()
  33. if self.options['interface']:
  34. if self.options['interface'] == "g":
  35. from gtkui import UI
  36. elif self.options['interface'] == "gt":
  37. from gtktrayui import UI
  38. else:
  39. print("no GUI defined")
  40. sys.exit()
  41. self.ui = UI(self.options, self.options['continuous'])
  42. self.ui.connect("command", self.process_command)
  43. # Can we load the icon resource?
  44. icon = self.load_resource("icon.png")
  45. if icon:
  46. self.ui.set_icon_active_asset(icon)
  47. # Can we load the icon_inactive resource?
  48. icon_inactive = self.load_resource("icon_inactive.png")
  49. if icon_inactive:
  50. self.ui.set_icon_inactive_asset(icon_inactive)
  51. if self.options['history']:
  52. self.history = []
  53. # Update the language if necessary
  54. self.language_updater = LanguageUpdater(self.config)
  55. self.language_updater.update_language_if_changed()
  56. # Create the recognizer
  57. self.recognizer = Recognizer(self.config)
  58. self.recognizer.connect('finished', self.recognizer_finished)
  59. def create_strings_file(self):
  60. # Open the strings file
  61. strings = open(self.config.strings_file, "w")
  62. # Add command words to the corpus
  63. for voice_cmd in sorted(self.commands.keys()):
  64. strings.write(voice_cmd.strip().replace('%d', '') + "\n")
  65. # Add number words to the corpus
  66. for word in self.number_parser.number_words:
  67. strings.write(word + "\n")
  68. # Close the strings file
  69. strings.close()
  70. def log_history(self, text):
  71. if self.options['history']:
  72. self.history.append(text)
  73. if len(self.history) > self.options['history']:
  74. # Pop off the first item
  75. self.history.pop(0)
  76. # Open and truncate the history file
  77. hfile = open(self.config.history_file, "w")
  78. for line in self.history:
  79. hfile.write(line + "\n")
  80. # Close the file
  81. hfile.close()
  82. def run_command(self, cmd):
  83. """Print the command, then run it"""
  84. print(cmd)
  85. subprocess.call(cmd, shell=True)
  86. def recognizer_finished(self, recognizer, text):
  87. t = text.lower()
  88. numt, nums = self.number_parser.parse_all_numbers(t)
  89. # Is there a matching command?
  90. if t in self.commands:
  91. # Run the valid_sentence_command if it's set
  92. if self.options['valid_sentence_command']:
  93. subprocess.call(self.options['valid_sentence_command'],
  94. shell=True)
  95. cmd = self.commands[t]
  96. # Should we be passing words?
  97. if self.options['pass_words']:
  98. cmd += " " + t
  99. self.run_command(cmd)
  100. self.log_history(text)
  101. elif numt in self.commands:
  102. # Run the valid_sentence_command if it's set
  103. if self.options['valid_sentence_command']:
  104. subprocess.call(self.options['valid_sentence_command'],
  105. shell=True)
  106. cmd = self.commands[numt]
  107. cmd = cmd.format(*nums)
  108. # Should we be passing words?
  109. if self.options['pass_words']:
  110. cmd += " " + t
  111. self.run_command(cmd)
  112. self.log_history(text)
  113. else:
  114. # Run the invalid_sentence_command if it's set
  115. if self.options['invalid_sentence_command']:
  116. subprocess.call(self.options['invalid_sentence_command'],
  117. shell=True)
  118. print("no matching command {0}".format(t))
  119. # If there is a UI and we are not continuous listen
  120. if self.ui:
  121. if not self.continuous_listen:
  122. # Stop listening
  123. self.recognizer.pause()
  124. # Let the UI know that there is a finish
  125. self.ui.finished(t)
  126. def run(self):
  127. if self.ui:
  128. self.ui.run()
  129. else:
  130. self.recognizer.listen()
  131. def quit(self):
  132. sys.exit()
  133. def process_command(self, UI, command):
  134. print(command)
  135. if command == "listen":
  136. self.recognizer.listen()
  137. elif command == "stop":
  138. self.recognizer.pause()
  139. elif command == "continuous_listen":
  140. self.continuous_listen = True
  141. self.recognizer.listen()
  142. elif command == "continuous_stop":
  143. self.continuous_listen = False
  144. self.recognizer.pause()
  145. elif command == "quit":
  146. self.quit()
  147. def load_resource(self, string):
  148. local_data = os.path.join(os.path.dirname(__file__), 'data')
  149. paths = ["/usr/share/kaylee/", "/usr/local/share/kaylee", local_data]
  150. for path in paths:
  151. resource = os.path.join(path, string)
  152. if os.path.exists(resource):
  153. return resource
  154. # If we get this far, no resource was found
  155. return False
  156. if __name__ == "__main__":
  157. # Make our kaylee object
  158. kaylee = Kaylee()
  159. # Init gobject threads
  160. GObject.threads_init()
  161. # We want a main loop
  162. main_loop = GObject.MainLoop()
  163. # Handle sigint
  164. signal.signal(signal.SIGINT, signal.SIG_DFL)
  165. # Run the kaylee
  166. kaylee.run()
  167. # Start the main loop
  168. try:
  169. main_loop.run()
  170. except:
  171. print("time to quit")
  172. main_loop.quit()
  173. sys.exit()