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.

Blather.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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)
  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. if self.ui:
  104. self.ui.quit()
  105. sys.exit()
  106. def process_command(self, UI, command):
  107. print command
  108. if command == "listen":
  109. self.recognizer.listen()
  110. elif command == "stop":
  111. self.recognizer.pause()
  112. elif command == "continuous_listen":
  113. self.continuous_listen = True
  114. self.recognizer.listen()
  115. elif command == "continuous_stop":
  116. self.continuous_listen = False
  117. self.recognizer.pause()
  118. elif command == "quit":
  119. self.quit()
  120. def load_resource(self,string):
  121. local_data = os.path.join(os.path.dirname(__file__), 'data')
  122. paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
  123. for path in paths:
  124. resource = os.path.join(path, string)
  125. if os.path.exists( resource ):
  126. return resource
  127. #if we get this far, no resource was found
  128. return False
  129. if __name__ == "__main__":
  130. parser = OptionParser()
  131. parser.add_option("-i", "--interface", type="string", dest="interface",
  132. action='store',
  133. help="Interface to use (if any). 'q' for Qt, 'g' for GTK")
  134. parser.add_option("-c", "--continuous",
  135. action="store_true", dest="continuous", default=False,
  136. help="starts interface with 'continuous' listen enabled")
  137. parser.add_option("-H", "--history", type="int",
  138. action="store", dest="history",
  139. help="number of commands to store in history file")
  140. (options, args) = parser.parse_args()
  141. #make our blather object
  142. blather = Blather(options)
  143. #init gobject threads
  144. gobject.threads_init()
  145. #we want a main loop
  146. main_loop = gobject.MainLoop()
  147. #handle sigint
  148. signal.signal(signal.SIGINT, signal.SIG_DFL)
  149. #run the blather
  150. blather.run()
  151. #start the main loop
  152. try:
  153. main_loop.run()
  154. except:
  155. print "time to quit"
  156. main_loop.quit()
  157. sys.exit()