使用命令行标记视频
本快速入门将引导您完成以下过程:
- 将一组视频复制到 Cloud Storage 中。
- 创建列有视频及其标签的 CSV 文件。
- 使用 AutoML Video 创建数据集,并训练和使用模型。
准备工作
设置项目
- 登录您的 Google Cloud 帐号。如果您是 Google Cloud 新手,请创建一个帐号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
创建或选择 Google Cloud 项目。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
-
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
-
-
Enable the AutoML and Cloud Storage APIs:
gcloud services enable storage-component.googleapis.com
automl.googleapis.com storage-api.googleapis.com - 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
创建或选择 Google Cloud 项目。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
-
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
-
-
Enable the AutoML and Cloud Storage APIs:
gcloud services enable storage-component.googleapis.com
automl.googleapis.com storage-api.googleapis.com - 将
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-east1
、us-west1
、europe-west1
、asia-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
name
。以下示例展示了此类响应,其中 project-number
是您的项目编号,operation-id
是为请求创建的长时间运行操作的 ID。
导入训练数据
REST
要导入训练数据时,请使用importData
方法。此方法要求您提供两个参数:
- 包含训练路径的 CSV 路径,
- 测试数据 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-east1
、us-west1
、europe-west1
、asia-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
VCN7506374678919774208
的响应。
获取导入操作的状态
您可以使用以下 curl
或 PowerShell 命令查询数据导入操作的状态。
REST
在使用任何请求数据之前,请先进行以下替换:
- operation-id:替换为导入数据操作的操作 ID。
- 注意:
- project-number:您的项目编号
- location-id:在其中添加注解的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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
{ "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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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
例如:
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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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
,且无错误列出,如以下示例所示。
批量预测任务完成后,预测输出结果将存储在您在命令中指定的 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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-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-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,则根据视频文件位置来确定区域。 - dataset-id:创建数据集时提供的 ID
HTTP 方法和网址:
DELETE https://automl.googleapis.com/v1beta1/dataset-name
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
后续步骤
- 了解分类工作流程