对象跟踪功能可跟踪在输入视频中检测到的对象。要发出对象跟踪请求,请调用 annotate
方法并在 features
字段中指定 OBJECT_TRACKING
。
对于在视频或视频片段中检测到的实体和空间位置,对象跟踪请求会使用适合这些实体和空间位置的标签来注释视频。例如,如果某个视频中有车辆正在穿过交通信号灯,则可能会产生“汽车”、“卡车”、“自行车”、“轮胎”、“灯”、“窗户”等标签。每个标签可包括一系列边界框,其中每个边界框都有一个包含时间偏移量的关联时间段,该时间偏移量指示相对于视频开始时的时长偏移量。注释还包含其他实体信息,包括实体 ID,您可以在 Google Knowledge Graph Search API 中使用该实体 ID 查找有关实体的更多信息。
对象跟踪与标签检测
对象跟踪与标签检测的不同之处在于,标签检测提供的标签没有边界框,而对象跟踪则在每个时间步提供给定视频中存在的各个对象的标签以及每个对象实例的边界框。
系统会将相同对象类型的多个实例分配给 ObjectTrackingAnnotation
消息的不同实例,其中,给定对象跟踪的所有实例都保留在其自己的 ObjectTrackingAnnotation
实例中。例如,如果视频中有一辆红色汽车和一辆蓝色汽车显示了 5 秒,则跟踪请求应返回 ObjectTrackingAnnotation
的两个实例。第一个实例将包含两辆汽车之一(例如红色汽车)的位置,而第二个实例将包含另一辆汽车的位置。
请求对 Cloud Storage 中的视频执行对象跟踪
以下示例演示了如何对位于 Cloud Storage 中的文件进行对象跟踪。
发送处理请求
下面演示了如何向 annotate
方法发送 POST
请求。该示例使用通过 Google Cloud CLI 为项目设置的服务账号的访问令牌。如需了解有关安装 Google Cloud CLI、使用服务账号设置项目以及获取访问令牌的说明,请参阅 Video Intelligence 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- INPUT_URI:STORAGE_URI
例如:"inputUri": "gs://cloud-videointelligence-demo/assistant.mp4",
。 - PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{ "inputUri": "STORAGE_URI ", "features": ["OBJECT_TRACKING"] }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 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://videointelligence.googleapis.com/v1/videos:annotate"
PowerShell (Windows)
将请求正文保存在名为 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://videointelligence.googleapis.com/v1/videos:annotate" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID " }
如果请求成功,Video Intelligence API 将返回操作的 name
。上面的示例展示了此类响应的示例,其中 PROJECT_NUMBER
是您的项目编号,OPERATION_ID
是为请求创建的长时间运行的操作的 ID。
获取结果
要获取请求的结果,请使用对 videos:annotate
的调用返回的操作名称发送 GET
,如下例所示。
在使用任何请求数据之前,请先进行以下替换:
- OPERATION_NAME:Video Intelligence API 返回的操作名称。操作名称采用
projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
格式 - PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
GET https://videointelligence.googleapis.com/v1/OPERATION_NAME
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_NUMBER " \
"https://videointelligence.googleapis.com/v1/OPERATION_NAME "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER " }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://videointelligence.googleapis.com/v1/OPERATION_NAME " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
// Object tracking annotations are returned as aobjectAnnotations
list. { "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID ", "metadata": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress", "annotationProgress": [ { "inputUri": "/cloud-ml-sandbox/video/chicago.mp4", "progressPercent": 100, "startTime": "2019-12-21T16:56:46.755199Z", "updateTime": "2019-12-21T16:59:17.911197Z" } ] }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse", "annotationResults": [ { "inputUri": "/cloud-ml-sandbox/video/chicago.mp4", "objectAnnotations": [ { "entity": { "entityId": "/m/0k4j", "description": "car", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.2672763, "top": 0.5677657, "right": 0.4388713, "bottom": 0.7623171 }, "timeOffset": "0s" }, { "normalizedBoundingBox": { "left": 0.26920167, "top": 0.5659805, "right": 0.44331276, "bottom": 0.76780635 }, "timeOffset": "0.100495s" }, ... { "normalizedBoundingBox": { "left": 0.83573246, "top": 0.6645812, "right": 1, "bottom": 0.99865407 }, "timeOffset": "2.311402s" } ], "segment": { "startTimeOffset": "0s", "endTimeOffset": "2.311402s" }, "confidence": 0.99488896 }, ... { "entity": { "entityId": "/m/0cgh4", "description": "building", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.010383379, "right": 0.21914443, "bottom": 0.5591795 }, "timeOffset": "0s" }, { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.009684974, "right": 0.22915152, "bottom": 0.56070584 }, "timeOffset": "0.100495s" }, ... { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.008624528, "right": 0.22723165, "bottom": 0.56158626 }, "timeOffset": "0.401983s" } ], "segment": { "startTimeOffset": "0s", "endTimeOffset": "0.401983s" }, "confidence": 0.33914912 }, ... { "entity": { "entityId": "/m/0cgh4", "description": "building", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.79324204, "top": 0.0006896425, "right": 0.99659824, "bottom": 0.5324423 }, "timeOffset": "37.585421s" }, { "normalizedBoundingBox": { "left": 0.78935236, "top": 0.0011992548, "right": 0.99659824, "bottom": 0.5374946 }, "timeOffset": "37.685917s" }, ... { "normalizedBoundingBox": { "left": 0.79404694, "right": 0.99659824, "bottom": 0.5280966 }, "timeOffset": "38.590379s" } ], "segment": { "startTimeOffset": "37.585421s", "endTimeOffset": "38.590379s" }, "confidence": 0.3415429 } ] } ] } }
下载注释结果
将来源中的注释复制到目标存储分区(请参阅复制文件和对象):
gcloud storage cp gcs_uri gs://my-bucket
注意:如果输出 gcs uri 由用户提供,则注释存储在该 gcs uri 中。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Video Intelligence 参考文档。
PHP:请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Video Intelligence 参考文档。
Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Video Intelligence 参考文档。
请求对本地文件中的视频执行对象跟踪
以下示例展示如何对本地存储的文件执行对象跟踪。
发送处理请求
要在本地视频文件上执行注释,请对视频文件的内容进行 base64 编码。在请求的 inputContent
字段中添加 base64 编码的内容。如需了解如何对视频文件的内容进行 base64 编码,请参阅 Base64 编码。
下面演示了如何向 videos:annotate
方法发送 POST
请求。该示例使用通过 Google Cloud CLI 为项目设置的服务账号的访问令牌。如需了解有关安装 Google Cloud CLI、使用服务账号设置项目以及获取访问令牌的说明,请参阅 Video Intelligence 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- inputContent:BASE64_ENCODED_CONTENT
例如:"UklGRg41AwBBVkkgTElTVAwBAABoZHJsYXZpaDgAAAA1ggAAxPMBAAAAAAAQCAA..."
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{ "inputContent": "BASE64_ENCODED_CONTENT ", "features": ["OBJECT_TRACKING"] }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 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://videointelligence.googleapis.com/v1/videos:annotate"
PowerShell (Windows)
将请求正文保存在名为 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://videointelligence.googleapis.com/v1/videos:annotate" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID " }
如果请求成功,则 Video Intelligence 会为您的操作分配 name
。以下示例展示了此类响应,其中 PROJECT_NUMBER
是您的项目编号,OPERATION_ID
是为请求创建的长时间运行操作的 ID。
获取结果
要获取请求的结果,您必须使用对 videos:annotate
的调用返回的操作名称发送 GET
,如下例所示。
在使用任何请求数据之前,请先进行以下替换:
- OPERATION_NAME:Video Intelligence API 返回的操作名称。操作名称采用
projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
格式 - PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
GET https://videointelligence.googleapis.com/v1/OPERATION_NAME
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_NUMBER " \
"https://videointelligence.googleapis.com/v1/OPERATION_NAME "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER " }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://videointelligence.googleapis.com/v1/OPERATION_NAME " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
// Object tracking annotations are returned as aobjectAnnotations
list. { "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID ", "metadata": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress", "annotationProgress": [ { "inputContent": "UklGRg41AwBBVkkgTElTVAwBAABoZHJsYXZpaDgAAAA1ggAAxPMBAAAAAAAQCAA...", "progressPercent": 100, "startTime": "2018-06-21T16:56:46.755199Z", "updateTime": "2018-06-21T16:59:17.911197Z" } ] }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse", "annotationResults": [ { "inputContent": "/cloud-ml-sandbox/video/chicago.mp4", "objectAnnotations": [ { "entity": { "entityId": "/m/0k4j", "description": "car", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.2672763, "top": 0.5677657, "right": 0.4388713, "bottom": 0.7623171 }, "timeOffset": "0s" }, { "normalizedBoundingBox": { "left": 0.26920167, "top": 0.5659805, "right": 0.44331276, "bottom": 0.76780635 }, "timeOffset": "0.100495s" }, ... { "normalizedBoundingBox": { "left": 0.83573246, "top": 0.6645812, "right": 1, "bottom": 0.99865407 }, "timeOffset": "2.311402s" } ], "segment": { "startTimeOffset": "0s", "endTimeOffset": "2.311402s" }, "confidence": 0.99488896 }, ... { "entity": { "entityId": "/m/0cgh4", "description": "building", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.010383379, "right": 0.21914443, "bottom": 0.5591795 }, "timeOffset": "0s" }, { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.009684974, "right": 0.22915152, "bottom": 0.56070584 }, "timeOffset": "0.100495s" }, ... { "normalizedBoundingBox": { "left": 0.12340179, "top": 0.008624528, "right": 0.22723165, "bottom": 0.56158626 }, "timeOffset": "0.401983s" } ], "segment": { "startTimeOffset": "0s", "endTimeOffset": "0.401983s" }, "confidence": 0.33914912 }, ... { "entity": { "entityId": "/m/0cgh4", "description": "building", "languageCode": "en-US" }, "frames": [ { "normalizedBoundingBox": { "left": 0.79324204, "top": 0.0006896425, "right": 0.99659824, "bottom": 0.5324423 }, "timeOffset": "37.585421s" }, { "normalizedBoundingBox": { "left": 0.78935236, "top": 0.0011992548, "right": 0.99659824, "bottom": 0.5374946 }, "timeOffset": "37.685917s" }, ... { "normalizedBoundingBox": { "left": 0.79404694, "right": 0.99659824, "bottom": 0.5280966 }, "timeOffset": "38.590379s" } ], "segment": { "startTimeOffset": "37.585421s", "endTimeOffset": "38.590379s" }, "confidence": 0.3415429 } ] } ] } }
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Video Intelligence 参考文档。
PHP:请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Video Intelligence 参考文档。
Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Video Intelligence 参考文档。