在Python中使用Azure Speech translation API从我的代码中获取空结果

tag5nh1u  于 2023-10-22  发布在  Python
关注(0)|答案(1)|浏览(110)

当我尝试在Colab中运行下面的代码时,我没有得到任何结果-也没有错误。博客的定位有什么问题吗?正如你可以猜到我是一个新手,所以我找不到的问题;- )

import json
import azure.cognitiveservices.speech as speechsdk

API_KEY = "2b0ddf64d03744d8aa77c0ca526ecc18"
ENDPOINT = "https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken"

media_file_path = "/content/konec_Outlook.wav"

translation_config = speechsdk.translation.SpeechTranslationConfig(
    subscription=API_KEY, endpoint=ENDPOINT) 
translation_config.speech_recognition_language = "en-GB"
translation_config.add_target_language("cs-CZ")

audio_config = speechsdk.audio.AudioConfig(filename=media_file_path)
recognizer = speechsdk.translation.TranslationRecognizer(
    translation_config=translation_config, audio_config=audio_config)

# Initial recognition
result = recognizer.recognize_once()
vars(result)

# Check if the initial recognition was successful
if result.reason == speechsdk.ResultReason.TranslatedSpeech:
    source_language_text = result.text
    duration = result.duration // pow(60, 4)
    print(result.translations['cs-CZ'])

    translation_json = json.loads(result.json)
    print(translation_json['RecognitionStatus'])
    print(translation_json['Duration'])
    print(translation_json['Text'])
    for translated in translation_json['Translation']['Translations']:
        print(translated['Language'])
        print(translated['Text'])
        print()

recognizer = speechsdk.translation.TranslationRecognizer(
    translation_config=translation_config, audio_config=audio_config)
outputs = []
toStop = False
while not toStop:
    if result.reason == speechsdk.ResultReason.Canceled:
        toStop = True
        break
    result = recognizer.recognize_once()
    translation_json = json.loads(result.json)
    for translated in translation_json['Translation']['Translations']:        
        print(translated['Language'])
        print(translated['Text'])
        outputs.append({'language': translated['Language'],  'text': translated['Text']})
        
print(outputs)
6rqinv9w

6rqinv9w1#

我能够从音频文件中翻译已识别的Azure语音。

  • 将下面的Python代码与Azure Cognitive Services配合使用,从音频文件中识别和翻译语音。它检测识别的语音的语言,然后将其翻译成捷克语。
import requests
import azure.cognitiveservices.speech as speechsdk

# Load the Azure Cognitive Services key and region for Translator

translator_key = ''
translator_region = ' '
translator_endpoint = 'https://api.cognitive.microsofttranslator.com'

# Load the Azure Cognitive Services key and region for Speech
speech_key = ""
speech_region = " "

def detect_language(text, key, region, endpoint):
    path = '/detect'
    url = endpoint + path
    params = {
        'api-version': '3.0'
    }
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': region,
        'Content-type': 'application/json'
    }
    body = [{
        'text': text
    }]
    request = requests.post(url, params=params, headers=headers, json=body)
    response = request.json()
    language = response[0]["language"]
    return language

def translate(text, source_language, target_language, key, region, endpoint):
    url = endpoint + '/translate'
    params = {
        'api-version': '3.0',
        'from': source_language,
        'to': target_language
    }
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': region,
        'Content-type': 'application/json'
    }
    body = [{
        'text': text
    }]
    request = requests.post(url, params=params, headers=headers, json=body)
    response = request.json()
    translation = response[0]["translations"][0]["text"]
    return translation

def recognize_and_translate_speech():
    try:
        # Configure the Speech SDK
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=speech_region)
        speech_config.speech_recognition_language = "en-US"

        # Set up the audio configuration to use an audio file
        audio_config = speechsdk.audio.AudioConfig(filename="C:/Users/Hello.wav")

        # Create a SpeechRecognizer
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

        # Recognize speech from the audio file
        speech_recognition_result = speech_recognizer.recognize_once_async().get()

        if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
            recognized_text = speech_recognition_result.text
            print("Recognized Speech: {}".format(recognized_text))
            
            # Detect the language of the recognized text
            source_language = detect_language(recognized_text, translator_key, translator_region, translator_endpoint)
            print("Detected Language: {}".format(source_language))
            
            # Translate the recognized text to Czech
            target_language = 'cs-CZ'  # Czech
            translated_text = translate(recognized_text, source_language, target_language, translator_key, translator_region, translator_endpoint)
            print("Translated Text (to Czech): {}".format(translated_text))
            
        elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
            print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
        elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = speech_recognition_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))
        
    except speechsdk.SpeechError as e:
        print("Speech SDK error: {}".format(e))
    except Exception as e:
        print("An error occurred: {}".format(e))

if __name__ == "__main__":
    recognize_and_translate_speech()

更新代码:

  • 为了处理异常,我在下面的代码中添加了一个try和exception块。
import requests
import azure.cognitiveservices.speech as speechsdk

# Load the Azure Cognitive Services key and region for Translator

translator_key = ''
translator_region = ' '
translator_endpoint = 'https://api.cognitive.microsofttranslator.com'

# Load the Azure Cognitive Services key and region for Speech
speech_key = ""
speech_region = " "

def detect_language(text, key, region, endpoint):
    path = '/detect'
    url = endpoint + path
    params = {
        'api-version': '3.0'
    }
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': region,
        'Content-type': 'application/json'
    }
    body = [{
        'text': text
    }]
    request = requests.post(url, params=params, headers=headers, json=body)
    response = request.json()
    language = response[0]["language"]
    return language

def translate(text, source_language, target_language, key, region, endpoint):
    url = endpoint + '/translate'
    params = {
        'api-version': '3.0',
        'from': source_language,
        'to': target_language
    }
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': region,
        'Content-type': 'application/json'
    }
    body = [{
        'text': text
    }]
    request = requests.post(url, params=params, headers=headers, json=body)
    response = request.json()
    translation = response[0]["translations"][0]["text"]
    return translation

def recognize_and_translate_speech():
    try:
        # Configure the Speech SDK
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=speech_region)
        speech_config.speech_recognition_language = "en-US"

        # Set up the audio configuration to use an audio file
        audio_config = speechsdk.audio.AudioConfig(filename="C:/Users/Hello.wav")

        # Create a SpeechRecognizer
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

        # Recognize speech from the audio file
        speech_recognition_result = speech_recognizer.recognize_once_async().get()

        if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
            recognized_text = speech_recognition_result.text
            print("Recognized Speech: {}".format(recognized_text))
            
            # Detect the language of the recognized text
            source_language = detect_language(recognized_text, translator_key, translator_region, translator_endpoint)
            print("Detected Language: {}".format(source_language))
            
            # Translate the recognized text to Czech
            target_language = 'cs-CZ'  # Czech
            translated_text = translate(recognized_text, source_language, target_language, translator_key, translator_region, translator_endpoint)
            print("Translated Text (to Czech): {}".format(translated_text))
            
        elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
            print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
        elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = speech_recognition_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))
        
    except Exception as e:
        print("An error occurred: {}".format(e))

if __name__ == "__main__":
    recognize_and_translate_speech()

输出:

相关问题