简体   繁体   中英

Azure Speech SDK | Python | Implementing Keyword Recognition

I am trying to create a custom wakeword using Azure cognitive services and Python. I am following the quickstart tutorial -

azure quickstart

I have generated the keyword model using the speech studio and now I am trying to implement it in Python. The quickstart has C# example where in it is using CognitiveServices.Speech, CognitiveServices.Speech.Audio. .NET has KeywordRecognizer class that implements the keyword recognition.

In Python, there is no KeywordRecognizer class, however there is a Recognizer , it has start_keyword_recognition method.

Initially I used it as below -

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

When I executed it, I got the below error -

AttributeError: 'Recognizer' object has no attribute '_impl'

when I referred to speech.py, it has the following implementation of keyword-recognition -

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)

The recognizer class has static method that returns _impl, but it uses _from_config method, which I am not able to locate in speech.py.

  1. Can we use Recognizer class and start_keyword_recognition method out of box.
  2. If not, please provide me any pointers on how I can implement it.

Please let me know if more details are required.

 @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 team has uploaded samples for almost all cases and I got the solution from there.

Code Snippet from Github site -

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()

The link to github site is -

Github Azure Samples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM