Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Blather.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/usr/bin/env python2
  2. # -- this code is licensed GPLv3
  3. # Copyright 2013 Jezra
  4. import sys
  5. import signal
  6. import gobject
  7. import os.path
  8. import subprocess
  9. from optparse import OptionParser
  10. try:
  11. import yaml
  12. except:
  13. print "YAML is not supported. ~/.config/blather/options.yaml will not function"
  14. #where are the files?
  15. conf_dir = os.path.expanduser("~/.config/blather")
  16. lang_dir = os.path.join(conf_dir, "language")
  17. command_file = os.path.join(conf_dir, "commands.conf")
  18. strings_file = os.path.join(conf_dir, "sentences.corpus")
  19. history_file = os.path.join(conf_dir, "blather.history")
  20. opt_file = os.path.join(conf_dir, "options.yaml")
  21. lang_file = os.path.join(lang_dir,'lm')
  22. dic_file = os.path.join(lang_dir,'dic')
  23. #make the lang_dir if it doesn't exist
  24. if not os.path.exists(lang_dir):
  25. os.makedirs(lang_dir)
  26. class Blather:
  27. def __init__(self, opts):
  28. #import the recognizer so Gst doesn't clobber our -h
  29. from Recognizer import Recognizer
  30. self.ui = None
  31. self.options = {}
  32. ui_continuous_listen = False
  33. self.continuous_listen = False
  34. self.commands = {}
  35. #read the commands
  36. self.read_commands()
  37. #load the options file
  38. self.load_options()
  39. #merge the opts
  40. for k,v in opts.__dict__.items():
  41. if (not k in self.options) or opts.override:
  42. self.options[k] = v
  43. if self.options['interface'] != None:
  44. if self.options['interface'] == "q":
  45. from QtUI import UI
  46. elif self.options['interface'] == "g":
  47. from GtkUI import UI
  48. elif self.options['interface'] == "gt":
  49. from GtkTrayUI import UI
  50. else:
  51. print "no GUI defined"
  52. sys.exit()
  53. self.ui = UI(args, self.options['continuous'])
  54. self.ui.connect("command", self.process_command)
  55. #can we load the icon resource?
  56. icon = self.load_resource("icon.png")
  57. if icon:
  58. self.ui.set_icon_active_asset(icon)
  59. #can we load the icon_inactive resource?
  60. icon_inactive = self.load_resource("icon_inactive.png")
  61. if icon_inactive:
  62. self.ui.set_icon_inactive_asset(icon_inactive)
  63. if self.options['history']:
  64. self.history = []
  65. #create the recognizer
  66. self.recognizer = Recognizer(lang_file, dic_file, self.options['microphone'] )
  67. self.recognizer.connect('finished',self.recognizer_finished)
  68. print "Using Options: ", self.options
  69. def read_commands(self):
  70. #read the.commands file
  71. file_lines = open(command_file)
  72. strings = open(strings_file, "w")
  73. for line in file_lines:
  74. print line
  75. #trim the white spaces
  76. line = line.strip()
  77. #if the line has length and the first char isn't a hash
  78. if len(line) and line[0]!="#":
  79. #this is a parsible line
  80. (key,value) = line.split(":",1)
  81. print key, value
  82. self.commands[key.strip().lower()] = value.strip()
  83. strings.write( key.strip()+"\n")
  84. #close the strings file
  85. strings.close()
  86. def load_options(self):
  87. #is there an opt file?
  88. try:
  89. opt_fh = open(opt_file)
  90. text = opt_fh.read()
  91. self.options = yaml.load(text)
  92. except:
  93. pass
  94. def log_history(self,text):
  95. if self.options['history']:
  96. self.history.append(text)
  97. if len(self.history) > self.options['history']:
  98. #pop off the first item
  99. self.history.pop(0)
  100. #open and truncate the blather history file
  101. hfile = open(history_file, "w")
  102. for line in self.history:
  103. hfile.write( line+"\n")
  104. #close the file
  105. hfile.close()
  106. # Print the cmd and then run the command
  107. def run_command(self, cmd):
  108. print cmd
  109. subprocess.call(cmd, shell=True)
  110. def recognizer_finished(self, recognizer, text):
  111. t = text.lower()
  112. #is there a matching command?
  113. if self.commands.has_key( t ):
  114. #run the valid_sentence_command if there is a valid sentence command
  115. if self.options['valid_sentence_command']:
  116. subprocess.call(self.options['valid_sentence_command'], shell=True)
  117. cmd = self.commands[t]
  118. #should we be passing words?
  119. if self.options['pass_words']:
  120. cmd+=" "+t
  121. self.run_command(cmd)
  122. else:
  123. self.run_command(cmd)
  124. self.log_history(text)
  125. else:
  126. #run the invalid_sentence_command if there is a valid sentence command
  127. if self.options['invalid_sentence_command']:
  128. subprocess.call(self.options['invalid_sentence_command'], shell=True)
  129. print "no matching command %s" %(t)
  130. #if there is a UI and we are not continuous listen
  131. if self.ui:
  132. if not self.continuous_listen:
  133. #stop listening
  134. self.recognizer.pause()
  135. #let the UI know that there is a finish
  136. self.ui.finished(t)
  137. def run(self):
  138. if self.ui:
  139. self.ui.run()
  140. else:
  141. blather.recognizer.listen()
  142. def quit(self):
  143. sys.exit()
  144. def process_command(self, UI, command):
  145. print command
  146. if command == "listen":
  147. self.recognizer.listen()
  148. elif command == "stop":
  149. self.recognizer.pause()
  150. elif command == "continuous_listen":
  151. self.continuous_listen = True
  152. self.recognizer.listen()
  153. elif command == "continuous_stop":
  154. self.continuous_listen = False
  155. self.recognizer.pause()
  156. elif command == "quit":
  157. self.quit()
  158. def load_resource(self,string):
  159. local_data = os.path.join(os.path.dirname(__file__), 'data')
  160. paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
  161. for path in paths:
  162. resource = os.path.join(path, string)
  163. if os.path.exists( resource ):
  164. return resource
  165. #if we get this far, no resource was found
  166. return False
  167. if __name__ == "__main__":
  168. parser = OptionParser()
  169. parser.add_option("-i", "--interface", type="string", dest="interface",
  170. action='store',
  171. help="Interface to use (if any). 'q' for Qt, 'g' for GTK, 'gt' for GTK system tray icon")
  172. parser.add_option("-c", "--continuous",
  173. action="store_true", dest="continuous", default=False,
  174. help="starts interface with 'continuous' listen enabled")
  175. parser.add_option("-p", "--pass-words",
  176. action="store_true", dest="pass_words", default=False,
  177. help="passes the recognized words as arguments to the shell command")
  178. parser.add_option("-o", "--override",
  179. action="store_true", dest="override", default=False,
  180. help="override config file with command line options")
  181. parser.add_option("-H", "--history", type="int",
  182. action="store", dest="history",
  183. help="number of commands to store in history file")
  184. parser.add_option("-m", "--microphone", type="int",
  185. action="store", dest="microphone", default=None,
  186. help="Audio input card to use (if other than system default)")
  187. parser.add_option("--valid-sentence-command", type="string", dest="valid_sentence_command",
  188. action='store',
  189. help="command to run when a valid sentence is detected")
  190. parser.add_option( "--invalid-sentence-command", type="string", dest="invalid_sentence_command",
  191. action='store',
  192. help="command to run when an invalid sentence is detected")
  193. (options, args) = parser.parse_args()
  194. #make our blather object
  195. blather = Blather(options)
  196. #init gobject threads
  197. gobject.threads_init()
  198. #we want a main loop
  199. main_loop = gobject.MainLoop()
  200. #handle sigint
  201. signal.signal(signal.SIGINT, signal.SIG_DFL)
  202. #run the blather
  203. blather.run()
  204. #start the main loop
  205. try:
  206. main_loop.run()
  207. except:
  208. print "time to quit"
  209. main_loop.quit()
  210. sys.exit()