Das ist f├╝rs Erste mal bei rausgekommen:
Code: Select all
from win32com.client import constants
import win32com.client
import voice
import eg
import wx
class SpeechRecognition(eg.PluginClass):
def __init__(self):
try:
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
except:
eg.PrintError ("Cannot create SpeechRecognition object")
return
maingroup = self.AddGroup(self.name)
maingroup.AddAction(self.SpeechRules)
self.context = self.listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.eventHandler = ContextEvents(self.context, self)
self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
def __start__(self):
try:
self.grammar.CmdSetRuleState("wordsRule", 1)
except:
pass
def __stop__(self):
self.grammar.CmdSetRuleState("wordsRule", 0)
def __close__(self):
self.listener = None
self.eventHandler = None
self.grammar = None
self.wordsRule = None
class SpeechRules(eg.ActionClass):
def __call__(self, Commands, Rule):
wordsRule = self.plugin.wordsRule
grammar = self.plugin.grammar
wordsRule.Clear()
[wordsRule.InitialState.AddWordTransition(None, word)for word in Commands ]
grammar.Rules.Commit()
grammar.CmdSetRuleState("wordsRule", 1)
grammar.Rules.Commit()
def GetLabel(self, Commands, Rule):
return "SpeechRecognition: %s" % Rule
def SetupConfigDialog(self, dialog, Commands=["hello"], Rule="Rule1"):
sizer = dialog.sizer
mySizer = wx.FlexGridSizer(cols=2)
desc1 = wx.StaticText(dialog, -1, "Rule Name:")
mySizer.Add(desc1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
Name = wx.TextCtrl(dialog, -1,Rule)
mySizer.Add(Name, 0, wx.EXPAND|wx.ALL, 5)
Edit = wx.TextCtrl(dialog, -1,)
mySizer.Add(Edit, 0, wx.EXPAND|wx.ALL, 5)
CommandsEdit = wx.ListBox(dialog, -1, choices=Commands, style=wx.LB_SINGLE)
mySizer.Add(CommandsEdit, 0, wx.EXPAND|wx.ALL, 5)
button1 = wx.Button(dialog, -1, label="Add Command")
def OnButton1(event):
newcommand = Edit.GetValue()
if len(newcommand)> 0:
CommandsEdit.Append(newcommand)
Edit.Clear()
button1.Bind(wx.EVT_BUTTON, OnButton1)
mySizer.Add(button1, 0, wx.EXPAND|wx.ALL, 5)
button2 = wx.Button(dialog, -1, label="Delete Command")
def OnButton2(event):
try:
CommandsEdit.Delete(CommandsEdit.GetSelection())
except:
pass
button2.Bind(wx.EVT_BUTTON, OnButton2)
mySizer.Add(button2, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(mySizer, 1, wx.EXPAND)
def ReturnResult():
Commands =[]
for i in range (CommandsEdit.GetCount()):
Commands.append(CommandsEdit.GetString(i))
return (Commands, Name.GetValue(),)
return ReturnResult
class ContextEventsBase(win32com.client.getevents("SAPI.SpSharedRecoContext")):
pass
class ContextEvents(ContextEventsBase):
def __init__(self, context, plugin):
self.plugin = plugin
ContextEventsBase.__init__(self, context)
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
newResult = win32com.client.Dispatch(Result)
self.plugin.TriggerEvent(newResult.PhraseInfo.GetText())
Man kann also f├╝r jede Gruppe ob z.B. Musik oder Video eine eigene
Sprachregel erstellen, was die Erkennungsgenauigkeit sehr erh├Âht.