from win32com.client import constants
import os
import win32com.client
import pythoncom
speaker = win32com.client.Dispatch("SAPI.SPVOICE")
class SpeechRecognition:
def __init__(self, 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()
[self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
self.grammar.Rules.Commit()
self.grammar.CmdSetRuleState("wordsRule", 1)
self.grammar.Rules.Commit()
self.eventHandler = ContextEvents(
Python 语音识别框架
最新推荐文章于 2024-07-28 16:58:34 发布
该博客介绍了一个使用Python和SAPI库实现的语音识别框架,能够识别特定词汇并作出相应的语音反馈。用户可以添加词汇,如“张三”、“你好”等,当识别到这些词汇时,程序会回应相应的语音消息。
摘要由CSDN通过智能技术生成