翻译语音-Wio终端


文档摘要

翻译语音 - Wio终端 在本课程的这一部分,你将编写代码以使用翻译服务来翻译文本。 使用翻译服务将文本转换为语音 语音服务的REST API不支持直接翻译,但你可以使用翻译服务将语音转文字服务生成的文本和语音响应的文本进行翻译。此服务有一个REST API,你可以用来翻译文本,但为了更方便地使用,这将在你的函数应用中被封装在一个HTTP触发器中。 任务 - 创建一个无服务器函数来翻译文本 在VS Code中打开你的 项目,并打开终端确保虚拟环境已激活。如果没有,请关闭并重新创建终端。

翻译语音 - Wio终端

在本课程的这一部分,你将编写代码以使用翻译服务来翻译文本。

使用翻译服务将文本转换为语音

语音服务的REST API不支持直接翻译,但你可以使用翻译服务将语音转文字服务生成的文本和语音响应的文本进行翻译。此服务有一个REST API,你可以用来翻译文本,但为了更方便地使用,这将在你的函数应用中被封装在一个HTTP触发器中。

任务 - 创建一个无服务器函数来翻译文本

  1. 在VS Code中打开你的smart-timer-trigger项目,并打开终端确保虚拟环境已激活。如果没有,请关闭并重新创建终端。

  2. 打开local.settings.json文件,并添加翻译API密钥和位置的设置:

    "TRANSLATOR_KEY": "<key>", "TRANSLATOR_LOCATION": "<location>"

    使用以下命令从VS Code终端中的函数应用项目的根目录替换<key> with the API key for your translator service resource. Replace <location> with the location you used when you created the translator service resource.

  3. Add a new HTTP trigger to this app called translate-text

    func new --name translate-text --template "HTTP trigger"

    这将创建一个名为translate-text.

  4. Replace the contents of the __init__.py file in the translate-text的HTTP触发器,并包含以下内容:

    import logging import os import requests import azure.functions as func location = os.environ['TRANSLATOR_LOCATION'] translator_key = os.environ['TRANSLATOR_KEY'] def main(req: func.HttpRequest) -> func.HttpResponse: req_body = req.get_json() from_language = req_body['from_language'] to_language = req_body['to_language'] text = req_body['text'] logging.info(f'Translating {text} from {from_language} to {to_language}') url = f'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0' headers = { 'Ocp-Apim-Subscription-Key': translator_key, 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json' } params = { 'from': from_language, 'to': to_language } body = [{ 'text' : text }] response = requests.post(url, headers=headers, params=params, json=body) return func.HttpResponse(response.json()[0]['translations'][0]['text'])

    此代码从HTTP请求中提取文本和语言,然后向翻译REST API发出请求,将语言作为URL参数传递,并将要翻译的文本作为主体。最后返回翻译结果。

  5. 在本地运行你的函数应用。然后可以像测试text-to-timer HTTP触发器一样使用工具如curl来调用它。确保将要翻译的文本和语言作为JSON主体传递:

    { "text": "Définir une minuterie de 30 secondes", "from_language": "fr-FR", "to_language": "en-US" }

    该示例将法语的“Définir une minuterie de 30 secondes”翻译成美式英语。它将返回“Set a 30-second timer”。

你可以在code/functions文件夹中找到此代码。

任务 - 使用翻译函数来翻译文本

  1. 打开用于训练LUIS的语言的smart-timer project in VS Code if it is not already open.

  2. Your smart timer will have 2 languages set - the language of the server that was used to train LUIS (the same language is also used to build the messages to speak to the user), and the language spoken by the user. Update the LANGUAGE constant in the config.h header file to be the language that will be spoken by the user, and add a new constant called SERVER_LANGUAGE文件:

    const char *LANGUAGE = "<user language>"; const char *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.

    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. Add the translator API key and location below the SPEECH_LOCATION替换为:

    const char *TRANSLATOR_API_KEY = "<KEY>"; const char *TRANSLATOR_LOCATION = "<LOCATION>";

    <KEY> with the API key for your translator service resource. Replace <LOCATION> with the location you used when you created the translator service resource.

  4. Add the translator trigger URL below the VOICE_URL替换为:

    const char *TRANSLATE_FUNCTION_URL = "<URL>";

    <URL> with the URL for the translate-text HTTP trigger on your function app. This will be the same as the value for TEXT_TO_TIMER_FUNCTION_URL, except with a function name of translate-text instead of text-to-timer.

  5. Add a new file to the src folder called text_translator.h.

  6. This new text_translator.h头文件将包含一个用于翻译文本的类。在此文件中添加以下内容以声明该类:

    #pragma once #include <Arduino.h> #include <ArduinoJson.h> #include <HTTPClient.h> #include <WiFiClient.h> #include "config.h" class TextTranslator { public: private: WiFiClient _client; }; TextTranslator textTranslator;

    声明该类的TextTranslator class, along with an instance of this class. The class has a single field for the WiFi client.

  7. To the public部分,添加一个方法来翻译文本:

    String translateText(String text, String from_language, String to_language) { }

    此方法接受要翻译的源语言和目标语言。处理语音时,语音将从用户语言翻译到LUIS服务器语言,而在给出响应时,将从LUIS服务器语言翻译到用户的语言。

  8. 在此方法中,添加代码以构造一个包含要翻译的文本和语言的JSON主体:

    DynamicJsonDocument doc(1024); doc["text"] = text; doc["from_language"] = from_language; doc["to_language"] = to_language; String body; serializeJson(doc, body); Serial.print("Translating "); Serial.print(text); Serial.print(" from "); Serial.print(from_language); Serial.print(" to "); Serial.print(to_language);
  9. 在此之下,添加以下代码以将主体发送到无服务器函数应用:

    HTTPClient httpClient; httpClient.begin(_client, TRANSLATE_FUNCTION_URL); int httpResponseCode = httpClient.POST(body);
  10. 接下来,添加代码以获取响应:

    String translated_text = ""; if (httpResponseCode == 200) { translated_text = httpClient.getString(); Serial.print("Translated: "); Serial.println(translated_text); } else { Serial.print("Failed to translate text - error "); Serial.println(httpResponseCode); }
  11. 最后,添加代码以关闭连接并返回翻译后的文本:

    httpClient.end(); return translated_text;

任务 - 翻译识别的语音和响应

  1. 打开main.cpp file.

  2. Add an include directive at the top of the file for the TextTranslator类头文件:

    #include "text_translator.h"
  3. 当计时器设置或过期时说的文字需要翻译。为此,在say函数的第一行添加以下内容:

    text = textTranslator.translateText(text, LANGUAGE, SERVER_LANGUAGE);

    这将把文本翻译成用户的语言。

  4. processAudio function, text is retrieved from the captured audio with the String text = speechToText.convertSpeechToText();调用之后,翻译文本:

    String text = speechToText.convertSpeechToText(); text = textTranslator.translateText(text, LANGUAGE, SERVER_LANGUAGE);

    这将把文本从用户的语言翻译成服务器使用的语言。

  5. 构建此代码,将其上传到你的Wio终端并通过串行监视器进行测试。一旦在串行监视器中看到Ready,按下C按钮(最靠近电源开关的左侧按钮),并讲话。确保你的函数应用正在运行,并通过自己说话或使用翻译应用程序以用户语言请求计时器。

    Connecting to WiFi.. Connected! Got access token. Ready. Starting recording... Finished recording Sending speech... Speech sent! {"RecognitionStatus":"Success","DisplayText":"Définir une minuterie de 2 minutes 27 secondes.","Offset":9600000,"Duration":40400000} Translating Définir une minuterie de 2 minutes 27 secondes. from fr-FR to en-US Translated: Set a timer of 2 minutes 27 seconds. Set a timer of 2 minutes 27 seconds. {"seconds": 147} Translating 2 minute 27 second timer started. from en-US to fr-FR Translated: 2 minute 27 seconde minute a commencé. 2 minute 27 seconde minute a commencé. Translating Times up on your 2 minute 27 second timer. from en-US to fr-FR Translated: Chronométrant votre minuterie de 2 minutes 27 secondes. Chronométrant votre minuterie de 2 minutes 27 secondes.

你可以在code/wio-terminal文件夹中找到此代码。

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

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


作者与出处
原作者: microsoft
来源:microsoft
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: microsoft 转发
评论区 (0)
U