def speech_recognize_with_auto_language_detection_UsingCustomizedModel(): """performs speech recognition from the audio file with auto language detection, using customized model""" speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region) audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename) # Replace the languages with your languages in BCP-47 format, e.g. fr-FR. # Please see https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support # for all supported languages en_language_config = speechsdk.languageconfig.SourceLanguageConfig("en-US") # Replace the languages with your languages in BCP-47 format, e.g. zh-CN. # Set the endpoint ID of your customized mode that will be used for fr-FR. # Replace with your own CRIS endpoint ID. fr_language_config = speechsdk.languageconfig.SourceLanguageConfig("fr-FR", "myendpointId") # create the auto detection language configuration with the source language configurations auto_detect_source_language_config = speechsdk.languageconfig.AutoDetectSourceLanguageConfig( sourceLanguageConfigs=[en_language_config, fr_language_config]) speech_recognizer = speechsdk.SpeechRecognizer( speech_config=speech_config, auto_detect_source_language_config=auto_detect_source_language_config, audio_config=audio_config) result = speech_recognizer.recognize_once() # Check the result if result.reason == speechsdk.ResultReason.RecognizedSpeech: auto_detect_source_language_result = speechsdk.AutoDetectSourceLanguageResult(result) print("Recognized: {} in language {}".format(result.text, auto_detect_source_language_result.language)) elif result.reason == speechsdk.ResultReason.NoMatch: print("No speech could be recognized") elif result.reason == speechsdk.ResultReason.Canceled: cancellation_details = result.cancellation_details print("Speech Recognition canceled: {}".format(cancellation_details.reason)) if cancellation_details.reason == speechsdk.CancellationReason.Error: print("Error details: {}".format(cancellation_details.error_details))
def speech_recognize_once_with_auto_language_detection_from_mic(): """performs one-shot speech recognition from the default microphone with auto language detection""" speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region) # create the auto detection language configuration with the potential source language candidates auto_detect_source_language_config = \ speechsdk.languageconfig.AutoDetectSourceLanguageConfig(languages=["de-DE", "en-US"]) speech_recognizer = speechsdk.SpeechRecognizer( speech_config=speech_config, auto_detect_source_language_config=auto_detect_source_language_config) result = speech_recognizer.recognize_once() # Check the result if result.reason == speechsdk.ResultReason.RecognizedSpeech: auto_detect_source_language_result = speechsdk.AutoDetectSourceLanguageResult( result) print("Recognized: {} in language {}".format( result.text, auto_detect_source_language_result.language)) elif result.reason == speechsdk.ResultReason.NoMatch: print("No speech could be recognized") elif result.reason == speechsdk.ResultReason.Canceled: cancellation_details = result.cancellation_details print("Speech Recognition canceled: {}".format( cancellation_details.reason)) if cancellation_details.reason == speechsdk.CancellationReason.Error: print("Error details: {}".format( cancellation_details.error_details))
def recognized_handler(e: speechsdk.SpeechRecognitionEventArgs): if speechsdk.ResultReason.RecognizedSpeech == e.result.reason and len( e.result.text) > 0: language = None if user_config['languages'] is not None: language_id_result = speechsdk.AutoDetectSourceLanguageResult( e.result) language = language_id_result.language nonlocal sequence_number sequence_number += 1 start_time = ticks_to_time(e.result.offset) end_time = ticks_to_time(e.result.offset + e.result.duration) caption = caption = caption_time_to_caption( srt=user_config['srt_enabled'], sequence_number=sequence_number, start_time=start_time, end_time=end_time, text=e.result.text, language=language) write_to_console_or_file(text=caption, user_config=user_config) elif speechsdk.ResultReason.NoMatch == e.result.reason: write_to_console( text='NOMATCH: Speech could not be recognized.{}'.format( linesep), user_config=user_config)
def language_from_speech_recognition_result( result: speechsdk.SpeechRecognitionResult, user_config: helper.Read_Only_Dict) -> str: if user_config["language_ID_languages"] is not None: return "[{}] ".format( speechsdk.AutoDetectSourceLanguageResult(result).language) else: return ""