使用Python中的Azure语音翻译API从我的代码中获取空结果

问题描述 投票:0回答:1

当我尝试在 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)

python azure translation azure-cognitive-services
1个回答
0
投票

使用下面的代码能够从音频文件翻译识别的 Azure 语音




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()



输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.