创建具有远程流式输入功能的入住人数统计应用

Vertex AI Vision 是一个由 AI 赋能的平台,可用于注入、分析和存储视频 数据。借助 Vertex AI Vision,您可以构建和部署 AI 应用。您可以利用 Vertex AI Vision 与其他产品组件的集成,构建端到端 Vertex AI Vision 解决方案。

如需开始使用 Vertex AI Vision 平台实现解决方案,请查看以下 Vertex AI Vision 概念和组件:

  • 数据流:表示解决方案中的视频流式传输层。数据流来源可以是实时视频(例如 IP 摄像头)或视频文件(例如 MP4 文件)。

  • 应用:实现数据流与 AI 处理器之间的连接,以便对视频执行机器学习操作。例如,您可以将摄像头画面连接到 AI 模型,以便统计从摄像头前经过的人数。

  • 媒体仓库:将数据流注入的视频存储到 Google Cloud 存储空间。将数据存储到此目标位置后,您可以查询对提取的数据流中的数据使用的 AI 处理器的分析输出和元数据。

目标

本教程介绍了如何执行以下操作:

  • 创建入座率统计应用。
  • 部署应用以供使用。
  • 设置远程机器以流式传输视频。
  • 将流式视频提取到应用中的数据流节点。
  • 在存储 Vertex AI Vision 的媒体仓库中搜索视频。

费用

在本文档中,您将使用的以下收费组件: Google Cloud

您可使用 价格计算器 根据您的预计使用情况来估算费用。

新 Google Cloud 用户可能有资格申请免费试用

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

准备工作

  1. 登录您的 Google Cloud 账号。 如果您是新手,请创建一个账号来评估我们的产品在实际场景中的表现。 Google Cloud新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine and Vision AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the following roles to the service account: Vision AI > Vision AI Editor, Compute Engine > Compute Instance Admin (beta), Storage > Storage Object Viewer.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    7. Click Continue.
    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. 安装 Google Cloud CLI。

  8. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  9. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  10. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Compute Engine and Vision AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  13. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the following roles to the service account: Vision AI > Vision AI Editor, Compute Engine > Compute Instance Admin (beta), Storage > Storage Object Viewer.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    7. Click Continue.
    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  14. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  15. 安装 Google Cloud CLI。

  16. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  17. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

仅当您从 Cloud Storage 存储桶复制示例视频文件时才需要此角色。

创建入座率统计应用

设置环境后,第一步是创建用于处理数据的应用。 应用可以视为连接以下各项的自动化流水线:

  • 数据注入:将视频 Feed 注入到数据流中。
  • 数据分析:提取后可以添加 AI 模型。可以对提取的视频信息执行任何计算机视觉操作。
  • 数据存储:视频 Feed 的两个版本(原始数据流和 AI 模型处理的数据流)可以存储在媒体仓库中。

在 Google Cloud 控制台中,应用以图表的形式表示。 此外,在 Vertex AI Vision 中,应用图必须至少包含两个节点:一个视频源节点(数据流),以及 至少 一个其他节点(处理模型或输出目标位置)。

创建空应用

您必须先创建一个空应用,然后才能填充应用图表。

控制台

在 Google Cloud 控制台中创建应用。

  1. 打开 Vertex AI Vision 信息中心的应用 标签页。

    前往“应用”标签页

  2. 点击加号创建 按钮。

  3. 输入 occupancy-count-app 作为应用名称,然后选择您的区域。

  4. 点击创建

添加应用组件节点

创建空应用后,您可以将以下三个节点添加到应用图表中:

  1. 注入节点:注入您创建的 Compute Engine 虚拟机实例发送的数据的数据流资源。
  2. 处理节点:对提取的数据执行操作的入座率分析模型。
  3. 存储节点:存储已处理视频的媒体仓库, 同时也是元数据存储区。仓库允许生成有关提取的视频数据的分析 信息,并存储 AI 模型推断出的有关数据的信息。

控制台

在控制台中向应用添加组件节点。

  1. 打开 Vertex AI Vision 信息中心的应用 标签页。

    前往“应用”标签页

  2. occupancy-count-app 行中,选择 查看图表。系统会将您转至处理流水线的图表可视化界面。

添加数据注入节点

  1. 如需添加输入数据流节点,请选择侧边菜单的连接器 部分中的数据流 选项。

  2. 在打开的信息流 菜单的来源 部分中,选择 添加信息流

  3. 添加数据流 菜单中,选择 注册新的 数据流 ,然后添加 occupancy-count-stream 作为数据流名称。

  4. 如需将数据流添加到应用图表,请点击添加数据流

添加数据处理节点

  1. 如需添加入座率统计模型节点,请选择侧边菜单的专用模型 部分中的入座率分析 选项。

  2. 保留默认选择 人员车辆

添加数据存储节点

  1. 如需添加输出目标位置(存储)节点,请选择侧边菜单的 连接器部分中的 Vertex AI Vision 的媒体仓库选项。

  2. Vertex AI Vision 的媒体仓库 菜单中,点击 连接仓库

  3. 连接仓库 菜单中,选择 创建新 仓库。将仓库命名为 occupancy-count-warehouse,并将 TTL 时长保留 14 天。

  4. 点击创建 按钮以添加仓库。

部署应用以供使用

使用所有必要组件构建端到端应用后,使用应用的最后一步是部署应用。

控制台

  1. 打开 Vertex AI Vision 信息中心的应用 标签页。

    前往“应用”标签页

  2. 在列表中,选择 occupancy-count-app 应用旁边的查看图表

  3. 在应用图表构建器页面中,点击 部署 按钮。

  4. 在以下确认对话框中,选择部署

    部署操作可能需要几分钟才能完成。部署完成后,节点旁边会显示绿色对勾标记。

    界面中已部署的应用

设置远程机器以流式传输视频

现在,您已部署入座率统计应用,该应用已准备好接收、处理和存储流式数据,您必须将视频数据实际流式传输到应用中。

在本教程中,您将创建一个托管视频的 Compute Engine 虚拟机实例,并从该虚拟机发送流式视频数据。

创建一个 Linux VM

从 Compute Engine 虚拟机实例发送视频的第一步是创建虚拟机实例。

控制台

  1. 在控制台中,进入虚拟机实例页面。

    转到虚拟机实例

  2. 选择您的项目并点击继续

  3. 点击创建实例

  4. 为您的虚拟机指定名称。如需了解详情,请参阅资源命名惯例

  5. 可选操作:更改此虚拟机的可用区。Compute Engine 会随机列出每个区域内的可用区,以鼓励跨多个可用区使用。

  6. 接受其余默认选项。如需详细了解这些 选项,请参阅创建并启动虚拟机

  7. 要创建并启动该虚拟机,请点击创建

设置虚拟机环境

虚拟机启动后,您可以使用控制台在浏览器中建立 SSH 连接。建立此连接后,您可以下载 vaictl 命令行工具,以便将视频注入到应用中。

控制台

与虚拟机建立 SSH 连接

  1. 在控制台中,进入虚拟机实例 页面。

    转到虚拟机实例

  2. 在您创建的实例行的连接 部分中,点击 SSH 。这会在新的浏览器窗口中打开 SSH 连接。

    界面中的 SSH 选项

下载 vaictl 命令行工具

  1. SSH-in-browser 窗口中,使用以下命令下载 Vertex AI Vision (vaictl) 命令行工具:

    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai_0.0-5_amd64.deb
    
  2. 请运行以下命令来安装命令行工具:

    sudo apt install ./visionai_0.0-5_amd64.deb
    
  3. 您可以运行以下命令来测试安装:

    vaictl --help
    

将视频文件提取到应用中

设置虚拟机环境后,您可以复制示例视频文件,然后使用 vaictl 将视频数据流式传输到入座率统计应用。

SSH-in-browser

将示例视频复制到虚拟机

  1. 在虚拟机的 SSH-in-browser 窗口中,使用以下 gcloud storage cp 命令复制示例视频。替换以下变量:
    • SOURCE:要使用的视频文件的位置。您可以使用自己的视频文件来源(例如 gs://BUCKET_NAME/FILENAME.mp4),也可以使用以下示例视频之一:
      • gs://cloud-samples-data/vertex-ai-vision/street_vehicles_people.mp4(包含人员和车辆的视频, 来源
      • gs://cloud-samples-data/vertex-ai-vision/highway_vehicles.mp4(仅包含车辆的视频, 来源
    gcloud storage cp SOURCE .

从虚拟机流式传输视频并将数据注入到应用中

  1. 如需将此本地视频文件发送到应用输入数据流,请使用以下命令。您必须进行以下变量替换:
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • LOCATION_ID:您的位置 ID。例如, us-central1。如需了解详情,请参阅 Cloud 位置
    • LOCAL_FILE.EXT:本地视频文件的文件名。 例如,my-video.mp4
    • --loop 标志:可选。循环文件数据以模拟 流式传输。

    此命令会将视频文件流式传输到数据流。如果您使用 --loop 标志,则视频会循环到数据流中,直到您 停止该命令为止:

    vaictl -p PROJECT_ID \
        -l LOCATION_ID \
        -c application-cluster-0 \
        --service-endpoint visionai.googleapis.com \
    send video-file to streams 'occupancy-count-stream' --file-path LOCAL_FILE.EXT --loop

从启动 vaictl 注入操作到视频显示在信息中心内,可能需要大约 100 秒。

数据流提取可用后,您可以在 Vertex AI Vision 信息中心的数据流 标签页中选择 occupancy-count-stream 数据流,以查看视频 Feed。

前往“数据流”标签页

界面中正在直播的实时视频的视图
在控制台中实时查看提取到数据流中的视频。 Google Cloud 视频来源Elizabeth Mavor(已添加像素化效果)。

在存储仓库中搜索视频内容

将视频数据注入到处理应用后,您可以查看分析后的视频数据,并根据入座率分析信息搜索数据。

控制台

  1. 打开 Vertex AI Vision 信息中心的仓库 标签页。

    前往“仓库”标签页

  2. 在列表中找到 occupancy-count-warehouse 仓库,然后点击 查看资产

  3. 人数统计车辆数统计 部分中,将最小值 设置为 1,并将最大值 设置为 5

  4. 如需过滤存储在 Vertex AI Vision 的媒体仓库中的已处理视频数据,请点击搜索

    界面中分面的存储数据视图
    在控制台中查看符合搜索条件的存储视频数据。 Google Cloud 视频来源Elizabeth Mavor(已应用搜索条件)。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 在 Google Cloud 控制台中,前往 管理资源 页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击 关闭以删除项目。

删除各个资源

删除 Compute Engine 虚拟机实例

  1. 在 Google Cloud 控制台中,进入 虚拟机实例 页面。

    转到虚拟机实例

  2. 选中要删除的实例对应的复选框。
  3. 如需删除实例,请依次点击 更多操作删除,然后按照说明操作。

删除仓库

  1. 在 Google Cloud 控制台中,进入 仓库 页面。

    前往“仓库”标签页

  2. 找到 您的 occupancy-count-warehouse 仓库。
  3. 如需删除仓库,请依次点击 操作删除仓库 ,然后按照说明操作。

删除数据流

  1. 在 Google Cloud 控制台中,进入 数据流 页面。

    前往“数据流”标签页

  2. 找到您的 occupancy-count-stream 数据流。
  3. 如需删除数据流,请依次点击 操作删除数据流 ,然后按照说明操作。

删除应用

  1. 在 Google Cloud 控制台中,进入 应用 页面。

    前往“应用”标签页

  2. 找到 您的 occupancy-count-app 应用。
  3. 如需删除应用,请依次点击 操作删除应用 ,然后按照说明操作。

后续步骤