使用命令行跟踪视频对象
本快速入门将引导您完成以下过程:
- 将一组视频复制到 Cloud Storage 中。
- 创建列有视频及其标签的 CSV 文件。
- 使用 AutoML Video 对象跟踪创建数据集,以训练和使用模型。
准备工作
设置项目
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID
替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID
替换为您的 Google Cloud 项目 名称。
-
-
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 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID
替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID
替换为您的 Google Cloud 项目 名称。
-
-
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
下面显示了如何发送 POST 请求。该示例使用 Google Cloud CLI 创建访问令牌。如需了解如何安装 gcloud CLI,请参阅 AutoML Video Intelligence 对象跟踪快速入门。在使用任何请求数据之前,请先进行以下替换:
- dataset-name:目标数据集的名称。
例如my_dataset_01
。 - 注意:
- 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", "videoObjectTrackingDatasetMetadata": { } }
如需发送请求,请选择以下方式之一:
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
VOT12345....
导入训练数据
REST
要导入训练数据时,请使用importData
方法。此方法要求您提供两个参数:
- 包含训练路径的 CSV 路径,
- 测试数据 CSV 文件。注意:这些文件位于 Cloud Storage 上的“automl-video-demo-data”存储桶中。
在使用任何请求数据之前,请先进行以下替换:
- dataset-id:您的数据集的 ID。此 ID 是数据集名称的最后一个元素。例如:
- 数据集名称:
projects/project-number/locations/location-id/datasets/3104518874390609379
- 数据集 ID:
3104518874390609379
- 数据集名称:
- bucket-name:替换为您在其中存储模型训练文件列表 CSV 文件的 Cloud Storage 存储桶的名称。
- csv-file-name:替换为模型训练文件列表 CSV 文件的名称。
- 注意:
- 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": ["gs://bucket-name/csv-file-name.csv"] } } }
如需发送请求,请选择以下方式之一:
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
VOT7506374678919774208
的响应。
获取导入操作的状态
您可以使用以下 curl
或 PowerShell 命令查询数据导入操作的状态。
REST
在使用任何请求数据之前,请先进行以下替换:
- operation-id:是为请求创建的长时间运行的操作的 ID,并在启动操作时在响应中提供,例如
VOT12345....
- 注意:
- 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
,但不会列出任何错误,如上例所示。- operation-name:AutoML Video Intelligence 对象跟踪 API 返回的操作名称。操作名称采用
projects/project-number/locations/location-id/operations/operation-id
格式
列出所有数据集
使用以下 curl
或 PowerShell 命令获取数据集列表以及导入数据集的示例视频数量。
REST
在使用任何请求数据之前,请先进行以下替换:
- 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/datasets
如需发送请求,请选择以下方式之一:
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/datasets "
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/datasets " | Select-Object -Expand Content
VOT3940649673949184000
是为请求创建的长时间运行的操作的操作 ID,并会在您启动操作时在响应中提供。
训练模型
启动模型训练操作
创建数据集并将训练数据导入数据集后,您可以训练自定义模型。 使用以下 curl
或 PowerShell 命令训练模型。
REST
在使用任何请求数据之前,请先进行以下替换:
- dataset-id:目标数据集的名称。例如:
my_dataset_01
显示名。 - model-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/models
请求 JSON 正文:
{ "datasetId": "dataset-id", "displayName": "model-name", "videoObjectTrackingModelMetadata": {}, }
如需发送请求,请选择以下方式之一:
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
VOT1741767155885539328
的响应。
获取模型训练操作的状态
您可以使用以下 curl
或 PowerShell 命令查询模型训练操作的状态。
REST
在使用任何请求数据之前,请先进行以下替换:
- operation-name:AutoML Video Intelligence 对象跟踪 API 返回的操作名称。操作名称采用
projects/project-number/locations/location-id/operations/operation-id
格式 - 注意:
- project-number:您的项目编号
- location-id:在其中添加注解的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,系统将根据视频文件位置确定区域。
HTTP 方法和网址:
GET https://automl.googleapis.com/v1beta1/operation-name
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-number" \
"https://automl.googleapis.com/v1beta1/operation-name"
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/operation-name" | Select-Object -Expand Content
done: true
,并且未列出任何错误。
验证模型可用性
模型训练操作成功完成后,您可以使用以下命令列出项目的模型,从而验证模型是否可用。
REST
在使用任何请求数据之前,请先进行以下替换:
- 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 响应:
进行预测
您可以使用 batchPredict 方法为视频请求注释(预测)。batchPredict
方法需要两个输入:
- 存储在 Cloud Storage 存储桶中的 CSV 文件,其中包含要添加注释的视频的路径,
- 要添加注释的视频片段的开始时间和结束时间。
在本快速入门中,请使用 traffic_video_batch_predict.csv
CSV 文件。
REST
在使用任何请求数据之前,请先进行以下替换:
- model-id:将替换为模型的标识符
- input-uri:包含要添加注释的文件的 Cloud Storage 存储桶(包括文件名)。必须以
gs://
开头。
例如:"inputUris": ["gs://automl-video-demo-data/traffic_videos/traffic_video_batch_predict.csv"],
。 - output-bucket:替换为包含预测结果的 Google Cloud Storage 存储桶。
- object-id:替换为对象名称,该名称用于标识在 Cloud Storage 存储桶中的哪个位置存储预测请求输出结果。注意:您必须拥有 Cloud Storage 存储桶的写入权限。
- 注意:
- 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" } } "params": { "score_threshold": "0.0" } }
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
获取预测操作的状态
您可以使用以下 curl
或 PowerShell 命令查询批量预测操作的状态。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-number:您项目的编号
- location-id:在其中添加注解的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,系统将根据视频文件位置确定区域。 - operation-id:是为请求创建的长时间运行的操作的 ID,并在启动操作时在响应中提供,例如
VOT12345....
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
projects/project-number/locations/location-id/operations/operation-id
格式
批量预测任务完成后,预测输出结果将存储在您在命令中指定的 Cloud Storage 存储桶中。每个视频片段都有一个 JSON 文件。JSON 文件的格式类似于 my-video-01.avi.json,其中文件扩展名 .json 附加到原始文件名。
{ "inputUris": ["automl-video-demo-data/sample_video.avi"], "object_annotations": [ { "annotation_spec": { "display_name": "Cat", "description": "Cat" }, "confidence": 0.43253016 "frames": [ { "frame": { "time_offset": { "seconds": 4, "nanos": 960000000 }, "normalized_bounding_box": { "x_min": 0.1, "y_min": 0.1, "x_max": 0.8, "y_max": 0.8 } } }, { "frame": { "time_offset": { "seconds": 5, "nanos": 960000000 }, "normalized_bounding_box": { "x_min": 0.2, "y_min": 0.2, "x_max": 0.9, "y_max": 0.9 } } } ], "segment": { "start_time_offset": { "seconds": 4, "nanos": 960000000 }, "end_time_offset": { "seconds": 5, "nanos": 960000000 } } } ], "error": { "details": [ ] } }
清理
如果您不再需要自定义模型和相关数据集,可以将其删除。
删除模型
您可以使用以下 curl
或 PowerShell 命令删除模型。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-number:您项目的编号
- location-id:在其中添加注解的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,系统将根据视频文件位置确定区域。 - model-id:将替换为模型的标识符。
HTTP 方法和网址:
DELETE https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/models/model-id
如需发送您的请求,请展开以下选项之一:
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
删除数据集
您可以使用以下 curl
或 PowerShell 命令删除数据集。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-number:您项目的编号
- location-id:在其中添加注解的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,系统将根据视频文件位置确定区域。 - datase-id:将替换为数据集 ID 的标识符。
HTTP 方法和网址:
DELETE https://automl.googleapis.com/v1beta1/projects/project-number/locations/location-id/datasets/dataset-id
如需发送您的请求,请展开以下选项之一:
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。