Video Intelligence API 可以使用 LABEL_DETECTION 功能来识别视频片段中出现的实体。此功能可识别对象、位置、活动、动物物种、产品等。
分析可以按如下方式划分:
- 帧级别:
实体在每一帧中被标识和标记(每秒采样一帧)。
- 镜头级别:
系统会在每个片段(或视频)中自动检测镜头。然后在每个镜头中标识实体并为其添加标签。
- 片段级别:
用户选择的视频片段可以通过为注释指定开始和结束的时间偏移值来指定分析(请参阅 VideoSegment)。然后在每个片段中标识实体并为其添加标签。如果未指定片段,则整个视频将被视为一个片段。
为本地文件添加注释
以下示例展示如何对本地文件执行视频分析以获取标签。
想要更深入了解其他内容?请查看我们的详细 Python 教程。
REST
发送处理请求
下面演示了如何向 videos:annotate
方法发送 POST
请求。您可以将 LabelDetectionMode
配置为镜头级和/或帧级注释。我们建议使用 SHOT_AND_FRAME_MODE
。该示例使用
使用 Google Cloud CLI 为项目设置的服务账号。对于
安装 Google Cloud CLI、使用服务设置项目的说明
并获取访问令牌,请参阅
Video Intelligence 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- BASE64_ENCODED_CONTENT:您的视频以 base64 编码数据。请参阅有关如何将数据转换为 base64 的说明。
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{
"inputContent": "BASE64_ENCODED_CONTENT",
"features": ["LABEL_DETECTION"],
}
如需发送您的请求,请展开以下选项之一:
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 将返回操作的名称。
获取结果
如需获取请求的结果,您必须向 projects.locations.operations
资源发送 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 响应:
响应
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
"annotationProgress": [
{
"progressPercent": 100,
"startTime": "2019-03-12T19:36:09.110351Z",
"updateTime": "2019-03-12T19:36:17.519069Z"
}
]
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
"annotationResults": [
{
"segmentLabelAnnotations": [
{
"entity": {
"entityId": "/m/01prls",
"description": "land vehicle",
"languageCode": "en-US"
},
"categoryEntities": [
{
"entityId": "/m/07yv9",
"description": "vehicle",
"languageCode": "en-US"
}
],
"segments": [
{
"segment": {
"startTimeOffset": "0s",
"endTimeOffset": "38.757872s"
},
"confidence": 0.6614419
}
]
},
{
"entity": {
"entityId": "/m/039jbq",
"description": "urban area",
"languageCode": "en-US"
},
"categoryEntities": [
{
"entityId": "/m/01n32",
"description": "city",
"languageCode": "en-US"
}
],
"segments": [
{
"segment": {
"startTimeOffset": "0s",
"endTimeOffset": "38.757872s"
},
"confidence": 0.92337775
}
]
},
...
为 Cloud Storage 上的文件添加注释
以下示例展示如何对 Cloud Storage 中的文件上的标签执行视频分析。
REST
详细了解如何安装和使用 Video Intelligence API 客户端库
请参阅 Video Intelligence API 客户端库。
发送处理请求
下面演示了如何向 annotate
方法发送 POST
请求。该示例使用
使用 Google Cloud CLI 为项目设置的服务账号。对于
安装 Google Cloud CLI、使用服务设置项目的说明
并获取访问令牌,请参阅
Video Intelligence 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- INPUT_URI:包含要添加注释的文件的 Cloud Storage 存储分区(包括文件名)。必须以
gs://
开头。
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{
"inputUri": "INPUT_URI",
"features": ["LABEL_DETECTION"],
}
如需发送您的请求,请展开以下选项之一:
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 将返回操作的名称。
获取结果
如需获取请求的结果,您必须向 projects.locations.operations
资源发送 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 响应:
响应
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
"annotationProgress": [
{
"inputUri": "INPUT_URI",
"progressPercent": 100,
"startTime": "2019-11-13T19:25:56.206335Z",
"updateTime": "2019-11-13T19:26:16.215615Z"
}
]
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
"annotationResults": [
{
"inputUri": "INPUT_URI",
"segmentLabelAnnotations": [
{
"entity": {
"entityId": "/m/01vkl",
"description": "circle",
"languageCode": "en-US"
},
"categoryEntities": [
{
"entityId": "/m/016nqd",
"description": "shape",
"languageCode": "en-US"
}
],
"segments": [
{
"segment": {
"startTimeOffset": "0s",
"endTimeOffset": "16.416666s"
},
"confidence": 0.36535457
}
]
},
...
]
}
]
}
}
下载注释结果
将来源中的注释复制到目标存储分区(请参阅复制文件和对象):
gsutil cp gcs_uri gs://my-bucket
注意:如果输出 gcs uri 由用户提供,则注释存储在该 gcs uri 中。