Pythonの音声モジュール

win32com.clientインポート定数からの
インポートのOS
インポートwin32com.client
輸入pythoncomの

クラスの音声認識:
デフ__init __(自己、wordsToAdd):
self.speaker = win32com.client.Dispatch( "SAPI.SpVoice")
self.listener = win32com.client.Dispatch ( "SAPI.SpSharedRecognizer")
self.context = self.listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.wordsRule = self.grammar.Rules.Add(」 wordsRule」、constants.SRATopLevel + constants.SRADynamic、0)
self.wordsRule.Clear()
[wordsToAddにおける単語のself.wordsRule.InitialState.AddWordTransition(なし、単語)]
self.grammar.Rules.Commit()
self.grammar.CmdSetRuleState( "wordsRule"、1)
self.grammar.Rules.Commit()
self.eventHandler = ContextEvents(self.context)
self.say( "正常に開始されました")
デフ(自己、フレーズ)と言う:
self.speaker.Speak(フレーズ)
クラスContextEvents(win32com.client.getevents( "SAPI.SpSharedRecoContext")):
デフOnRecognition(自己、StreamNumber、StreamPosition、RecognitionType、結果):
newResult = win32com。 client.Dispatch(結果)
プリント( "说:"、newResult.PhraseInfo.GetText())
S = newResult.PhraseInfo.GetText()
であればS == "记事本":
os.system( "メモ帳")

もし__name__ == '__main__':
wordsToAdd = [ "オフ"、 "シャットダウンキャンセル"、 "メモ帳"、 "描画ボード"、 "錠剤"、 "設定"、 "メモ帳を閉じ"]
speechReco =音声認識(wordsToAdd)
一方TRUE:
pythoncom.PumpWaitingMessages( )

おすすめ

転載: www.cnblogs.com/pygo/p/12274671.html