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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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")
  14. strings_file = os.path.join(conf_dir, "sentences.corpus")
  15. lang_file = os.path.join(lang_dir,'lm')
  16. dic_file = os.path.join(lang_dir,'dic')
  17. #make the lang_dir if it doesn't exist
  18. if not os.path.exists(lang_dir):
  19. os.makedirs(lang_dir)
  20. class Blather:
  21. def __init__(self, opts):
  22. #import the recognizer so Gst doesn't clobber our -h
  23. from Recognizer import Recognizer
  24. self.ui = None
  25. ui_continuous_listen = False
  26. self.continuous_listen = False
  27. self.commands = {}
  28. self.read_commands()
  29. self.recognizer = Recognizer(lang_file, dic_file)
  30. self.recognizer.connect('finished',self.recognizer_finished)
  31. if opts.interface != None:
  32. if opts.interface == "q":
  33. #import the ui from qt
  34. from QtUI import UI
  35. elif opts.interface == "g":
  36. from GtkUI import UI
  37. else:
  38. print "no GUI defined"
  39. sys.exit()
  40. self.ui = UI(args,opts.continuous)
  41. self.ui.connect("command", self.process_command)
  42. def read_commands(self):
  43. #read the.commands file
  44. file_lines = open(command_file)
  45. strings = open(strings_file, "w")
  46. for line in file_lines:
  47. print line
  48. #trim the white spaces
  49. line = line.strip()
  50. #if the line has length and the first char isn't a hash
  51. if len(line) and line[0]!="#":
  52. #this is a parsible line
  53. (key,value) = line.split(":",1)
  54. print key, value
  55. self.commands[key.strip().lower()] = value.strip()
  56. strings.write( key.strip()+"\n")
  57. #close the strings file
  58. strings.close()
  59. def recognizer_finished(self, recognizer, text):
  60. t = text.lower()
  61. #is there a matching command?
  62. if self.commands.has_key( t ):
  63. cmd = self.commands[t]
  64. print cmd
  65. subprocess.call(cmd, shell=True)
  66. else:
  67. print "no matching command"
  68. #if there is a UI and we are not continuous listen
  69. if self.ui:
  70. if not self.continuous_listen:
  71. #stop listening
  72. self.recognizer.pause()
  73. #let the UI know that there is a finish
  74. self.ui.finished(t)
  75. def run(self):
  76. if self.ui:
  77. self.ui.run()
  78. else:
  79. blather.recognizer.listen()
  80. def quit(self):
  81. if self.ui:
  82. self.ui.quit()
  83. sys.exit()
  84. def process_command(self, UI, command):
  85. print command
  86. if command == "listen":
  87. self.recognizer.listen()
  88. elif command == "stop":
  89. self.recognizer.pause()
  90. elif command == "continuous_listen":
  91. self.continuous_listen = True
  92. self.recognizer.listen()
  93. elif command == "continuous_stop":
  94. self.continuous_listen = False
  95. self.recognizer.pause()
  96. elif command == "quit":
  97. self.quit()
  98. if __name__ == "__main__":
  99. parser = OptionParser()
  100. parser.add_option("-i", "--interface", type="string", dest="interface",
  101. action='store',
  102. help="Interface to use (if any). 'q' for Qt, 'g' for GTK")
  103. parser.add_option("-c", "--continuous",
  104. action="store_true", dest="continuous", default=False,
  105. help="starts interface with 'continuous' listen enabled")
  106. (options, args) = parser.parse_args()
  107. #make our blather object
  108. blather = Blather(options)
  109. #init gobject threads
  110. gobject.threads_init()
  111. #we want a main loop
  112. main_loop = gobject.MainLoop()
  113. #handle sigint
  114. signal.signal(signal.SIGINT, signal.SIG_DFL)
  115. #run the blather
  116. blather.run()
  117. #start the main loop
  118. try:
  119. main_loop.run()
  120. except:
  121. print "time to quit"
  122. main_loop.quit()
  123. sys.exit()