使用命令行标记视频

本快速入门将引导您完成以下过程:

  • 将一组视频复制到 Cloud Storage 中。
  • 创建列有视频及其标签的 CSV 文件。
  • 使用 AutoML Video 创建数据集,并训练和使用模型。

准备工作

设置项目

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 安装 Google Cloud CLI。
  3. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  4. 创建或选择 Google Cloud 项目

    • 创建 Google Cloud 项目:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替换为您要创建的 Google Cloud 项目的名称。

    • 选择您创建的 Google Cloud 项目:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替换为您的 Google Cloud 项目 名称。

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. Enable the AutoML and Cloud Storage APIs:

    gcloud services enable storage-component.googleapis.com automl.googleapis.com storage-api.googleapis.com
  7. 安装 Google Cloud CLI。
  8. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  9. 创建或选择 Google Cloud 项目

    • 创建 Google Cloud 项目:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替换为您要创建的 Google Cloud 项目的名称。

    • 选择您创建的 Google Cloud 项目:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替换为您的 Google Cloud 项目 名称。

  10. 确保您的 Google Cloud 项目已启用结算功能

  11. Enable the AutoML and Cloud Storage APIs:

    gcloud services enable storage-component.googleapis.com automl.googleapis.com storage-api.googleapis.com
  12. PROJECT_ID 环境变量设置为项目 ID
    export PROJECT_ID=PROJECT_ID
    AutoML API 调用和资源名称中包含您的项目 ID。PROJECT_ID 环境变量提供了一种指定此 ID 的便捷方式。

创建数据集并导入训练数据

创建数据集

确定数据集的名称,并使用以下 curl 或 PowerShell 命令创建采用该名称的新数据集。

REST

在使用任何请求数据之前,请先进行以下替换:

  • dataset-name:界面中显示的数据集的名称
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

POST  https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets

请求 JSON 正文:

{
  "displayName": "dataset-name",
  "videoClassificationDatasetMetadata": {
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
" https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri " https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets" | Select-Object -Expand Content
如果响应成功,AutoML Video Intelligence 分类 API 将为您的操作返回 name。以下示例展示了此类响应,其中 project-number 是您的项目编号,operation-id 是为请求创建的长时间运行操作的 ID。

导入训练数据

REST

要导入训练数据时,请使用 importData 方法。此方法要求您提供两个参数:

  1. 包含训练路径的 CSV 路径,
  2. 测试数据 CSV 文件。注意:这些文件位于 Cloud Storage 上的“automl-video-demo-data”存储桶中。

在使用任何请求数据之前,请先进行以下替换:

  • input-uri:包含要添加注释的文件的 Cloud Storage 存储桶(包括文件名)。必须以 gs:// 开头。例如:
    "inputUris": ["gs://automl-video-demo-data/hmdb_split1.csv"]
  • dataset-id:替换为数据集的数据集标识符(而不是显示名)。例如 VCN4798585402963263488
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

POST  https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets/dataset-id:importData

请求 JSON 正文:

{
   "inputConfig": {
      "gcsSource": {
         "inputUris": input-uri
      }
   }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
" https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets/dataset-id:importData"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri " https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets/dataset-id:importData" | Select-Object -Expand Content
您应该会收到数据导入操作的 ID。该示例显示包含导入操作 ID VCN7506374678919774208 的响应。

获取导入操作的状态

您可以使用以下 curl 或 PowerShell 命令查询数据导入操作的状态。

REST

在使用任何请求数据之前,请先进行以下替换:

  • operation-id:替换为导入数据操作的操作 ID。
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id" | Select-Object -Expand Content
导入操作可能需要一些时间才能完成。导入任务完成后,您会看到 done: true 处于未列出任何错误的操作状态,如下例所示。

列出所有数据集

使用以下 curl 或 PowerShell 命令获取数据集列表以及导入数据集的示例视频数量。

REST

在使用任何请求数据之前,请先进行以下替换:

  • project-number:您的项目编号
  • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,则根据视频文件位置来确定区域。

HTTP 方法和网址:

 https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

训练模型

启动模型训练操作

创建数据集并将训练数据导入数据集后,您可以训练模型。

使用以下 curl 或 PowerShell 命令训练模型。

REST

在使用任何请求数据之前,请先进行以下替换:

  • dataset-id:此 ID 是数据集名称的最后一个元素。例如,如果数据集的名称为 projects/434039606874/locations/us-central1/datasets/VCN3104518874390609379,则数据集 ID 为 VCN3104518874390609379
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

POST https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models

请求 JSON 正文:

{
  "displayName": "test_model",
  "dataset_id": "dataset-id",
  "videoClassificationModelMetadata": {}
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models" | Select-Object -Expand Content
操作开始后,响应中会提供 operation-id,例如 VCN123...
{
  "name": "projects/project-number/locations/location-id/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "progressPercentage": 100,
    "createTime": "2020-02-27T01:56:28.395640Z",
    "updateTime": "2020-02-27T02:04:12.336070Z"
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.Model",
    "name": "projects/project-number/locations/location-id/models/operation-id",
    "createTime": "2020-02-27T02:00:22.329970Z",
    "videoClassificationModelMetadata": {
      "trainBudget": "1",
      "trainCost": "1",
      "stopReason": "BUDGET_REACHED"
    },
    "displayName": "a_98487760535e48319dd204e6394670"
  }
}

获取模型训练操作的状态

使用以下 curl 或 PowerShell 命令训练模型。

REST

在使用任何请求数据之前,请先进行以下替换:

  • operation-id:替换为训练操作的操作 ID。
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id" | Select-Object -Expand Content
操作完成后,您会看到 done: true,并且未列出任何错误。

验证模型可用性

成功完成模型训练操作后,您可以使用以下 curl 或 PowerShell 命令列出项目的模型,以验证模型是否可用。

REST

在使用任何请求数据之前,请先进行以下替换:

  • dataset-id:替换为数据集的数据集标识符(而不是显示名)。
  • model-name:替换为您为模型选择的名称
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id" | Select-Object -Expand Content
您应该会收到数据导入操作的 ID。
例如:VCN1741767155885539328

进行预测

您可以使用 batchPredict 命令请求对视频进行注释(预测)。batchPredict 命令接受 Cloud Storage 存储桶中的 CSV 文件(其中包含要添加注释的视频的路径,以及要添加注释的视频片段的开始时间和结束时间)作为输入参数。在此快速入门中,该 CSV 文件名为 hmdb_split1_test_gs_predict.csv

运行以下 curl 或 PowerShell 命令以发出批量(异步)预测请求。

REST

在使用任何请求数据之前,请先进行以下替换:

  • input-uri:包含要添加注释的文件的 Cloud Storage 存储桶(包括文件名)。必须以 gs:// 开头。例如:
    "inputUris": ["gs://automl-video-demo-data/hmdb_split1_test_gs_predict.csv"]
  • output-bucket 替换为您的 Cloud Storage 存储桶名称。例如 my-project-vcm
  • object-id:替换为导入数据操作的操作 ID。
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

POST https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models/model-id:batchPredict

请求 JSON 正文:

{
  "inputConfig": {
    "gcsSource": {
      "inputUris": [input-uri]
    }
  },
  "outputConfig": {
    "gcsDestination": {
      "outputUriPrefix": "gs://output-bucket/object-id"
    }
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models/model-id:batchPredict "

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models/model-id:batchPredict " | Select-Object -Expand Content

您应该会收到批量预测请求的操作 ID。例如:VCN926615623331479552

获取预测操作的状态

您可以使用以下 curl 或 PowerShell 命令查询批量预测操作的状态。

REST

在使用任何请求数据之前,请先进行以下替换:

  • operation-id:替换为导入数据操作的操作 ID。
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
"https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-number" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/operations/operation-id" | Select-Object -Expand Content
根据您在 CSV 文件中指定的视频数量,批量预测任务可能需要一些时间才能完成。任务完成后,您将在操作状态中看到 done: true,且无错误列出,如以下示例所示。

批量预测任务完成后,预测输出结果将存储在您在命令中指定的 Cloud Storage 存储桶中。每个视频片段都有一个 JSON 文件。例如:
my-video-01.avi.json

{
  "inputUris": ["gs://automl-video-demo-data/sample_video.avi"]
  "segment_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 4,
          "nanos": 960000000
        }
      },
      "confidence": 0.43253016
    }, {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 4,
          "nanos": 960000000
        }
      },
      "confidence": 0.56746984
    } ],
    "frames": [ ]
  } ],
  "shot_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 5
        }
      },
      "confidence": 0.43253016
    }, {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 5
        }
      },
      "confidence": 0.56746984
    } ],
    "frames": [ ]
  } ],
  "one_second_sliding_window_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ ],
    "frames": [ {
      "time_offset": {
        "nanos": 800000000
      },
      "confidence": 0.54533803
    }, {
      "time_offset": {
        "nanos": 800000000
      },
      ...
      "confidence": 0.57945728
    }, {
      "time_offset": {
        "seconds": 4,
        "nanos": 300000000
      },
      "confidence": 0.42054281
    } ]
  } ],
  "object_annotations": [ ],
  "error": {
    "details": [ ]
  }
}

清理

为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。

如果您不再需要模型和相关数据集,可以将其删除。

列出模型

您可以使用以下 curl 或 PowerShell 命令列出项目的模型及其标识符:

REST

在使用任何请求数据之前,请先进行以下替换:

  • model-name:创建模型时的响应提供的模型全名。全名的格式如下:projects/project-number/locations/location-id/models
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

删除模型

您可以使用以下命令删除模型。

REST

在使用任何请求数据之前,请先进行以下替换:

  • model-id:将替换为模型的标识符
  • 注意:
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,系统将根据视频文件位置确定区域。

HTTP 方法和网址:

DELETE https://automl.googleapis.com/v1beta1/projects/project-number/locations/test/models/model-id

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

列出数据集

您可以使用以下 curl 或 PowerShell 命令列出项目的数据集及其标识符:

REST

在使用任何请求数据之前,请先进行以下替换:

  • project-number:您的项目编号
  • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,则根据视频文件位置来确定区域。

HTTP 方法和网址:

 https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

删除数据集

您可以使用以下 curl 或 PowerShell 命令删除数据集。

REST

在使用任何请求数据之前,请先进行以下替换:

  • dataset-name:数据集的全名,来自创建数据集时的响应。全名的格式为:
    projects/project-number/locations/location-id/datasets/dataset-id
    • project-number:您的项目编号
    • location-id:在其中添加注解的 Cloud 区域。支持的云区域为:us-east1us-west1europe-west1asia-east1。如果未指定区域,则根据视频文件位置来确定区域。
    • dataset-id:创建数据集时提供的 ID

HTTP 方法和网址:

DELETE  https://automl.googleapis.com/v1beta1/dataset-name

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

后续步骤