Share via

Does stop_continuous_recognition() finish all callbacks

rick2049deckard 0 Reputation points
2023-10-19T22:35:01.51+00:00

Hei there

I am not sure about this and the documentation didnt help. I use continous speech recognition with the python sdk. If I invoke the function "stop_continuous_recognition(), is it guaranteed that all callbacks finish or could they also finish after invoking the method? The best scenario would be that if I call ity all recognizing and recognized callbacks finish executing and only after that the finishes executing. But I am not sure. Any help would be appreciated!

Azure Speech in Foundry Tools

1 answer

Sort by: Most helpful
  1. Rohit Mungi 49,131 Reputation points Microsoft Employee Moderator
    2023-10-20T11:49:10.7233333+00:00

    @rick2049deckard To understand the callback sequence I used the following code from the SDK repo to check how the events are fired before the stop continuous recognition is called.

    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename)
    
    def stop_cb(evt):        
        """callback that signals to stop continuous recognition upon receiving an event `evt`"""  
        print('CLOSING on {}'.format(evt))        
    	nonlocal done        
        done = True
    # Connect callbacks to the events fired by the speech recognizer    speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))    speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))    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)))    
    
    # stop continuous recognition on either session stopped or canceled events    speech_recognizer.session_stopped.connect(stop_cb)    
    speech_recognizer.canceled.connect(stop_cb)
    
    # Start continuous speech recognition    
    speech_recognizer.start_continuous_recognition()    
    
    while not done:        
        time.sleep(.5)
    
    
    
    

    The sample uses the sample audio file found in the same repo and uses start and stop continuous recognition to read the file.

    This is the case where I just run the sample as is and the logs printed from each event.

    SESSION STARTED: SessionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e)
    RECOGNIZING: SpeechRecognitionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e, result=SpeechRecognitionResult(result_id=c36fe5067ae94d418f8ac820cf3f7fe7, text="what's", reason=ResultReason.RecognizingSpeech))
    RECOGNIZING: SpeechRecognitionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e, result=SpeechRecognitionResult(result_id=0eabd607ced149828b8e1f107c4edc72, text="what's the weather like", reason=ResultReason.RecognizingSpeech))
    RECOGNIZED: SpeechRecognitionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e, result=SpeechRecognitionResult(result_id=62d6726531cc48f4909c1566699c8db6, text="What's the weather like?", reason=ResultReason.RecognizedSpeech))
    CANCELED SpeechRecognitionCanceledEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e, result=SpeechRecognitionResult(result_id=e9c2f05e8de54a0d8628acaf629bf580, text="", reason=ResultReason.Canceled))
    CLOSING on SpeechRecognitionCanceledEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e, result=SpeechRecognitionResult(result_id=e9c2f05e8de54a0d8628acaf629bf580, text="", reason=ResultReason.Canceled))
    SESSION STOPPED SessionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e)
    CLOSING on SessionEventArgs(session_id=5ddaf90eff394742b7b9bc07ccb2f45e)
    
    
    

    The events are fired after start is called and all the other events as identified by the SDK call the corresponding callback prints the events and result. In this case since we are waiting for done to be set to True in stop_cb, calling stop_continuous_recognition() is not relevant here since we are using a file and the stop event will be called irrespective of this, since the session stop event fired when end silence was detected in the file. You can check this scenario by commenting stop_continuous_recognition() and running the sample. If you are using audio from microphone you should call stop though and use its callback.

    Finally, if you comment the while loop that checks on done, the stop recognition is called immediately and this clearly shows that calling stop will not wait for other callbacks to complete. See the logs in this case.

    #commented while loop
    SESSION STARTED: SessionEventArgs(session_id=e00a9843a1af431d9c0f0b0a175ca9ac)
    RECOGNIZED: SpeechRecognitionEventArgs(session_id=e00a9843a1af431d9c0f0b0a175ca9ac, result=SpeechRecognitionResult(result_id=08e9e5aeb2f24af3b26899a2753f6b7e, text="", reason=ResultReason.NoMatch))
    SESSION STOPPED SessionEventArgs(session_id=e00a9843a1af431d9c0f0b0a175ca9ac)
    CLOSING on SessionEventArgs(session_id=e00a9843a1af431d9c0f0b0a175ca9ac)
    

    So, to summarize calling stop will not call other callbacks or wait for them to complete. You will have to ensure your audio session is complete before calling the corresponding stop callback. I hope this helps!!

    Was this answer helpful?

    0 comments No comments

Your answer

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