Azure语音SDK | Python |实施关键字识别

问题描述

我正在尝试使用Azure认知服务和Python创建自定义唤醒词。我正在关注快速入门教程-

azure quickstart

我已经使用语音工作室生成了关键字模型,现在我正尝试在Python中实现它。快速入门提供了C#示例,其中使用了CognitiveServices.Speech,CognitiveServices.Speech.Audio。 .NET具有实现关键字识别的KeywordRecognizer类。

在Python中,没有KeywordRecognizer类,但是有一个Recognizer,它具有start_keyword_recognition方法

最初,我按如下方式使用它-

keywordModel = speechsdk.KeywordRecognitionModel("hello_raven.table")
#audioConfig = audiosdk.AudioConfig(use_default_microphone = True)
keywordRecognizer = speechsdk.Recognizer()
result = keywordRecognizer.start_keyword_recognition(keywordModel)

执行它时,出现以下错误-

AttributeError:“识别器”对象没有属性“ _impl”

当我提到Speech.py​​时,它具有以下关键字识别实现-

def start_keyword_recognition(self,model: KeywordRecognitionModel):
    """
    Synchronously initiates keyword recognition operation.

    :param model: the keyword recognition model that specifies the keyword to be recognized.
    """
    return self._impl.start_keyword_recognition(model._impl)

识别器类具有返回_impl的静态方法,但是它使用_from_config方法,而我无法在Speech.py​​中找到它。

  1. 我们可以直接使用Recognizer类和start_keyword_recognition方法吗?
  2. 如果没有,请向我提供有关如何实现它的任何指示。

如果需要更多详细信息,请告诉我。

 @staticmethod
    def _get_impl(reco_type,speech_config,audio_config):
        if audio_config is not None:
            _impl = reco_type._from_config(speech_config._impl,audio_config._impl)
        else:
            _impl = reco_type._from_config(speech_config._impl,None)

        return _impl

解决方法

Azure团队已经上传了几乎所有案例的样本,我从那里得到了解决方案。

Github网站上的代码段-

speech_config = speechsdk.SpeechConfig(subscription=speech_key,region=service_region)

# Creates an instance of a keyword recognition model. Update this to
# point to the location of your keyword recognition model.
model = speechsdk.KeywordRecognitionModel("YourKeywordRecognitionModelFile.table")

# The phrase your keyword recognition model triggers on.
keyword = "YourKeyword"

speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

done = False

def stop_cb(evt):
    """callback that signals to stop continuous recognition upon receiving an event `evt`"""
..........
..........

# Start keyword recognition
speech_recognizer.start_keyword_recognition(model)
print('Say something starting with "{}" followed by whatever you want...'.format(keyword))
while not done:
    time.sleep(.5)

speech_recognizer.stop_keyword_recognition()

到github站点的链接是-

Github Azure Samples