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

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