使用命令行为视频添加注释
本页面介绍如何为您拥有的项目使用 curl 发出 Video Intelligence API 请求。
您可以按照本页中所述的步骤操作,也可以尝试将本快速入门作为 Google Cloud 培训实验室。
准备工作
- 登录您的 Google Cloud 账号。 如果您是新手,请创建一个账号来评估我们的产品在实际场景中的表现。 Google Cloud新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Video Intelligence API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Video Intelligence API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
所需的角色
如需获得为视频添加注释所需的权限,请让您的管理员向您授予项目的以下 IAM 角色:
-
Service Usage Consumer (
roles/serviceusage.serviceUsageConsumer) -
Storage Admin (
roles/storage.admin)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。
创建存储桶
在开发环境中,使用 gcloud storage buckets create
命令 创建存储桶。
运行该命令之前,请替换以下值:
BUCKET_LOCATION:您要在存储桶中存储数据的地理位置。例如,US。
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION
如果请求成功,该命令将返回以下消息:
Creating gs://videointelligence-quickstart-bucket/...
将视频上传到存储桶
在开发环境中,使用 gcloud storage cp
命令 将视频上传到您创建的存储桶。
运行该命令之前,请替换以下值:
OBJECT_LOCATION:对象的本地路径。例如,Desktop/dog.mp4。
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket
如果成功,则响应类似如下示例:
Completed files 1/1 | 164.3kiB/164.3kiB
为视频添加注释
gcloud
使用 gcloud CLI,对要分析的视频的路径调用 detect-labels 命令。
运行该命令之前,请替换以下值:
YOUR_OBJECT:您上传的视频的文件名。例如,dog.mp4。
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT
命令行
使用 curl 向 videos:annotate 方法发出 POST 请求。
运行该命令之前,请替换以下值:
YOUR_OBJECT:您上传的视频的文件名。例如,dog.mp4。
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data '{"inputUri":"gs://videointelligence-quickstart-bucket/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\ "https://videointelligence.googleapis.com/v1/videos:annotate"
Video Intelligence API 创建操作以处理您的请求。 响应包括操作名称: 响应包含操作名称:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID" }
您可以通过调用 v1.operations
端点来请求有关操作的信息,将下面示例中的 OPERATION_NAME 替换为上一步中返回的名称:
curl -X GET \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ https://videointelligence.googleapis.com/v1/OPERATION_NAME
此命令会返回与您的操作相关的信息。如果操作已完成,则包含一个 done 字段并将其设置为 true:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress", "annotationProgress": [ { "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT", "progressPercent": 100, "startTime": "2020-04-01T22:13:17.978847Z", "updateTime": "2020-04-01T22:13:29.576004Z" } ] }, "done": true, ... }
大约一分钟后,请求会返回注解结果。结果应类似于以下内容:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress", "annotationProgress": [ { "inputUri": "videointelligence-quickstart-bucket/YOUR_OBJECT", "progressPercent": 100, "startTime": "2020-04-01T22:13:17.978847Z", "updateTime": "2020-04-01T22:13:29.576004Z" } ] }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse", "annotationResults": [ { "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT", "segmentLabelAnnotations": [ { "entity": { "entityId": "/m/07bsy", "description": "transport", "languageCode": "en-US" }, "segments": [ { "segment": { "startTimeOffset": "0s", "endTimeOffset": "38.757872s" }, "confidence": 0.81231534 } ] }, { "entity": { "entityId": "/m/01n32", "description": "city", "languageCode": "en-US" }, "categoryEntities": [ { "entityId": "/m/043rvww", "description": "geographical feature", "languageCode": "en-US" } ], "segments": [ { "segment": { "startTimeOffset": "0s", "endTimeOffset": "38.757872s" }, "confidence": 0.3942462 } ] }, ... { "entity": { "entityId": "/m/06gfj", "description": "road", "languageCode": "en-US" }, "segments": [ { "segment": { "startTimeOffset": "0s", "endTimeOffset": "38.757872s" }, "confidence": 0.86698604 } ] } ] } ] } }
恭喜!您已向 Video Intelligence API 发送了第一个请求。
清理
为避免因本面页中使用的资源 导致您的 Google Cloud 账号产生费用,请执行以下操作之一:
如果您想继续使用您创建的项目,请使用带有
--recursive标志的gcloud storage rm命令删除存储桶及其中的所有内容:gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
如果您不需要为此快速入门创建的项目,请删除该项目。
- 在 Google Cloud 控制台中,前往 管理资源 页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击 关闭以删除项目。