OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Azure keyword recognition model (`.table`) not working when feeding it wave files

  • Thread starter Thread starter Mike B
  • Start date Start date
M

Mike B

Guest
I have the following script:

Code:
import time
import azure.cognitiveservices.speech as speechsdk
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

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


def recognize_keyword_from_wav_file(wav_file_path):
    """Performs keyword-triggered speech recognition with a WAV file."""
    global true_positives, false_positives, false_negatives

    try:
        speech_config = speechsdk.SpeechConfig(subscription='xyz', region='westeurope')
        model = speechsdk.KeywordRecognitionModel("./keyword.table")
        audio_config = speechsdk.audio.AudioConfig(filename=wav_file_path)
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    except Exception as e:
        logging.error(f"Failed to initialize speech recognizer: {e}")
        return

    def recognizing_cb(evt):
        """Callback for recognizing event."""
        
        try:
            if evt.result.reason == speechsdk.ResultReason.RecognizingKeyword:
                logging.info(f'RECOGNIZING KEYWORD: {evt}')
            elif evt.result.reason == speechsdk.ResultReason.RecognizingSpeech:
                logging.info(f'RECOGNIZING: {evt}')
        except Exception as e:
            logging.error(f"Error in recognizing callback: {e}")

    def recognized_cb(evt):
        """Callback for recognized event."""
        
        try:
            if evt.result.reason == speechsdk.ResultReason.RecognizedKeyword:
                logging.info(f'RECOGNIZED KEYWORD: {evt}')
            elif evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
                logging.info(f'RECOGNIZED: {evt}')
        except Exception as e:
            logging.error(f"Error in recognized callback: {e}")

    try:
        speech_recognizer.recognizing.connect(recognizing_cb)
        speech_recognizer.recognized.connect(recognized_cb)
        speech_recognizer.session_started.connect(lambda evt: logging.info(f'SESSION STARTED: {evt}'))
        speech_recognizer.session_stopped.connect(lambda evt: logging.info(f'SESSION STOPPED {evt}'))
        speech_recognizer.canceled.connect(lambda evt: logging.info(f'CANCELED {evt}'))

        speech_recognizer.start_keyword_recognition(model)
        logging.info(f'Say something starting with "{KEYWORD}" followed by whatever you want...')

        speech_recognizer.recognize_once()

        speech_recognizer.stop_keyword_recognition()
    except Exception as e:
        logging.error(f"Error during speech recognition: {e}")


# Example usage:
if __name__ == "__main__":
    wav_file_path = "./output01.wav"
    recognize_keyword_from_wav_file(wav_file_path)

This is only giving me RecognizedSpeech but never RecognizedKeyword. This happens when using wave files as input to the keyword recognition model instead of the default microphone streamed by: audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True), which works fine. Any ideas?
<p>I have the following script:</p>
<pre class="lang-py prettyprint-override"><code>import time
import azure.cognitiveservices.speech as speechsdk
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

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


def recognize_keyword_from_wav_file(wav_file_path):
"""Performs keyword-triggered speech recognition with a WAV file."""
global true_positives, false_positives, false_negatives

try:
speech_config = speechsdk.SpeechConfig(subscription='xyz', region='westeurope')
model = speechsdk.KeywordRecognitionModel("./keyword.table")
audio_config = speechsdk.audio.AudioConfig(filename=wav_file_path)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
except Exception as e:
logging.error(f"Failed to initialize speech recognizer: {e}")
return

def recognizing_cb(evt):
"""Callback for recognizing event."""

try:
if evt.result.reason == speechsdk.ResultReason.RecognizingKeyword:
logging.info(f'RECOGNIZING KEYWORD: {evt}')
elif evt.result.reason == speechsdk.ResultReason.RecognizingSpeech:
logging.info(f'RECOGNIZING: {evt}')
except Exception as e:
logging.error(f"Error in recognizing callback: {e}")

def recognized_cb(evt):
"""Callback for recognized event."""

try:
if evt.result.reason == speechsdk.ResultReason.RecognizedKeyword:
logging.info(f'RECOGNIZED KEYWORD: {evt}')
elif evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
logging.info(f'RECOGNIZED: {evt}')
except Exception as e:
logging.error(f"Error in recognized callback: {e}")

try:
speech_recognizer.recognizing.connect(recognizing_cb)
speech_recognizer.recognized.connect(recognized_cb)
speech_recognizer.session_started.connect(lambda evt: logging.info(f'SESSION STARTED: {evt}'))
speech_recognizer.session_stopped.connect(lambda evt: logging.info(f'SESSION STOPPED {evt}'))
speech_recognizer.canceled.connect(lambda evt: logging.info(f'CANCELED {evt}'))

speech_recognizer.start_keyword_recognition(model)
logging.info(f'Say something starting with "{KEYWORD}" followed by whatever you want...')

speech_recognizer.recognize_once()

speech_recognizer.stop_keyword_recognition()
except Exception as e:
logging.error(f"Error during speech recognition: {e}")


# Example usage:
if __name__ == "__main__":
wav_file_path = "./output01.wav"
recognize_keyword_from_wav_file(wav_file_path)
</code></pre>
<p>This is only giving me <code>RecognizedSpeech</code> but never <code>RecognizedKeyword</code>. This happens when using wave files as input to the keyword recognition model instead of the default microphone streamed by: <code>audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)</code>, which works fine. Any ideas?</p>
 

Latest posts

Top