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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #where are the files?
  11. conf_dir = os.path.expanduser("~/.config/blather")
  12. lang_dir = os.path.join(conf_dir, "language")
  13. command_file = os.path.join(conf_dir, "commands.conf")
  14. strings_file = os.path.join(conf_dir, "sentences.corpus")
  15. history_file = os.path.join(conf_dir, "blather.history")
  16. lang_file = os.path.join(lang_dir,'lm')
  17. dic_file = os.path.join(lang_dir,'dic')
  18. #make the lang_dir if it doesn't exist
  19. if not os.path.exists(lang_dir):
  20. os.makedirs(lang_dir)
  21. class Blather:
  22. def __init__(self, opts):
  23. #import the recognizer so Gst doesn't clobber our -h
  24. from Recognizer import Recognizer
  25. self.ui = None
  26. #keep track of the opts
  27. self.opts = opts
  28. ui_continuous_listen = False
  29. self.continuous_listen = False
  30. self.commands = {}
  31. self.read_commands()
  32. self.recognizer = Recognizer(lang_file, dic_file, opts.microphone )
  33. self.recognizer.connect('finished',self.recognizer_finished)
  34. if opts.interface != None:
  35. if opts.interface == "q":
  36. #import the ui from qt
  37. from QtUI import UI
  38. elif opts.interface == "g":
  39. from GtkUI import UI
  40. else:
  41. print "no GUI defined"
  42. sys.exit()
  43. self.ui = UI(args,opts.continuous)
  44. self.ui.connect("command", self.process_command)
  45. #can we load the icon resource?
  46. icon = self.load_resource("icon.png")
  47. if icon:
  48. self.ui.set_icon(icon)
  49. if self.opts.history:
  50. self.history = []
  51. def read_commands(self):
  52. #read the.commands file
  53. file_lines = open(command_file)
  54. strings = open(strings_file, "w")
  55. for line in file_lines:
  56. print line
  57. #trim the white spaces
  58. line = line.strip()
  59. #if the line has length and the first char isn't a hash
  60. if len(line) and line[0]!="#":
  61. #this is a parsible line
  62. (key,value) = line.split(":",1)
  63. print key, value
  64. self.commands[key.strip().lower()] = value.strip()
  65. strings.write( key.strip()+"\n")
  66. #close the strings file
  67. strings.close()
  68. def log_history(self,text):
  69. if self.opts.history:
  70. self.history.append(text)
  71. if len(self.history) > self.opts.history:
  72. #pop off the first item
  73. self.history.pop(0)
  74. #open and truncate the blather history file
  75. hfile = open(history_file, "w")
  76. for line in self.history:
  77. hfile.write( line+"\n")
  78. #close the file
  79. hfile.close()
  80. def recognizer_finished(self, recognizer, text):
  81. t = text.lower()
  82. #is there a matching command?
  83. if self.commands.has_key( t ):
  84. cmd = self.commands[t]
  85. print cmd
  86. subprocess.call(cmd, shell=True)
  87. self.log_history(text)
  88. else:
  89. print "no matching command"
  90. #if there is a UI and we are not continuous listen
  91. if self.ui:
  92. if not self.continuous_listen:
  93. #stop listening
  94. self.recognizer.pause()
  95. #let the UI know that there is a finish
  96. self.ui.finished(t)
  97. def run(self):
  98. if self.ui:
  99. self.ui.run()
  100. else:
  101. blather.recognizer.listen()
  102. def quit(self):
  103. sys.exit()
  104. def process_command(self, UI, command):
  105. print command
  106. if command == "listen":
  107. self.recognizer.listen()
  108. elif command == "stop":
  109. self.recognizer.pause()
  110. elif command == "continuous_listen":
  111. self.continuous_listen = True
  112. self.recognizer.listen()
  113. elif command == "continuous_stop":
  114. self.continuous_listen = False
  115. self.recognizer.pause()
  116. elif command == "quit":
  117. self.quit()
  118. def load_resource(self,string):
  119. local_data = os.path.join(os.path.dirname(__file__), 'data')
  120. paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
  121. for path in paths:
  122. resource = os.path.join(path, string)
  123. if os.path.exists( resource ):
  124. return resource
  125. #if we get this far, no resource was found
  126. return False
  127. if __name__ == "__main__":
  128. parser = OptionParser()
  129. parser.add_option("-i", "--interface", type="string", dest="interface",
  130. action='store',
  131. help="Interface to use (if any). 'q' for Qt, 'g' for GTK")
  132. parser.add_option("-c", "--continuous",
  133. action="store_true", dest="continuous", default=False,
  134. help="starts interface with 'continuous' listen enabled")
  135. parser.add_option("-H", "--history", type="int",
  136. action="store", dest="history",
  137. help="number of commands to store in history file")
  138. parser.add_option("-m", "--microphone", type="int",
  139. action="store", dest="microphone", default=None,
  140. help="Audio input card to use (if other than system default)")
  141. (options, args) = parser.parse_args()
  142. #make our blather object
  143. blather = Blather(options)
  144. #init gobject threads
  145. gobject.threads_init()
  146. #we want a main loop
  147. main_loop = gobject.MainLoop()
  148. #handle sigint
  149. signal.signal(signal.SIGINT, signal.SIG_DFL)
  150. #run the blather
  151. blather.run()
  152. #start the main loop
  153. try:
  154. main_loop.run()
  155. except:
  156. print "time to quit"
  157. main_loop.quit()
  158. sys.exit()