在 IoT Edge 设备上部署实时视频分析Deploy Live Video Analytics on an IoT Edge device

本文列出了将帮助你在 IoT Edge 设备上部署实时视频分析的步骤。This article lists the steps that will help you deploy Live Video Analytics on your IoT Edge device. 你可能会在以下情况下执行此操作,例如:如果你有权访问本地 Linux 计算机并且/或之前创建了 Azure 媒体服务帐户。You would do this, for example, if you have access to a local Linux machine, and/or have previously created an Azure Media Services account.

备注

IoT Edge 版本 1.0.4 及更高版本的实时视频分析中提供了对 ARM64 设备的支持。Support for ARM64 devices is available in Live Video Analytics on IoT Edge builds 1.0.4 and newer. 支持在 ARM64 设备上运行 Azure IoT Edge 运行时,该支持为公共预览版。Support for running Azure IoT Edge runtime on ARM64 devices is in public preview.

必备条件Prerequisites

配置 Azure 资源以使用实时视频分析Configuring Azure resources for using Live Video Analytics

创建自定义 Azure 资源管理器角色Create custom Azure Resource Manager role

请参阅创建自定义 Azure 资源管理器角色,并将其分配到用于实时视频分析的服务主体。See Create custom Azure Resource Manager role and assign it to a service principal for Live Video Analytics use.

设置高级流式处理终结点Set up a premium streaming endpoint

如果你打算使用实时视频分析将视频录制到云中,然后在重播之前按顺序使用查询 API,则建议更新媒体服务以使用高级流式处理终结点If you intend to use Live Video Analytics to record video continuously to the cloud, and subsequently use query APIs before playing it back, then we recommend updating your Media Service to use a premium streaming endpoint.

这是可选步骤。This is an optional step. 可以使用此 Azure CLI 命令执行此操作:You can use this Azure CLI command to do so:

az ams streaming-endpoint scale --resource-group $RESOURCE_GROUP --account-name $AMS_ACCOUNT -n default --scale-units 1

可以使用此命令启动流式处理终结点You can use this command to start the streaming endpoint

重要

此时,你的订阅将开始计费。Your subscription will start getting billed at this point.

az ams streaming-endpoint start --resource-group $RESOURCE_GROUP --account-name $AMS_ACCOUNT -n default --no-wait

按照本文中的步骤获取访问媒体服务 API 的凭据:访问媒体服务 API,并选择“门户”选项卡。Follow the steps in this article to get credentials to access the Media Service APIs: access the Media Service APIs and select the Portal tab.

创建并使用本地用户帐户进行部署Create and use local user account for deployment

若要运行 IoT Edge 上的实时视频分析模块,请创建权限尽可能少的本地用户帐户。To run the Live Video Analytics on IoT Edge module create a local user account with as few privileges as possible. 例如,在 Linux 计算机上运行以下命令:As an example, run the following commands on your Linux machine:

sudo groupadd -g 1010 localuser
sudo adduser --home /home/edgeuser --uid 1010 -gid 1010 edgeuser

授予设备存储权限Granting permissions to device storage

现在,你创建了一个本地用户帐户,Now that you have created a local user account,

  • 需要一个本地文件夹来存储应用程序配置数据。You will need a local folder to store the application configuration data. 使用以下命令创建文件夹并向本地用户帐户授予写入该文件夹的权限:Create a folder and grant permissions to the localuser account write to that folder using the following commands:
sudo mkdir /var/lib/azuremediaservices
sudo chown -R edgeuser /var/lib/azuremediaservices
sudo mkdir /var/media
sudo chown -R edgeuser /var/media

部署实时视频分析 Edge 模块Deploy Live Video Analytics Edge module

IoT Edge 上的实时视频分析公开记录在模块孪生配置架构中的模块孪生属性。The Live Video Analytics on IoT Edge exposes module twin properties that are documented in Module Twin configuration schema.

使用 Azure 门户进行部署Deploy using the Azure portal

Azure 门户引导你创建部署清单并将部署推送到 IoT Edge 设备。The Azure portal guides you through creating a deployment manifest and pushing the deployment to an IoT Edge device.

选择设备并设置模块Select your device and set modules

  1. 登录 Azure 门户,导航到 IoT 中心。Sign in to the Azure portal and navigate to your IoT hub.
  2. 从菜单中选择“IoT Edge” 。Select IoT Edge from the menu.
  3. 在设备列表中单击目标设备的 ID。Click on the ID of the target device from the list of devices.
  4. 选择“设置模块” 。Select Set Modules.

配置部署清单Configure a deployment manifest

部署清单是一个 JSON 文档,其中描述了要部署的模块、数据在模块间的流动方式以及模块孪生的所需属性。A deployment manifest is a JSON document that describes which modules to deploy, how data flows between the modules, and desired properties of the module twins. Azure 门户提供了一个引导你完成创建部署清单的向导。The Azure portal has a wizard that walks you through creating a deployment manifest. 该向导将三个步骤整理成多个选项卡:“模块”、“路由”和“查看 + 创建” 。It has three steps organized into tabs: Modules, Routes, and Review + Create.

添加模块Add modules

  1. 在页面的“IoT Edge 模块”部分,单击“添加”下拉列表并选择“IoT Edge 模块”,显示“添加 IoT Edge 模块”页面 。In the IoT Edge Modules section of the page, click the Add dropdown and select IoT Edge Module to display the Add IoT Edge Module page.

  2. 在“模块设置”选项卡上,提供模块的名称,然后指定容器映像 URI :On the Module Settings tab, provide a name for the module and then specify the container image URI:
    示例:Examples:

    • IoT Edge 模块名称:lvaEdgeIoT Edge Module Name: lvaEdge
    • 映像 URI:mcr.microsoft.com/media/live-video-analytics:1.0Image URI: mcr.microsoft.com/media/live-video-analytics:1.0

    添加

    提示

    如本过程所述,在“模块设置”、“容器创建选项”和“模块孪生设置”选项卡上指定值之前,请勿选择“添加” 。Don't select Add until you've specified values on the Module Settings, Container Create Options, and Module Twin Settings tabs as described in this procedure.

    警告

    当你调用模块时,Azure IoT Edge 会区分大小写。Azure IoT Edge is case-sensitive when you make calls to modules. 请记下用作模块名称的确切字符串。`Make note of the exact string you use as the module name.`

  3. 打开“环境变量”选项卡。Open the Environment Variables tab.

    在显示环境变量的输入框中添加以下值Add the following values in the input boxes that you see Environment Variables

  4. 打开“容器创建选项”选项卡。Open the Container Create Options tab.

    容器创建选项

    将以下 JSON 复制并粘贴到框中,从而限制模块生成的日志文件的大小。Copy and paste the following JSON into the box, to limit the size of the log files produced by the module.

    {
        "HostConfig": {
            "LogConfig": {
                "Type": "",
                "Config": {
                    "max-size": "10m",
                    "max-file": "10"
                }
            },
            "Binds": [
               "/var/lib/azuremediaservices:/var/lib/azuremediaservices",
               "/var/media:/var/media"
            ]
        }
    }
    

    JSON 中的 "Binds" 部分有 2 个条目:The "Binds" section in the JSON has 2 entries:

    1. "/var/lib/azuremediaservices:/var/lib/azuremediaservices":这用于从容器绑定永久性应用程序配置数据并将其存储在边缘设备上。"/var/lib/azuremediaservices:/var/lib/azuremediaservices": This is used to bind the persistent application configuration data from the container and store it on the edge device.
    2. "/var/media:/var/media":这将绑定边缘设备和容器之间的媒体文件夹。"/var/media:/var/media": This binds the media folders between the edge device and the container. 这用于在运行支持在边缘设备上存储视频剪辑的媒体图拓扑时存储视频录制。This is used to store the video recordings when you run a media graph topology that supports storing of video clips on the edge device.
  5. 在“模块孪生设置”选项卡上,复制以下 JSON 并将其粘贴到框中。On the Module Twin Settings tab, copy the following JSON and paste it into the box.

    孪生设置

    IoT Edge 上的实时视频分析需要一组必需的孪生属性才能运行,如模块孪生配置架构中所列。Live Video Analytics on IoT Edge requires a set of mandatory twin properties in order to run, as listed in Module Twin configuration schema.

    需要输入到模块孪生设置编辑框中的 JSON 将如下所示:The JSON that you need to enter into Module Twin Settings edit box will look like this:

    {
        "applicationDataDirectory": "/var/lib/azuremediaservices",
        "azureMediaServicesArmId": "/subscriptions/{subscriptionID}/resourceGroups/{resourceGroupName}/providers/microsoft.media/mediaservices/{AMS-account-name}",
        "aadTenantId": "{the-ID-of-your-tenant}",
        "aadServicePrincipalAppId": "{the-ID-of-the-service-principal-app-for-ams-account}",
        "aadServicePrincipalSecret": "{secret}"
    }
    

    以下是上述 JSON 所需的属性,These are required properties and for the JSON above,

    • {subscriptionID} - 这是你的 Azure 订阅 ID{subscriptionID} - this is your Azure Subscription ID
    • {resourceGroupName} - 这是你的媒体服务帐户所属的资源组{resourceGroupName} - this the resource group to which your Media Service account belongs
    • {AMS-account-name} - 这是你的媒体服务帐户名称{AMS-account-name} - this is the name of your Media Services account

    若要获取其他值,请查看访问 Azure 媒体服务 API,并选择“门户”选项卡。To get the other values, see Access Azure Media Services API and select the Portal tab.

    • aadTenantId - 这是你的租户 ID,与上述链接中的 "AadTenantId" 相同。aadTenantId - this is the ID of your tenant and is the same as the "AadTenantId" from the above link.
    • aadServicePrincipalAppId - 这是你的媒体服务帐户的服务主体的应用 ID,与上述链接中的 "AadClientId" 相同。aadServicePrincipalAppId - this is the app ID of the service principal for your Media Service Account and is the same as the "AadClientId" from the above link.
    • aadServicePrincipalSecret - 这是服务主体的密码,与上述链接中的 "AadSecret" 相同。aadServicePrincipalSecret - this is the password of the service principal and is the same as the "AadSecret" from the above link.

    下面是一些可添加到 JSON 的其他建议属性,将有助于监视模块。Below are some additional recommended properties that can be added to the JSON and will help in monitoring the module. 有关详细信息,请参阅监视和日志记录For more information, see monitoring and logging:

    "diagnosticsEventsOutputName": "lvaEdgeDiagnostics",
    "OperationalEventsOutputName": "lvaEdgeOperational",
    "logLevel": "Information",
    "logCategories": "Application,Events"
    

    以下是可以在 JSON 中添加的一些可选属性:Following are some optional properties that you could add in the JSON:

    "aadEndpoint": "https://login.chinacloudapi.cn",
    "aadResourceId": "https://management.core.chinacloudapi.cn/",
    "armEndpoint": "https://management.chinacloudapi.cn/",
    "allowUnsecuredEndpoints": true
    

    备注

    出于教程和快速入门的目的,孪生属性 allowUnsecuredEndpoints 被设置为了 true。The twin property allowUnsecuredEndpoints is set as true for the purpose of the tutorials and the quickstarts.
    在生产环境中运行时,应将此属性设置为 false。You should set this property to false when running in production environment. 这将确保应用程序将阻止所有不安全的终结点,并且需要有效的连接凭据才能运行图形拓扑。This will ensure that the application will block all unsecured endpoints and in order to run the graph topologies, valid connection credentials will be needed.

    选择“添加”以添加模块孪生属性。Select Add to add the module twin properties.

  6. 在完成时选择“下一步:路由”继续转到路由部分。Select Next: Routes to continue to the routes section. 指定路由。Specify routes.

保留默认路由并选择“下一步: 查看 + 创建”继续转到查看部分。Keep the default routes and select Next: Review + create to continue to the review section.

评审部署Review deployment

评审部分介绍了根据上述两部分中的选择所创建的 JSON 部署清单。The review section shows you the JSON deployment manifest that was created based on your selections in the previous two sections. 另外还有两个模块已声明但却未添加,它们是:$edgeAgent 和 $edgeHub。There are also two modules declared that you didn't add: $edgeAgent and $edgeHub. 这两个模块构成 IoT Edge 运行时,并且是每个部署中所需的默认模块。These two modules make up the IoT Edge runtime and are required defaults in every deployment.

查看部署信息,然后选择“创建”。Review your deployment information, then select Create.

验证部署Verify your deployment

创建部署后,返回到 IoT 中心的“IoT Edge”页。After you create the deployment, you return to the IoT Edge page of your IoT hub.

  1. 选择用作部署目标的 IoT Edge 设备,以打开其详细信息。Select the IoT Edge device that you targeted with the deployment to open its details.
  2. 在设备详细信息中,验证 Blob 存储模块是否已列为“在部署中指定”和“由设备报告”。In the device details, verify that the blob storage module is listed as both Specified in deployment and Reported by device.

可能需要等待一段时间,该模块才会在设备上启动并向 IoT 中心发回报告。It may take a few moments for the module to be started on the device and then reported back to IoT Hub. 刷新页面以查看更新的状态。Refresh the page to see an updated status. 状态代码:200 -OK 表示 IoT Edge 运行时状况良好并且运行正常。Status code: 200 –OK means that the IoT Edge runtime is healthy and is operating fine.

屏幕截图显示了 IoT Edge 运行时的状态值。

调用直接方法Invoke a direct method

接下来,让我们通过调用直接方法来测试示例。Next, lets test the sample by invoking a direct method. 阅读 IoT Edge 上的实时视频分析的直接方法,了解 lvaEdge 模块提供的所有直接方法。Read direct methods for Live Video Analytics on IoT Edge to understand all the direct methods provided by our lvaEdge module.

  1. 单击创建的边缘模块,此操作会将你转到其配置页面。Clicking on the edge module you created, will take you to its configuration page.

    屏幕截图显示了边缘模块的配置页面。

  2. 单击“直接方法”菜单选项。Click on the Direct Method menu option.

    备注

    正如你在当前页面上看到的那样,你需要在连接字符串部分中添加一个值。You will need to add a value in the Connection string sections as you can see on the current page. 无需隐藏或更改“设置名称”部分中的任何内容。You do not need to hide or change anything in the Setting name section. 可以让它处于公开状态。It is ok to let it be public.

    直接方法

  3. 在“方法名称”框中输入“GraphTopologyList”。Next, Enter "GraphTopologyList" in the Method Name box.

  4. 接下来,复制以下 JSON 有效负载并将其粘贴到有效负载框中。Next, copy and paste the below JSON payload in the payload box.

    {
        "@apiVersion" : "1.0"
    }
    
  5. 单击页面顶部的“调用方法”选项Click on “Invoke Method” option on top of the page

    直接方法

  6. 你应该会在“结果”框中看到状态 200 消息You should see a status 200 message in the Result box

    状态 200 消息

后续步骤Next steps

试用快速入门:入门 - IoT Edge 上的实时视频分析Try Quickstart: Get started - Live Video Analytics on IoT Edge

提示

如果继续学习上述快速入门,当使用 Visual Studio Code 调用直接方法时,将使用根据本文说明添加到 IoT 中心的设备,而不是默认的 lva-sample-deviceIf you proceed with the above quickstart, when invoking the direct methods using Visual Studio Code, you will use the device that was added to the IoT Hub via this article, instead of the default lva-sample-device.