快速入门:检测运动并在边缘设备上录制视频Quickstart: Detect motion and record video on edge devices

本快速入门介绍了如何在 IoT Edge 上使用实时视频分析来分析(模拟)IP 相机中的实时视频源。This quickstart shows you how to use Live Video Analytics on IoT Edge to analyze the live video feed from a (simulated) IP camera. 本快速入门介绍如何检测是否存在任何运动,如果存在,则将 MP4 视频剪辑录制到边缘设备上的本地文件系统中。It shows how to detect if any motion is present, and if so, record an MP4 video clip to the local file system on the edge device. 本快速入门将 Azure VM 用作 IoT Edge 设备,并且还使用模拟的实时视频流。The quickstart uses an Azure VM as an IoT Edge device and also uses a simulated live video stream.

本文基于用 C# 编写的示例代码。This article is based on sample code written in C#. 本文以检测运动并发出事件快速入门为基础。It builds on the Detect motion and emit events quickstart.

先决条件Prerequisites

提示

在安装 Azure IoT Tools 时,系统可能会提示安装 Docker。When installing Azure IoT Tools, you might be prompted to install Docker. 可以忽略此提示。Feel free to ignore the prompt.

观看示例视频Review the sample video

在为此快速入门设置 Azure 资源时,一个停车场短视频被复制到 Azure 中用作 IoT Edge 设备的 Linux VM 上。As you set up the Azure resources for this quickstart, a short video of a parking lot is copied to the Linux VM in Azure that's used as the IoT Edge device. 本教程将使用此视频文件模拟实时流。This video file will be used to simulate a live stream for this tutorial.

打开 VLC 媒体播放器等应用程序,选择 Ctrl+N,然后将此链接粘贴到停车场视频中并开始播放。Open an application like VLC media player, select Ctrl+N, and paste this link to the parking lot video to start playback. 大约在 5 秒的时候,一辆白色汽车在停车场间移动。At about the 5-second mark, a white car moves through the parking lot.

完成以下步骤,以使用 IoT Edge 上的实时视频分析功能检测汽车的运动,并从大约第 5 秒的标记处开始录制视频剪辑。Complete the following steps to use Live Video Analytics on IoT Edge to detect the motion of the car and record a video clip starting around the 5-second mark.

概述Overview

信号流

上图显示本快速入门中信号的流动方式。The preceding diagram shows how the signals flow in this quickstart. Edge 模块模拟托管实时流协议 (RTSP) 服务器的 IP 相机。An edge module simulates an IP camera that hosts a Real-Time Streaming Protocol (RTSP) server. RTSP 源节点从该服务器拉取视频源,并将视频帧发送到运动检测处理器节点。An RTSP source node pulls the video feed from this server and sends video frames to the motion detection processor node. RTSP 源会将同一视频帧发送到信号入口处理器节点,该节点在被事件触发前会保持关闭状态。The RTSP source sends the same video frames to a signal gate processor node, which remains closed until it's triggered by an event.

当运动检测处理器检测到视频中的运动时,它会将事件发送到信号入口处理器节点,并触发该节点。When the motion detection processor detects motion in the video, it sends an event to the signal gate processor node, triggering it. 门会打开,并将这一状态保持到配置的时间结束为止,并将视频帧发送到文件接收器节点。The gate opens for the configured duration of time, sending video frames to the file sink node. 该接收器节点以 MP4 文件形式将视频录制到边缘设备上的本地文件系统中。This sink node records the video as an MP4 file on the local file system of your edge device. 该文件保存在配置的位置。The file is saved in the configured location.

在本快速入门中,请执行以下操作:In this quickstart, you will:

  1. 创建并部署媒体图。Create and deploy the media graph.
  2. 解释结果。Interpret the results.
  3. 清理资源。Clean up resources.

检查和编辑示例文件Examine and edit the sample files

作为本快速入门的先决条件的一部分,请将示例代码下载到一个文件夹中。As part of the prerequisites for this quickstart, you downloaded the sample code to a folder. 按照以下步骤检查并编辑示例代码。Follow these steps to examine and edit the sample code.

  1. 在 Visual Studio Code 中,转到 src/edge。In Visual Studio Code, go to src/edge. 你可看到 .env 文件以及一些部署模板文件。You see your .env file and a few deployment template files.

    部署模板是指边缘设备的部署清单,其中使用变量指定某些属性。The deployment template refers to the deployment manifest for the edge device, where variables are used for some properties. 该 .env 文件包含这些变量的值。The .env file includes the values for those variables.

  2. 转到 src/cloud-to-device-console-app 文件夹。Go to the src/cloud-to-device-console-app folder. 你可在此处看到 appsettings.json 文件和一些其他文件:Here you see the appsettings.json file and a few other files:

    • c2d-console-app.csproj - Visual Studio Code 的项目文件。c2d-console-app.csproj - The project file for Visual Studio Code.

    • operations.json - 希望程序运行的操作的列表。operations.json - The list of operations that you want the program to run.

    • Program.cs - 示例程序代码。Program.cs - The sample program code. 此代码:This code:

      • 加载应用设置。Loads the app settings.
      • 调用 IoT Edge 模块上的实时视频分析公开的直接方法。Invokes direct methods that the Live Video Analytics on IoT Edge module exposes. 可以通过调用模块的直接方法来使用该模块分析实时视频流。You can use the module to analyze live video streams by invoking its direct methods.
      • 暂停以检查“终端”窗口中程序的输出,并检查“输出”窗口中模块生成的事件 。Pauses so you can examine the output from the program in the TERMINAL window and examine the events generated by the module in the OUTPUT window.
      • 调用直接方法以清理资源。Invokes direct methods to clean up resources.
  3. 编辑 operations.json 文件:Edit the operations.json file:

    • 将链接更改为图拓扑:Change the link to the graph topology:

      "topologyUrl" : "https://raw.githubusercontent.com/Azure/live-video-analytics/master/MediaGraph/topologies/evr-motion-files/topology.json"

    • GraphInstanceSet 下,编辑图拓扑的名称,使其与上一个链接中的值匹配:Under GraphInstanceSet, edit the name of the graph topology to match the value in the preceding link:

      "topologyName" : "EVRToFilesOnMotionDetection"

    • 编辑 RTSP URL,使其指向视频文件:Edit the RTSP URL to point to the video file:

      "value": "rtsp://rtspsim:554/media/lots_015.mkv"

    • GraphTopologyDelete 下,编辑名称:Under GraphTopologyDelete, edit the name:

      "name": "EVRToFilesOnMotionDetection"

查看 - 检查模块的状态Review - Check the modules' status

生成和部署 IoT Edge 部署清单步骤中,在 Visual Studio Code 中,展开 AZURE IOT 中心(在左下部分)下的“lva-sample-device”节点 。In the Generate and deploy the IoT Edge deployment manifest step, in Visual Studio Code, expand the lva-sample-device node under AZURE IOT HUB (in the lower-left section). 应会看到已部署以下模块:You should see the following modules deployed:

  • 实时视频分析模块,名为 lvaEdgeThe Live Video Analytics module, named lvaEdge

  • rtspsim 模块,可模拟 RTSP 服务器,充当实时视频源的源The rtspsim module, which simulates an RTSP server that acts as the source of a live video feed

    模块

备注

如果使用的是自己的边缘设备,而不是设置脚本预配的边缘设备,请转到你的边缘设备并使用管理员权限运行以下命令,以拉取并存储该快速入门所使用的示例视频文件:If you are using your own edge device instead of the one provisioned by our setup script, go to your edge device and run the following commands with admin rights, to pull and store the sample video file used for this quickstart:

mkdir /home/lvaadmin/samples
mkdir /home/lvaadmin/samples/input    
curl https://lvamedia.blob.core.windows.net/public/camera-300s.mkv > /home/lvaadmin/samples/input/camera-300s.mkv  
chown -R lvaadmin /home/lvaadmin/samples/  

查看 - 准备监视事件Review - Prepare for monitoring events

请确保已完成准备监视事件的步骤。Make sure you've completed the steps to Prepare to monitor events.

开始监视内置事件终结点

运行示例程序Run the sample program

  1. 在 Visual Studio Code 中,打开“扩展”选项卡(或按 Ctrl+Shift+X),然后搜索“Azure IoT 中心”。In Visual Studio Code, open the Extensions tab (or press Ctrl+Shift+X) and search for Azure IoT Hub.

  2. 右键单击并选择“扩展设置”。Right click and select Extension Settings.

    扩展设置

  3. 搜索并启用“显示详细消息”。Search and enable "Show Verbose Message".

    显示详细消息

  4. 通过选择 F5 键启动调试会话。Start a debugging session by selecting the F5 key. “终端”窗口会打印一些消息。The TERMINAL window prints some messages.

  5. operations.json 代码调用直接方法 GraphTopologyListGraphInstanceListThe operations.json code calls the direct methods GraphTopologyList and GraphInstanceList. 如果在学完先前的快速入门后清理了资源,则该过程将返回空列表,然后暂停。If you cleaned up resources after previous quickstarts, then this process will return empty lists and then pause. 按 Enter 键。Select the Enter key.

    --------------------------------------------------------------------------
    Executing operation GraphTopologyList
    -----------------------  Request: GraphTopologyList  --------------------------------------------------
    {
      "@apiVersion": "1.0"
    }
    ---------------  Response: GraphTopologyList - Status: 200  ---------------
    {
      "value": []
    }
    --------------------------------------------------------------------------
    Executing operation WaitForInput
    Press Enter to continue
    

“终端”窗口将显示下一组直接方法调用:The TERMINAL window shows the next set of direct method calls:

  • 使用前面的 topologyUrl 调用 GraphTopologySetA call to GraphTopologySet that uses the topologyUrl
  • GraphInstanceSet 的调用,该调用使用以下正文:A call to GraphInstanceSet that uses the following body:
{
  "@apiVersion": "1.0",
  "name": "Sample-Graph",
  "properties": {
    "topologyName": "EVRToFilesOnMotionDetection",
    "description": "Sample graph description",
    "parameters": [
      {
        "name": "rtspUrl",
        "value": "rtsp://rtspsim:554/media/lots_015.mkv"
      },
      {
        "name": "rtspUserName",
        "value": "testuser"
      },
      {
        "name": "rtspPassword",
        "value": "testpassword"
      }
    ]
  }
}
  • GraphInstanceActivate 的调用,用于启动图形实例和视频流。A call to GraphInstanceActivate that starts the graph instance and the flow of video.
  • GraphInstanceList 的第二次调用,显示图形实例处于正在运行状态。A second call to GraphInstanceList that shows that the graph instance is in the running state .
  1. “终端”窗口中的输出会在出现 Press Enter to continue 时暂停。The output in the TERMINAL window pauses at Press Enter to continue. 暂时不要选择 Enter。Don't select Enter yet. 向上滚动,查看调用的直接方法的 JSON 响应有效负载。Scroll up to see the JSON response payloads for the direct methods that you invoked.

  2. 切换到 Visual Studio Code 中的“输出”窗口。Switch to the OUTPUT window in Visual Studio Code. 你将看到 IoT Edge 模块上的实时视频分析正发送到 IoT 中心的消息。You see the messages that the Live Video Analytics on IoT Edge module is sending to the IoT hub. 本快速入门中的以下部分将讨论这些消息。The following section of this quickstart discusses these messages.

  3. 媒体图将继续运行并打印结果。The media graph continues to run and print results. RTSP 模拟器不断循环源视频。The RTSP simulator keeps looping the source video. 若要停止媒体图,请返回“终端”窗口,并选择 Enter。To stop the media graph, return to the TERMINAL window and select Enter.

    接下来的一系列调用会清理资源:The next series of calls cleans up the resources:

    • 调用 GraphInstanceDeactivate 停用图形实例。A call to GraphInstanceDeactivate deactivates the graph instance.
    • 调用 GraphInstanceDelete 删除该实例。A call to GraphInstanceDelete deletes the instance.
    • 调用 GraphTopologyDelete 删除拓扑。A call to GraphTopologyDelete deletes the topology.
    • 最后一次调用 GraphTopologyList 显示该列表现在为空。A final call to GraphTopologyList shows that the list is now empty.

解释结果Interpret results

运行媒体图时,来自运动检测器处理器节点的结果将通过 IoT 中心接收器节点发送到 IoT 中心。When you run the media graph, the results from the motion detector processor node pass through the IoT Hub sink node to the IoT hub. Visual Studio Code 的“输出”窗口中显示的消息包含 body 部分和 applicationProperties 部分。The messages you see in the OUTPUT window of Visual Studio Code contain a body section and an applicationProperties section. 有关详细信息,请参阅创建和读取 IoT 中心消息For more information, see Create and read IoT Hub messages.

在下面的消息中,实时视频分析模块定义了应用程序属性和正文内容。In the following messages, the Live Video Analytics module defines the application properties and the content of the body.

MediaSessionEstablished 事件MediaSessionEstablished event

对媒体图进行实例化后,RTSP 源节点尝试连接到在 rtspsim-live555 容器上运行的 RTSP 服务器。When a media graph is instantiated, the RTSP source node attempts to connect to the RTSP server that runs on the rtspsim-live555 container. 如果连接成功,则打印以下事件。If the connection succeeds, the following event is printed.

[IoTHubMonitor] [05:37:21 AM] Message received from [lva-sample-device/lvaEdge]:
{  
"body": {
"sdp": "SDP:\nv=0\r\no=- 1586450538111534 1 IN IP4 xxx.xxx.xxx.xxx\r\ns=Matroska video+audio+(optional)subtitles, streamed by the LIVE555 Media Server\r\ni=media/camera-300s.mkv\r\nt=0 0\r\na=tool:LIVE555 Streaming Media v2020.03.06\r\na=type:broadcast\r\na=control:*\r\na=range:npt=0-300.000\r\na=x-qt-text-nam:Matroska video+audio+(optional)subtitles, streamed by the LIVE555 Media Server\r\na=x-qt-text-inf:media/camera-300s.mkv\r\nm=video 0 RTP/AVP 96\r\nc=IN IP4 0.0.0.0\r\nb=AS:500\r\na=rtpmap:96 H264/90000\r\na=fmtp:96 packetization-mode=1;profile-level-id=4D0029;sprop-parameter-sets={SPS}\r\na=control:track1\r\n"  
},  
"applicationProperties": {  
    "dataVersion": "1.0",  
    "topic": "/subscriptions/{subscriptionID}/resourceGroups/{name}/providers/microsoft.media/mediaservices/hubname",  
    "subject": "/graphInstances/Sample-Graph-1/sources/rtspSource",  
    "eventType": "Microsoft.Media.MediaGraph.Diagnostics.MediaSessionEstablished",  
    "eventTime": "2020-05-21T05:37:21.398Z",
    }  
}

在以上脚本中:In the preceding output:

  • 消息为诊断事件 MediaSessionEstablishedThe message is a diagnostics event, MediaSessionEstablished. 它指示 RTSP 源节点(使用者)建立了与 RTSP 模拟器的连接,并开始接收(模拟的)实时馈送。It indicates that the RTSP source node (the subject) established a connection with the RTSP simulator and has begun to receive a (simulated) live feed.
  • applicationProperties 中的 subject 引用生成消息的图形拓扑中的节点。In applicationProperties, subject references the node in the graph topology from which the message was generated. 在本例中,该消息来自 RTSP 源节点。In this case, the message originates from the RTSP source node.
  • applicationProperties 中的 eventType 指示此事件是诊断事件。In applicationProperties, eventType indicates that this event is a diagnostics event.
  • eventTime 值为事件发生的时间。The eventTime value is the time when the event occurred.
  • body 部分包含有关诊断事件的数据。The body section contains data about the diagnostics event. 在本例中,数据包含会话描述协议 (SDP) 详细信息。In this case, the data comprises the Session Description Protocol (SDP) details.

RecordingStarted 事件RecordingStarted event

检测到运动时,将激活信号入口处理器节点,媒体图中的文件接收器节点将开始写入 MP4 文件。When motion is detected, the signal gate processor node is activated, and the file sink node in the media graph starts to the write an MP4 file. 文件接收器节点发送操作事件。The file sink node sends an operational event. type 设置为 motion,指示它是运动检测处理器的结果。The type is set to motion to indicate that it's a result from the motion detection processor. eventTime 值是运动发生的 UTC 时间。The eventTime value is the UTC time at which the motion occurred. 有关该过程的详细信息,请参阅本快速入门中的概述部分。For more information about this process, see the Overview section in this quickstart.

以下是此消息的示例:Here's an example of this message:

[IoTHubMonitor] [05:37:27 AM] Message received from [lva-sample-device/lvaEdge]:
{
  "body": {
    "outputType": "filePath",
    "outputLocation": "/var/media/sampleFilesFromEVR-filesinkOutput-20200521T053726Z.mp4"
  },
  "applicationProperties": {
    "topic": "/subscriptions/{subscriptionID}/resourceGroups/{name}/providers/microsoft.media/mediaservices/hubname",  
    "subject": "/graphInstances/Sample-Graph-1/sinks/fileSink",
    "eventType": "Microsoft.Media.Graph.Operational.RecordingStarted",
    "eventTime": "2020-05-21T05:37:27.713Z",
    "dataVersion": "1.0"
  }
}

在上面的消息中:In the preceding message:

  • applicationProperties 中的 subject 引用生成消息的媒体图中的节点。In applicationProperties, subject references the node in the media graph from which the message was generated. 在本例中,该消息来自文件接收器节点。In this case, the message originates from the file sink node.
  • applicationProperties 中的 eventType 指示此事件是操作事件。In applicationProperties, eventType indicates that this event is operational.
  • eventTime 值为事件发生的时间。The eventTime value is the time when the event occurred. 此时间为 MediaSessionEstablished 后视频开始流动后的 5 到 6 秒。This time is 5 to 6 seconds after MediaSessionEstablished and after video starts to flow. 该时间对应于汽车开始驶入停车场时的第 5-6 秒标记处。This time corresponds to the 5-to-6-second mark when the car started to move into the parking lot.
  • body 部分包含有关操作事件的数据。The body section contains data about the operational event. 在本例中,数据包括 outputTypeoutputLocationIn this case, the data comprises outputType and outputLocation.
  • outputType 变量表示此信息与文件路径有关。The outputType variable indicates that this information is about the file path.
  • outputLocation 值是 MP4 文件在边缘模块中的位置。The outputLocation value is the location of the MP4 file in the edge module.

RecordingStopped 和 RecordingAvailable 事件RecordingStopped and RecordingAvailable events

如果在图形拓扑中检查信号入口处理器节点的属性,你会看到激活时间设置为 5 秒。If you examine the properties of the signal gate processor node in the graph topology, you see that the activation times are set to 5 seconds. 因此,在收到 RecordingStarted 事件后约 5 秒,你将获得:So about 5 seconds after the RecordingStarted event is received, you get:

  • RecordingStopped 事件,指示录制已停止。A RecordingStopped event, indicating that the recording has stopped.
  • RecordingAvailable 事件,指示现在可查看 MP4 文件。A RecordingAvailable event, indicating that the MP4 file can now be used for viewing.

这两个事件的发出时间通常彼此相隔数秒。The two events are typically emitted within seconds of each other.

播放 MP4 文件剪辑Play the MP4 clip

通过使用 OUTPUT_VIDEO_FOLDER_ON_DEVICE 密钥,将 MP4 文件写入在 .env 文件中配置的边缘设备上的目录中。The MP4 files are written to a directory on the edge device that you configured in the .env file by using the OUTPUT_VIDEO_FOLDER_ON_DEVICE key. 如果使用了默认值,则结果应位于 /var/media/ 文件夹中。If you used the default value, then the results should be in the /var/media/ folder.

播放 MP4 文件剪辑:To play the MP4 clip:

  1. 转到资源组,找到 VM 进行连接。Go to your resource group, find the VM, and then connect.

    资源组

    VM

  2. 使用设置 Azure 资源时生成的凭据登录。Sign in by using the credentials that were generated when you set up your Azure resources.

  3. 在命令提示符下,转到相关目录。At the command prompt, go to the relevant directory. 默认位置为 /var/media。The default location is /var/media. 应会在目录中看到 MP4 文件。You should see the MP4 files in the directory.

    输出

  4. 使用安全复制 (SCP) 将文件复制到本地计算机。Use Secure Copy (SCP) to copy the files to your local machine.

  5. 使用 VLC 媒体播放器或任何其他 MP4 文件播放器播放这些文件。Play the files by using VLC media player or any other MP4 player.

清理资源Clean up resources

如果想学习其他快速入门,请保留所创建的资源。If you intend to try the other quickstarts, then keep the resources you created. 否则,请在 Azure 门户中,转到资源组,选择运行本快速入门所用的资源组,然后删除所有资源。Otherwise, in the Azure portal, go to your resource groups, select the resource group where you ran this quickstart, and then delete all of the resources.

后续步骤Next steps