文本检测功能可执行光学字符识别(OCR),检测并提取输入视频中的文本。
文本检测适用于 Cloud Vision API 支持的所有语言。
请求对 Cloud Storage 中的视频执行文本检测
以下示例演示了如何对 Cloud Storage 中的文件执行文本检测。
发送视频注释请求
以下代码展示了如何向 videos:annotate
方法发送 POST 请求。该示例使用 Google Cloud CLI 创建访问令牌。如需了解如何安装 gcloud CLI,请参阅 Video Intelligence API 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- INPUT_URI:包含要添加注释的文件的 Cloud Storage 存储分区(包括文件名)。必须以
gs://
开头。
例如:"inputUri": "gs://cloud-videointelligence-demo/assistant.mp4",
。 - LANGUAGE_CODE:[可选]例如 “en-US”
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{ "inputUri": "INPUT_URI ", "features": ["TEXT_DETECTION"], "videoContext": { "textDetectionConfig": { "languageHints": ["LANGUAGE_CODE "] } } }
如需发送您的请求,请展开以下选项之一:
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。
- PROJECT_NUMBER:您项目的编号
- LOCATION_ID:在其中添加注释的 Cloud 区域。支持的云区域为:
us-east1
、us-west1
、europe-west1
、asia-east1
。如果未指定区域,系统将根据视频文件位置确定区域。 - OPERATION_ID:是为请求创建的长时间运行的操作的 ID,并在启动操作时在响应中提供,例如
12345...
获取注释结果
要检索操作的结果,请使用从 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 响应:
响应
"textAnnotations": [ { "text": "Hair Salon", "segments": [ { "segment": { "startTimeOffset": "0.833333s", "endTimeOffset": "2.291666s" }, "confidence": 0.99438506, "frames": [ { "rotatedBoundingBox": { "vertices": [ { "x": 0.7015625, "y": 0.59583336 }, { "x": 0.7984375, "y": 0.59583336 }, { "x": 0.7984375, "y": 0.64166665 }, { "x": 0.7015625, "y": 0.64166665 } ] }, "timeOffset": "0.833333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.041666s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.250s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6319444 }, { "x": 0.70234376, "y": 0.6319444 } ] }, "timeOffset": "1.458333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.666666s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.875s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "2.083333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "2.291666s" } ] } ] }, { "text": "\"Sure, give me one second.\"", "segments": [ { "segment": { "startTimeOffset": "10.625s", "endTimeOffset": "13.333333s" }, "confidence": 0.98716676, "frames": [ { "rotatedBoundingBox": { "vertices": [ { "x": 0.60859376, "y": 0.59583336 }, { "x": 0.8952959, "y": 0.5903528 }, { "x": 0.89560676, "y": 0.6417387 }, { "x": 0.60890454, "y": 0.64721924 } ] }, "timeOffset": "10.625s" }, ... ] }
textAnnotations
列表的形式返回。
注意:仅当值为 True 时,才会返回 done 字段。操作未完成的响应中不包含该字段。
下载注释结果
将来源中的注释复制到目标存储分区(请参阅复制文件和对象):
gcloud storage cp gcs_uri gs://my-bucket
注意:如果输出 gcs uri 由用户提供,则注释存储在该 gcs uri 中。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
如需向 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 API 快速入门
在使用任何请求数据之前,请先进行以下替换:
- "inputContent": BASE64_ENCODED_CONTENT
例如:"UklGRg41AwBBVkkgTElTVAwBAABoZHJsYXZpaDgAAAA1ggAAxPMBAAAAAAAQCAA..."
- LANGUAGE_CODE:[可选]例如 “en-US”
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{ "inputContent": "BASE64_ENCODED_CONTENT ", "features": ["TEXT_DETECTION"], "videoContext": { "textDetectionConfig": { "languageHints": ["LANGUAGE_CODE "] } } }
如需发送您的请求,请展开以下选项之一:
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。
- OPERATION_ID:并在启动操作时在响应中提供,例如
12345...
获取注释结果
要检索操作的结果,请使用从 videos:annotate 调用返回的操作名称发出 GET 请求,如以下示例所示。
在使用任何请求数据之前,请先进行以下替换:
- 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 响应:
响应
"textAnnotations": [ { "text": "Hair Salon", "segments": [ { "segment": { "startTimeOffset": "0.833333s", "endTimeOffset": "2.291666s" }, "confidence": 0.99438506, "frames": [ { "rotatedBoundingBox": { "vertices": [ { "x": 0.7015625, "y": 0.59583336 }, { "x": 0.7984375, "y": 0.59583336 }, { "x": 0.7984375, "y": 0.64166665 }, { "x": 0.7015625, "y": 0.64166665 } ] }, "timeOffset": "0.833333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.041666s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.250s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6319444 }, { "x": 0.70234376, "y": 0.6319444 } ] }, "timeOffset": "1.458333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.666666s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "1.875s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "2.083333s" }, { "rotatedBoundingBox": { "vertices": [ { "x": 0.70234376, "y": 0.6 }, { "x": 0.7992188, "y": 0.6 }, { "x": 0.7992188, "y": 0.6333333 }, { "x": 0.70234376, "y": 0.6333333 } ] }, "timeOffset": "2.291666s" } ] } ] }, { "text": "\"Sure, give me one second.\"", "segments": [ { "segment": { "startTimeOffset": "10.625s", "endTimeOffset": "13.333333s" }, "confidence": 0.98716676, "frames": [ { "rotatedBoundingBox": { "vertices": [ { "x": 0.60859376, "y": 0.59583336 }, { "x": 0.8952959, "y": 0.5903528 }, { "x": 0.89560676, "y": 0.6417387 }, { "x": 0.60890454, "y": 0.64721924 } ] }, "timeOffset": "10.625s" }, ... ] }
文本检测注释以 textAnnotations
列表的形式返回。注意:仅当值为 True 时,才会返回 done 字段。操作未完成的响应中不包含该字段。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Video Intelligence 参考文档。
PHP:请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Video Intelligence 参考文档。
Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Video Intelligence 参考文档。