当我尝试在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)
1条答案
按热度按时间6rqinv9w1#
我能够从音频文件中翻译已识别的Azure语音。
更新代码:
输出: