ich versuche gerade auf SAPI 4 (Microsoft Speech API) in Python zuzugreifen um ein Spracherekennungstool zu schreiben. Es sollte eigentlich alles funktionieren. Die Einbindung über ActiveX geht, nur der EventHandler für die Spracherkennung wird nicht gestartet. Die Parameterliste des Events OnPhraseFinish stimmt auch noch nicht. Eigentlich sollte es dort einen Fehler geben.
Vieleicht kann mir ja jemand von euch helfen.
Code: Alles auswählen
import win32com.client
from win32com.client import *
import pythoncom
import string
defaultNamedOptArg=pythoncom.Missing
defaultNamedNotOptArg=pythoncom.Missing
defaultUnnamedArg=pythoncom.Missing
gencache.EnsureModule("ActiveListen.ActiveListen", 0, 1, 0)
class dictation:
""" Initialize the speech recognition with the passed in list of words """
def __init__(self, eventClass=None):
# For text-to-speech
self.sr = win32com.client.DispatchWithEvents("ActiveListen.ActiveListen", eventClass)
self.sr.GrammarFromString("[Grammar] \n langid = 1033 \n type=dictation")
self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
# For speech recognition - first create a listener
self.say(self.sr.Grammars(1))
self.say("Started successfully")
"""Speak a word or phrase"""
def say(self, phrase):
self.speaker.Speak(phrase)
class ExampleSapiEvents:
def OnPhraseFinish(self):
newResult = win32com.client.Dispatch(Result)
print "You said: ",newResult.PhraseInfo.GetText()
def OnPhraseStart(self, hi=defaultNamedNotOptArg, lo=defaultNamedNotOptArg):
print "Hallo"
if __name__=='__main__':
wordsToAdd = [ "Hello", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"]
speechReco = dictation( ExampleSapiEvents)
while 1:
pythoncom.PumpWaitingMessages()