Hi @De Wit Wendy , Thanks for using Microsoft Q&A Platform.
The catch here is that in the recognized_cb() function if condition "speechsdk.ResultReason.RecognizingSpeech == evt.result.reason" needs to be corrected to "speechsdk.ResultReason.RecognizedSpeech==evt.result.reason"
evt.result.reason == speechsdk.ResultReason.RecognizedSpeech
is used to check if the recognized speech segment is final, means that the recognizer has finished processing that segment of audio and has produced a final recognition result for it.
evt.result.reason == speechsdk.ResultReason.RecognizingSpeech
is used to check if the recognizer is currently processing a speech segment and has produced a partial recognition result for it.
Please make the following code changes so that you can see the desired results.
def recognized_cb(evt: speechsdk.SpeechRecognitionEventArgs) :
if speechsdk.ResultReason.RecognizedSpeech==evt.result.reason and len(evt.result.text) > 0 :
print('RECOGNIZED:', evt.result.text)
output_file.write(evt.result.text)
output_file.flush()
Following these changes, your final updated/working code should look like this.
def stt_run4(wav_file_path, taal, key, regio, outputfile):
speech_config = speechsdk.SpeechConfig(subscription=key, region=regio)
speech_config.speech_recognition_language=taal
audio_config = speechsdk.audio.AudioConfig(filename=wav_file_path)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
done = False
# Set up the output file for the transcript
output_file = open(outputfile, "w")
def stop_cb(evt):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
# Close the output file and stop the continuous recognition session
output_file.close()
speech_recognizer.stop_continuous_recognition()
print("Transcript saved in file:", outputfile)
nonlocal done
done = True
def recognized_cb(evt: speechsdk.SpeechRecognitionEventArgs) :
if speechsdk.ResultReason.RecognizedSpeech==evt.result.reason and len(evt.result.text) > 0 :
print('RECOGNIZED:', evt.result.text)
output_file.write(evt.result.text)
output_file.flush()
# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
speech_recognizer.recognized.connect(recognized_cb)
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)
This final code works for me. Please try and let us know how it goes on your end.
I hope this helps.
Regards,
Vasavi
-Please kindly accept the answer and vote 'Yes if you feel helpful to support the community, thanks.