Stop continuous speech recognition from microphone

erjvd 0 Reputation points
2024-07-31T13:04:03.96+00:00

Dear,

I am currently trying to write a python script that recognizes speech from a microphone using continuous recognition. I used the sample code from the Azure speech service (https://learn.microsoft.com/en-us/azure/ai-services/speech-service/how-to-recognize-speech?pivots=programming-language-python). However, my program never exits the while loop. How is the configuration of the 'stop_continuous_recognition' function? Which action stops the recognition? Thank you!

import time
from dotenv import dotenv_values
import azure.cognitiveservices.speech as speechsdk

def recognised_speech(evt):
    print(f"You: {evt.result.text}")

def cont_speech_to_text():
    done_talking=False
    
    def stop_cb(evt):
        print('You: {}'.format(evt))
        nonlocal done_talking
        done_talking = True
        speech_recognizer.stop_continuous_recognition()

    #speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
    speech_recognizer.recognized.connect(recognised_speech)
    speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
    speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt)))
    speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))

    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(stop_cb)

    speech_recognizer.start_continuous_recognition()
    while not done_talking:
        time.sleep(.5)

# Configure the Azure Speech
SPEECH_REGION = "westeurope"
keypath="..."
speechkey=dotenv_values(keypath+".key")
speech_config = speechsdk.SpeechConfig(subscription=speechkey['KEY'], region=SPEECH_REGION)

# Set the language and azure speech parameters
speech_config.speech_recognition_language="en-US"
speech_config.speech_synthesis_voice_name='en-US-AvaMultilingualNeural'
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

cont_speech_to_text()
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
2,065 questions
{count} votes

1 answer

Sort by: Most helpful
  1. VasaviLankipalle-MSFT 18,676 Reputation points Moderator
    2024-07-31T21:25:32.0266667+00:00

    Hello @erjvd , Thanks for using Microsoft Q&A Platform.

    Yes, you use continuous recognition when you want to control when to stop recognizing. It requires you to connect to EventSignal to get the recognition results. To stop recognition, you must call stop_continuous_recognition() or stop_continuous_recognition().

    Here is the sample code, please refer to this GitHub for additional scenarios: https://github.com/Azure-Samples/cognitive-services-speech-sdk/blob/76a75e0248c941314efd62ed59b32fe566034121/samples/python/console/speech_sample.py#L359

    def speech_recognize_continuous_async_from_microphone():
        """performs continuous speech recognition asynchronously with input from microphone"""
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
        # The default language is "en-us".
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
        done = False
        def recognizing_cb(evt: speechsdk.SpeechRecognitionEventArgs):
            print('RECOGNIZING: {}'.format(evt))
        def recognized_cb(evt: speechsdk.SpeechRecognitionEventArgs):
            print('RECOGNIZED: {}'.format(evt))
        def stop_cb(evt: speechsdk.SessionEventArgs):
            """callback that signals to stop continuous recognition"""
            print('CLOSING on {}'.format(evt))
            nonlocal done
            done = True
        # Connect callbacks to the events fired by the speech recognizer
        speech_recognizer.recognizing.connect(recognizing_cb)
        speech_recognizer.recognized.connect(recognized_cb)
        speech_recognizer.session_stopped.connect(stop_cb)
        speech_recognizer.canceled.connect(stop_cb)
        # Perform recognition. `start_continuous_recognition_async asynchronously initiates continuous recognition operation,
        # Other tasks can be performed on this thread while recognition starts...
        # wait on result_future.get() to know when initialization is done.
        # Call stop_continuous_recognition_async() to stop recognition.
        result_future = speech_recognizer.start_continuous_recognition_async()
        result_future.get()  # wait for voidfuture, so we know engine initialization is done.
        print('Continuous Recognition is now running, say something.')
        while not done:
            # No real sample parallel work to do on this thread, so just wait for user to type stop.
            # Can't exit function or speech_recognizer will go out of scope and be destroyed while running.
            print('type "stop" then enter when done')
            stop = input()
            if (stop.lower() == "stop"):
                print('Stopping async recognition.')
                speech_recognizer.stop_continuous_recognition_async()
                break
        print("recognition stopped, main thread can exit now.")
    

    I hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.