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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if self.opts.history:
  46. self.history = []
  47. def read_commands(self):
  48. #read the.commands file
  49. file_lines = open(command_file)
  50. strings = open(strings_file, "w")
  51. for line in file_lines:
  52. print line
  53. #trim the white spaces
  54. line = line.strip()
  55. #if the line has length and the first char isn't a hash
  56. if len(line) and line[0]!="#":
  57. #this is a parsible line
  58. (key,value) = line.split(":",1)
  59. print key, value
  60. self.commands[key.strip().lower()] = value.strip()
  61. strings.write( key.strip()+"\n")
  62. #close the strings file
  63. strings.close()
  64. def log_history(self,text):
  65. if self.opts.history:
  66. self.history.append(text)
  67. if len(self.history) > self.opts.history:
  68. #pop off the first item
  69. self.history.pop(0)
  70. #open and truncate the blather history file
  71. hfile = open(history_file, "w")
  72. for line in self.history:
  73. hfile.write( line+"\n")
  74. #close the file
  75. hfile.close()
  76. def recognizer_finished(self, recognizer, text):
  77. t = text.lower()
  78. #is there a matching command?
  79. if self.commands.has_key( t ):
  80. cmd = self.commands[t]
  81. print cmd
  82. subprocess.call(cmd, shell=True)
  83. self.log_history(text)
  84. else:
  85. print "no matching command"
  86. #if there is a UI and we are not continuous listen
  87. if self.ui:
  88. if not self.continuous_listen:
  89. #stop listening
  90. self.recognizer.pause()
  91. #let the UI know that there is a finish
  92. self.ui.finished(t)
  93. def run(self):
  94. if self.ui:
  95. self.ui.run()
  96. else:
  97. blather.recognizer.listen()
  98. def quit(self):
  99. if self.ui:
  100. self.ui.quit()
  101. sys.exit()
  102. def process_command(self, UI, command):
  103. print command
  104. if command == "listen":
  105. self.recognizer.listen()
  106. elif command == "stop":
  107. self.recognizer.pause()
  108. elif command == "continuous_listen":
  109. self.continuous_listen = True
  110. self.recognizer.listen()
  111. elif command == "continuous_stop":
  112. self.continuous_listen = False
  113. self.recognizer.pause()
  114. elif command == "quit":
  115. self.quit()
  116. if __name__ == "__main__":
  117. parser = OptionParser()
  118. parser.add_option("-i", "--interface", type="string", dest="interface",
  119. action='store',
  120. help="Interface to use (if any). 'q' for Qt, 'g' for GTK")
  121. parser.add_option("-c", "--continuous",
  122. action="store_true", dest="continuous", default=False,
  123. help="starts interface with 'continuous' listen enabled")
  124. parser.add_option("-H", "--history", type="int",
  125. action="store", dest="history",
  126. help="number of commands to store in history file")
  127. (options, args) = parser.parse_args()
  128. #make our blather object
  129. blather = Blather(options)
  130. #init gobject threads
  131. gobject.threads_init()
  132. #we want a main loop
  133. main_loop = gobject.MainLoop()
  134. #handle sigint
  135. signal.signal(signal.SIGINT, signal.SIG_DFL)
  136. #run the blather
  137. blather.run()
  138. #start the main loop
  139. try:
  140. main_loop.run()
  141. except:
  142. print "time to quit"
  143. main_loop.quit()
  144. sys.exit()