将 IoT DevKit AZ3166 与 Azure Functions 和认知服务配合使用生成语言翻译Use IoT DevKit AZ3166 with Azure Functions and Cognitive Services to make a language translator

本文介绍如何使用 Azure 认知服务将 IoT DevKit 设为语言翻译。In this article, you learn how to make IoT DevKit as a language translator by using Azure Cognitive Services. 它会录制你的声音并将其翻译为 DevKit 屏幕上显示的英文文本。It records your voice and translates it to English text shown on the DevKit screen.

MXChip IoT DevKit 是具有多种外设和传感器的集成 Arduino 兼容板。The MXChip IoT DevKit is an all-in-one Arduino compatible board with rich peripherals and sensors. 可以使用 Visual Studio Code 中的 Azure IoT Device Workbench(Azure IoT 设备 Workbench)或 Azure IoT 工具扩展包进行开发。You can develop for it using Azure IoT Device Workbench or Azure IoT Tools extension pack in Visual Studio Code. 项目目录包含有助于确定 IoT 解决方案原型的示例应用程序。The projects catalog contains sample applications to help you prototype IoT solutions.

开始之前Before you begin

若要完成本教程中的步骤,请先执行以下任务:To complete the steps in this tutorial, first do the following tasks:

创建 Azure 认知服务Create Azure Cognitive Service

  1. 在 Azure 门户中,点击“创建资源”,然后搜索“语音”。In the Azure portal, click Create a resource and search for Speech. 填写窗体,创建语音服务。Fill out the form to create the Speech Service.

    语音服务

  2. 转到刚才创建的语音服务,单击“Keys”部分进行复制,记下访问它的 DevKit 的 Key1。Go to the Speech service you just created, click Keys section to copy and note down the Key1 for DevKit accessing to it.

    复制密钥

打开示例项目Open sample project

  1. 确保 IoT DevKit 未连接到计算机。Make sure your IoT DevKit is not connected to your computer. 先启动 VS Code,然后将 DevKit 连接到计算机。Start VS Code first, and then connect the DevKit to your computer.

  2. 单击 F1 以打开命令面板,键入并选择 Azure IoT Device Workbench: Open Examples...Click F1 to open the command palette, type and select Azure IoT Device Workbench: Open Examples.... 然后选择“IoT DevKit”作为开发板。Then select IoT DevKit as board.

  3. 在 IoT Workbench 的“示例”页中,找到 DevKit Translator 并单击“打开示例”。In the IoT Workbench Examples page, find DevKit Translator and click Open Sample. 然后选择用于下载示例代码的默认路径。Then selects the default path to download the sample code.

    打开示例

配合使用语音服务和 Azure FunctionsUse Speech Service with Azure Functions

  1. 在 VS Code 中,单击 F1,键入并选择 Azure IoT Device Workbench: Provision Azure Services...In VS Code, click F1, type and select Azure IoT Device Workbench: Provision Azure Services....

    预配 Azure 服务

  2. 按照以下步骤完成 Azure IoT 中心和 Azure Functions 的预配。Follow the steps to finish provisioning of Azure IoT Hub and Azure Functions.

    预配步骤

    记录创建的 Azure IoT 中心设备的名称。Take a note of the Azure IoT Hub device name you created.

  3. 打开 Functions\DevKitTranslatorFunction.cs 并使用记录的设备名称和语音服务密钥更新以下代码行。Open Functions\DevKitTranslatorFunction.cs and update the following lines of code with the device name and Speech Service key you noted down.

    // Subscription Key of Speech Service
    const string speechSubscriptionKey = "";
    
    // Region of the speech service, see https://docs.azure.cn/cognitive-services/speech-service/regions for more details.
    const string speechServiceRegion = "";
    
    // Device ID
    const string deviceName = "";
    
  4. 单击 F1,键入并选择 Azure IoT Device Workbench: Deploy to Azure...Click F1, type and select Azure IoT Device Workbench: Deploy to Azure.... 如果 VS Code 要求对重新部署进行确认,请单击“是”。If VS Code asks for confirmation for redeployment, click Yes. 部署警告

  5. 请确保部署成功。Make sure the deployment is successful. 部署成功

  6. 在 Azure 门户中,转到 Function App 部分,找到刚刚创建的 Azure 函数应用。In Azure portal, go to Functions Apps section, find the Azure Function app just created. 单击 devkit_translator,然后单击“</> 获取函数 URL”以复制 URL。Click devkit_translator, then click </> Get Function URL to copy the URL. 复制函数 URL

  7. 将 URL 粘贴到 azure_config.h 文件。Paste the URL into azure_config.h file. Azure 配置

    备注

    如果函数应用无法正常工作,请查看此常见问题解答部分以解决该问题。If the Function app does not work properly, check this FAQ section to resolve it.

生成并上传设备代码Build and upload device code

  1. 按以下方式,将 DevKit 切换为配置模式:Switch the DevKit to configuration mode by:

    • 按住按钮 A。Hold down button A.
    • 按下然后松开“重置”按钮。Press and release Reset button.

    屏幕将显示 DevKit ID 和“配置”。You will see the screen displays the DevKit ID and Configuration.

    DevKit 配置模式

  2. 单击 F1,键入并选择 Azure IoT Device Workbench: Configure Device Settings... > Config Device Connection StringClick F1, type and select Azure IoT Device Workbench: Configure Device Settings... > Config Device Connection String. 选择 Select IoT Hub Device Connection String,将其配置为 DevKit。Select Select IoT Hub Device Connection String to configure it to the DevKit. 配置连接字符串

  3. 操作成功后,将看到通知。You will see the notification once it's done successfully. 成功配置连接字符串

  4. 再次单击 F1,键入并选择 Azure IoT Device Workbench: Upload Device CodeClick F1 again, type and select Azure IoT Device Workbench: Upload Device Code. 它启动编译,并将代码上传到 DevKit。It starts compile and upload the code to DevKit. 设备上传

测试项目Test the project

应用初始化后,请按照 DevKit 屏幕上的说明进行操作。After app initialization, follow the instructions on the DevKit screen. 默认源语言为中文。The default source language is Chinese.

若要选择另一种语言进行翻译,请执行以下操作:To select another language for translation:

  1. 按下按钮 A 进入设置模式。Press button A to enter setup mode.
  2. 按下按钮 B 滚动所有支持的源语言。Press button B to scroll all supported source languages.
  3. 按下按钮 A 确认所选的源语言。Press button A to confirm your choice of source language.
  4. 说话时按住按钮 B,然后释放按钮 B 以启动翻译。Press and hold button B while speaking, then release button B to initiate the translation.
  5. 屏幕上将显示英语的已翻译文本。The translated text in English shows on the screen.

滚动以选择语言

翻译结果

在“翻译结果”屏幕上,可以:On the translation result screen, you can:

  • 按下按钮 A 和 B,滚动并选择源语言。Press buttons A and B to scroll and select the source language.

  • 按下按钮 B 开始说话。Press the B button to talk. 释放按钮 B,发送语音并获取翻译文本。To send the voice and get the translation text, release the B button.

工作原理How it works

mini-solution-voice-to-tweet-diagram

IoT DevKit 会记录你的声音,然后发送 HTTP 请求以触发 Azure Functions。The IoT DevKit records your voice then posts an HTTP request to trigger Azure Functions. Azure Functions 会调用认知服务语音翻译 API 进行翻译。Azure Functions calls the cognitive service speech translator API to do the translation. Azure Functions 获取翻译文本后,它将 C2D 消息发送到设备。After Azure Functions gets the translation text, it sends a C2D message to the device. 然后翻译将显示在屏幕上。Then the translation is displayed on the screen.

问题和反馈Problems and feedback

如果遇到问题,请参阅 IoT DevKit 常见问题解答,或通过以下渠道联系我们:If you encounter problems, refer to the IoT DevKit FAQ or reach out to us using the following channels:

后续步骤Next Steps

通过使用 Azure Functions 和认知服务,你已了解如何使用 IoT DevKit 进行翻译。You have learned how to use the IoT DevKit as a translator by using Azure Functions and Cognitive Services. 在这个操作方法中,你学会了如何:In this how-to, you learned how to:

  • 使用 Visual Studio Code 任务自动执行云预配Use Visual Studio Code task to automate cloud provisions
  • 配置 Azure IoT 设备连接字符串Configure Azure IoT device connection string
  • 部署 Azure FunctionDeploy the Azure Function
  • 测试语音消息翻译Test the voice message translation

继续学习其他教程:Advance to the other tutorials to learn: