翻译语音-虚拟IoT设备


文档摘要

翻译语音 - 虚拟IoT设备 在本课程的这一部分中,你将编写代码,在使用语音服务将语音转换为文本时进行语音翻译,然后使用翻译服务将文本翻译成另一种语言,最后生成语音响应。 使用语音服务翻译语音 语音服务不仅可以将语音转换为同一种语言的文本,还可以将输出翻译成其他语言。 任务 - 使用语音服务翻译语音 在VS Code中打开项目 ,并确保虚拟环境已加载到终端中。 在现有导入语句下方添加以下导入语句: 这些导入语句用于翻译语音,并导入一个用于训练LUIS的语言服务器变量: 替换 with the locale name for language you will be speaking in, for example for French, or for Cantonese.

翻译语音 - 虚拟IoT设备

在本课程的这一部分中,你将编写代码,在使用语音服务将语音转换为文本时进行语音翻译,然后使用翻译服务将文本翻译成另一种语言,最后生成语音响应。

使用语音服务翻译语音

语音服务不仅可以将语音转换为同一种语言的文本,还可以将输出翻译成其他语言。

任务 - 使用语音服务翻译语音

  1. 在VS Code中打开项目 smart-timer ,并确保虚拟环境已加载到终端中。

  2. 在现有导入语句下方添加以下导入语句:

    from azure.cognitiveservices import speech from azure.cognitiveservices.speech.translation import SpeechTranslationConfig, TranslationRecognizer import requests

    这些导入语句用于翻译语音,并导入一个用于训练LUIS的语言服务器变量:

    language = '<user language>' server_language = '<server language>'

    替换 <user language> with the locale name for language you will be speaking in, for example fr-FR for French, or zn-HK for Cantonese.

    Replace <server language> with the locale name for language used to train LUIS.

    You can find a list of the supported languages and their locale names in the Language and voice support documentation on Microsoft docs.

    If you don't speak multiple languages you can use a service like Bing Translate or Google Translate to translate from your preferred language to a language of your choice. These services can then play audio of the translated text. Be aware that the speech recognizer will ignore some audio output from your device, so you may need to use an additional device to play the translated text.

    For example, if you train LUIS in English, but want to use French as the user language, you can translate sentences like "set a 2 minute and 27 second timer" from English into French using Bing Translate, then use the Listen translation button to speak the translation into your microphone.

    The listen translation button on Bing translate

  3. Replace the recognizer_config and recognizer 声明为以下内容:

    translation_config = SpeechTranslationConfig(subscription=speech_api_key, region=location, speech_recognition_language=language, target_languages=(language, server_language)) recognizer = TranslationRecognizer(translation_config=translation_config)

    这创建了一个翻译配置,用于识别用户语言中的语音,并创建用户和服务器语言的翻译。然后使用此配置创建一个翻译识别器——一种可以将语音识别输出翻译成多种语言的语音识别器。

    需要在 target_languages, otherwise you won't get any translations.

  4. Update the recognized 函数中指定原始语言,替换函数的全部内容为以下内容:

    if args.result.reason == speech.ResultReason.TranslatedSpeech: language_match = next(l for l in args.result.translations if server_language.lower().startswith(l.lower())) text = args.result.translations[language_match] if (len(text) > 0): print(f'Translated text: {text}') message = Message(json.dumps({ 'speech': text })) device_client.send_message(message)

    此代码检查是否因为语音被翻译而触发了识别事件(该事件可能在其他情况下触发,例如语音被识别但未被翻译)。如果语音被翻译,则在 args.result.translations dictionary that matches the server language.

    The args.result.translations dictionary is keyed off the language part of the locale setting, not the whole setting. For example, if you request a translation into fr-FR for French, the dictionary will contain an entry for fr, not fr-FR 中查找翻译。

    然后将翻译后的文本发送到IoT Hub。

  5. 运行此代码以测试翻译。确保你的函数应用正在运行,并请求用户语言中的计时器,可以通过自己说这种语言或使用翻译应用程序来实现。

    (.venv) ➜ smart-timer python app.py Connecting Connected Translated text: Set a timer of 2 minutes and 27 seconds.

使用翻译服务翻译文本

语音服务不支持将文本反向翻译成语音,相反,你可以使用翻译服务将文本翻译成另一种语言。此服务有一个REST API,可以用来翻译文本。

任务 - 使用翻译资源翻译文本

  1. speech_api_key 下面添加翻译API密钥:

    translator_api_key = '<key>'

    替换 <key> with the API key for your translator service resource.

  2. Above the say function, define a translate_text 函数,用于将从服务器语言到用户语言的文本翻译成用户语言:

    def translate_text(text):
  3. 在此函数内部,定义REST API调用的URL和头部:

    url = f'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0' headers = { 'Ocp-Apim-Subscription-Key': translator_api_key, 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json' }

    此API的URL不是位置特定的,而是通过头部传递位置信息。API密钥直接使用,因此与语音服务不同,不需要从令牌发行API获取访问令牌。

  4. 在此定义调用的参数和主体:

    params = { 'from': server_language, 'to': language } body = [{ 'text' : text }]

    params defines the parameters to pass to the API call, passing the from and to languages. This call will translate text in the from language into the to language.

    The body 包含要翻译的文本。这是一个数组,可以在同一调用中翻译多个文本块。

  5. 发起REST API调用并获取响应:

    response = requests.post(url, headers=headers, params=params, json=body)

    返回的响应是一个JSON数组,其中包含一个项,该条目包含所有传入主体项的翻译。此条目有一个数组,包含传入主体的所有项的翻译。

    [ { "translations": [ { "text": "Chronométrant votre minuterie de 2 minutes 27 secondes.", "to": "fr" } ] } ]
  6. 返回数组中第一个翻译项的第一个翻译的 test 属性:

    return response.json()[0]['translations'][0]['text']
  7. 更新 say 函数,以便在生成SSML之前翻译要说出的文本:

    print('Original:', text) text = translate_text(text) print('Translated:', text)

    此代码还将在控制台中打印原始文本和翻译后的文本。

  8. 运行你的代码。确保你的函数应用正在运行,并请求用户语言中的计时器,可以通过自己说这种语言或使用翻译应用程序来实现。

    (.venv) ➜ smart-timer python app.py Connecting Connected Translated text: Set a timer of 2 minutes and 27 seconds. Original: 2 minute 27 second timer started. Translated: 2 minute 27 seconde minute a commencé. Original: Times up on your 2 minute 27 second timer. Translated: Chronométrant votre minuterie de 2 minutes 27 secondes.

    由于不同语言表达方式的不同,你可能会得到与你在LUIS中提供的示例略有不同的翻译。如果是这种情况,请向LUIS添加更多示例,重新训练并重新发布模型。

你可以在此文件夹 code/virtual-iot-device 中找到此代码。

你的多语言计时器程序成功了!

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


发布者: 作者: 转发
评论区 (0)
U