本页面介绍如何使用 curl
发出 Video Intelligence API 请求。
您可以按照本页中所述的步骤操作,也可以尝试将本快速入门作为 Google Cloud 培训实验室。
准备工作
- 登录您的 Google Cloud 帐号。如果您是 Google Cloud 新手,请创建一个帐号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
在 Google Cloud Console 的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
确保您的 Cloud 项目已启用结算功能。 了解如何确认您的项目是否已启用结算功能。
- 启用所需的 API。
-
创建服务帐号:
-
在 Cloud Console 中,转到创建服务帐号页面。
转到“创建服务帐号” - 选择一个项目。
-
在服务帐号名称字段中,输入一个名称。 Cloud Console 会根据此名称填充服务帐号 ID 字段。
在服务帐号说明字段中,输入说明。例如,
Service account for quickstart
。 -
点击完成以完成服务帐号的创建过程。
不要关闭浏览器窗口。您将在下一步骤中用到它。
-
-
创建服务帐号密钥:
- 在 Cloud Console 中,点击您创建的服务帐号的电子邮件地址。
- 点击密钥。
- 依次点击添加密钥和创建新密钥。
- 点击创建。JSON 密钥文件将下载到您的计算机上。
- 点击关闭。
-
将环境变量
GOOGLE_APPLICATION_CREDENTIALS
设置为包含您的服务帐号密钥的 JSON 文件的路径。 此变量仅适用于当前的 shell 会话,因此,如果您打开新的会话,请重新设置该变量。 - 安装并初始化 Cloud SDK。
发出视频注释请求
gcloud
使用gcloud
命令行工具在要分析的视频路径上调用 detect-labels
命令。
gcloud ml video detect-labels gs://your_bucket/your_object
命令行
使用
curl
向videos:annotate
方法发出 POST 请求,使用gcloud auth application-default print-access-token
命令粘贴服务帐号的访问令牌:curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ "inputUri":"gs://your_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": "/your_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": "your_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": "/your_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 Platform 费用,请使用 Cloud Console 删除您不需要的项目。