Somewhat fancy voice command recognition software
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. strings = open(self.config.strings_file, "w")
  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. # Close the strings file
  68. strings.close()
  69. def log_history(self, text):
  70. if self.options['history']:
  71. self.history.append(text)
  72. if len(self.history) > self.options['history']:
  73. # Pop off the first item
  74. self.history.pop(0)
  75. # Open and truncate the history file
  76. hfile = open(self.config.history_file, "w")
  77. for line in self.history:
  78. hfile.write(line + "\n")
  79. # Close the file
  80. hfile.close()
  81. def run_command(self, cmd):
  82. """Print the command, then run it"""
  83. print(cmd)
  84. subprocess.call(cmd, shell=True)
  85. def recognizer_finished(self, recognizer, text):
  86. t = text.lower()
  87. numt, nums = self.number_parser.parse_all_numbers(t)
  88. # Is there a matching command?
  89. if t in self.commands:
  90. # Run the valid_sentence_command if it's set
  91. if self.options['valid_sentence_command']:
  92. subprocess.call(self.options['valid_sentence_command'],
  93. shell=True)
  94. cmd = self.commands[t]
  95. # Should we be passing words?
  96. if self.options['pass_words']:
  97. cmd += " " + t
  98. self.run_command(cmd)
  99. self.log_history(text)
  100. elif numt in self.commands:
  101. # Run the valid_sentence_command if it's set
  102. if self.options['valid_sentence_command']:
  103. subprocess.call(self.options['valid_sentence_command'],
  104. shell=True)
  105. cmd = self.commands[numt]
  106. cmd = cmd.format(*nums)
  107. # Should we be passing words?
  108. if self.options['pass_words']:
  109. cmd += " " + t
  110. self.run_command(cmd)
  111. self.log_history(text)
  112. else:
  113. # Run the invalid_sentence_command if it's set
  114. if self.options['invalid_sentence_command']:
  115. subprocess.call(self.options['invalid_sentence_command'],
  116. shell=True)
  117. print("no matching command {0}".format(t))
  118. # If there is a UI and we are not continuous listen
  119. if self.ui:
  120. if not self.continuous_listen:
  121. # Stop listening
  122. self.recognizer.pause()
  123. # Let the UI know that there is a finish
  124. self.ui.finished(t)
  125. def run(self):
  126. if self.ui:
  127. self.ui.run()
  128. else:
  129. self.recognizer.listen()
  130. def quit(self):
  131. sys.exit()
  132. def process_command(self, UI, command):
  133. print(command)
  134. if command == "listen":
  135. self.recognizer.listen()
  136. elif command == "stop":
  137. self.recognizer.pause()
  138. elif command == "continuous_listen":
  139. self.continuous_listen = True
  140. self.recognizer.listen()
  141. elif command == "continuous_stop":
  142. self.continuous_listen = False
  143. self.recognizer.pause()
  144. elif command == "quit":
  145. self.quit()
  146. def load_resource(self, string):
  147. local_data = os.path.join(os.path.dirname(__file__), 'data')
  148. paths = ["/usr/share/kaylee/", "/usr/local/share/kaylee", local_data]
  149. for path in paths:
  150. resource = os.path.join(path, string)
  151. if os.path.exists(resource):
  152. return resource
  153. # If we get this far, no resource was found
  154. return False
  155. if __name__ == "__main__":
  156. # Make our kaylee object
  157. kaylee = Kaylee()
  158. # Init gobject threads
  159. GObject.threads_init()
  160. # We want a main loop
  161. main_loop = GObject.MainLoop()
  162. # Handle sigint
  163. signal.signal(signal.SIGINT, signal.SIG_DFL)
  164. # Run the kaylee
  165. kaylee.run()
  166. # Start the main loop
  167. try:
  168. main_loop.run()
  169. except:
  170. print("time to quit")
  171. main_loop.quit()
  172. sys.exit()