Vision API 可以检测并转录 Cloud Storage 中存储的 PDF 和 TIFF 文件中的文本。
必须使用 files:asyncBatchAnnotate
函数请求对 PDF 和 TIFF 执行文档文本检测;该函数将执行离线(异步)请求并通过 operations
资源提供其状态。
PDF/TIFF 请求的输出会写入在指定的 Cloud Storage 存储桶中创建的 JSON 文件。
限制
Vision API 可接受最多 2,000 页的 PDF/TIFF 文件。如果文件包含更多页面,则会返回错误。
身份验证
files:asyncBatchAnnotate
请求不支持 API 密钥。如需了解如何使用服务账号进行身份验证,请参阅使用服务账号。
用于进行身份验证的账号必须能够访问您为输出指定的 Cloud Storage 存储桶(roles/editor
、roles/storage.objectCreator
或权限更高的角色)。
您可以使用 API 密钥来查询该操作的状态;如需查看相关说明,请参阅使用 API 密钥。
文档文本检测请求
目前,PDF/TIFF 文档检测仅适用于存储在 Cloud Storage 存储桶中的文件。响应 JSON 文件同样会保存到 Cloud Storage 存储桶。

gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf
,来源:美国人口普查局。
在使用任何请求数据之前,请先进行以下替换:
- CLOUD_STORAGE_BUCKET:用于保存输出文件的 Cloud Storage 存储桶/目录,采用以下格式表示:
gs://bucket/directory/
- CLOUD_STORAGE_FILE_URI:Cloud Storage 存储桶中有效文件 (PDF/TIFF) 的路径。您必须至少拥有该文件的读取权限。
示例:
gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf
- FEATURE_TYPE:有效的特征类型。
对于
files:asyncBatchAnnotate
请求,您可以使用以下特征类型:DOCUMENT_TEXT_DETECTION
TEXT_DETECTION
- PROJECT_ID:您的 Google Cloud 项目 ID。
特定于字段的注意事项:
inputConfig
- 用于替换其他 Vision API 请求中所用的image
字段。它包含两个子字段:gcsSource.uri
- PDF 或 TIFF 文件的 Google Cloud Storage URI(可供发出请求的用户或服务账号访问)。mimeType
- 接受的文件类型之一:application/pdf
或image/tiff
。
outputConfig
- 指定输出详细信息。它包含两个子字段:gcsDestination.uri
- 有效的 Google Cloud Storage URI。该存储桶必须可供发出请求的用户或服务账号写入。文件名为output-x-to-y
,其中x
和y
表示包含在该输出文件中的 PDF/TIFF 页码。如果该文件已存在,其内容将被覆盖。batchSize
- 指定每个输出 JSON 文件中应包含多少页输出。
HTTP 方法和网址:
POST https://vision.googleapis.com/v1/files:asyncBatchAnnotate
请求 JSON 正文:
{ "requests":[ { "inputConfig": { "gcsSource": { "uri": "CLOUD_STORAGE_FILE_URI " }, "mimeType": "application/pdf" }, "features": [ { "type": "FEATURE_TYPE " } ], "outputConfig": { "gcsDestination": { "uri": "CLOUD_STORAGE_BUCKET " }, "batchSize": 1 } } ] }
如需发送请求,请选择以下方式之一:
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_ID " \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://vision.googleapis.com/v1/files:asyncBatchAnnotate"
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID " }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/files:asyncBatchAnnotate" | Select-Object -Expand Content
如果 asyncBatchAnnotate
请求成功,返回的响应中将包含单个名称字段:
{ "name": "projects/usable-auth-library/operations/1efec2285bd442df" }
此名称表示具有一个关联 ID(例如 1efec2285bd442df
)的长时间运行的操作,您可以使用 v1.operations
API 对其进行查询。
如需检索您的 Vision 注释响应,请向 v1.operations
端点发送 GET 请求,同时在网址中传递操作 ID:
GET https://vision.googleapis.com/v1/operations/operation-id
例如:
curl -X GET -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Content-Type: application/json" \ https://vision.googleapis.com/v1/projects/project-id /locations/location-id /operations/1efec2285bd442df
如果操作正在进行:
{ "name": "operations/1efec2285bd442df", "metadata": { "@type": "type.googleapis.com/google.cloud.vision.v1.OperationMetadata", "state": "RUNNING", "createTime": "2019-05-15T21:10:08.401917049Z", "updateTime": "2019-05-15T21:10:33.700763554Z" } }
操作完成后,state
会显示为 DONE
,并且结果会写入您指定的 Google Cloud Storage 文件中:
{ "name": "operations/1efec2285bd442df", "metadata": { "@type": "type.googleapis.com/google.cloud.vision.v1.OperationMetadata", "state": "DONE", "createTime": "2019-05-15T20:56:30.622473785Z", "updateTime": "2019-05-15T20:56:41.666379749Z" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse", "responses": [ { "outputConfig": { "gcsDestination": { "uri": "gs://your-bucket-name /folder /" }, "batchSize": 1 } } ] } }
输出文件中的 JSON 类似于图片 [文档文本检测请求](/vision/docs/ocr) 的 JSON;不过,前者多了一个 context
字段,用于显示指定的 PDF 或 TIFF 位置以及相应文件中的页数:
output-1-to-1.json
完整文件
{ "inputConfig": { "gcsSource": { "uri": "gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf" }, "mimeType": "application/pdf" }, "responses": [ { "fullTextAnnotation": { "pages": [ { "property": { "detectedLanguages": [ { "languageCode": "en", "confidence": 0.94 } ] }, "width": 612, "height": 792, "blocks": [ { "boundingBox": { "normalizedVertices": [ { "x": 0.12908497, "y": 0.10479798 }, ... { "x": 0.12908497, "y": 0.1199495 } ] }, "paragraphs": [ { ... }, "words": [ { ... }, "symbols": [ { ... "text": "C", "confidence": 0.99 }, { "property": { "detectedLanguages": [ { "languageCode": "en" } ] }, "text": "O", "confidence": 0.99 }, ... } ] } ], "text": "CONTENTS\n.\n1-1\nII-1\nIII-1\nList of Statistical Tables... \nHow to Use This Census Report ..\nTable Finding Guide .\nUser Notes .......\nStatistical Tables.........\nAppendixes \nA Geographic Terms and Concepts .........\nB Definitions of Subject Characteristics.\nData Collection and Processing Procedures... \nQuestionnaire. ........\nE Maps .................\nF Operational Overview and accuracy of the Data.......\nG Residence Rule and Residence Situations for the \n2010 Census of the United States... \nH Acknowledgments .....\nE\n*Appendix may be found in the separate volume, CPH-1-A, Summary Population and\nHousing Characteristics, Selected Appendixes, on the Internet at <www.census.gov\n/prod/cen2010/cph-1-a.pdf>.\nContents\n" }, "context": { "uri": "gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf", "pageNumber": 1 } } ] }
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Vision Go API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
在试用此示例之前,请按照Vision API 快速入门:使用客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Vision API Java 参考文档。
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Vision Node.js API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Python 设置说明进行操作。 如需了解详情,请参阅 Vision Python API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
您使用的 gcloud
命令取决于文件类型。
如需执行 PDF 文本检测,请使用
gcloud ml vision detect-text-pdf
命令,如以下示例所示:gcloud ml vision detect-text-pdf gs://
my_bucket /input_file gs://my_bucket /out_put_prefix 如需执行 TIFF 文本检测,请使用
gcloud ml vision detect-text-tiff
命令,如以下示例所示:gcloud ml vision detect-text-tiff gs://
my_bucket /input_file gs://my_bucket /out_put_prefix
C#: 请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Vision 参考文档。
PHP: 请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Vision 参考文档。
Ruby 版: 请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Vision 参考文档。
多区域支持
现可指定洲级数据存储和 OCR 处理。目前支持以下区域:
us
:仅限美国eu
:欧盟
位置
借助 Cloud Vision,您可以控制存储和处理项目资源的位置。具体来说,您可以将 Cloud Vision 配置为仅在欧盟地区存储和处理您的数据。
默认情况下,Cloud Vision 会在全球位置存储和处理资源,这意味着 Cloud Vision 不保证您的资源将保留在特定位置或区域内。如果您选择欧盟位置,Google 只会在欧盟地区存储和处理您的数据。您和您的用户可以从任意位置访问该数据。
使用 API 设置位置
Vision API 支持全球 API 端点 (vision.googleapis.com
) 以及两个基于区域的端点:欧盟端点 (eu-vision.googleapis.com
) 和美国端点 (us-vision.googleapis.com
)。使用这些端点进行特定于区域的处理。例如,要仅在欧盟地区存储和处理数据,请使用 URI eu-vision.googleapis.com
代替 vision.googleapis.com
进行 REST API 调用:
- https://eu-vision.googleapis.com/v1/projects/PROJECT_ID/locations/eu/images:annotate
- https://eu-vision.googleapis.com/v1/projects/PROJECT_ID/locations/eu/images:asyncBatchAnnotate
- https://eu-vision.googleapis.com/v1/projects/PROJECT_ID/locations/eu/files:annotate
- https://eu-vision.googleapis.com/v1/projects/PROJECT_ID/locations/eu/files:asyncBatchAnnotate
如需仅在美国存储和处理您的数据,请在上述方法中使用美国端点 (us-vision.googleapis.com
)。
使用客户端库设置位置
默认情况下,Vision API 客户端库会访问全球 API 端点 (vision.googleapis.com
)。如需仅在欧盟地区存储和处理您的数据,您需要明确设置端点 (eu-vision.googleapis.com
)。以下代码示例展示了如何配置此设置。
在使用任何请求数据之前,请先进行以下替换:
- REGION_ID:有效的区域位置标识符之一:
us
:仅限美国eu
:欧盟
- CLOUD_STORAGE_IMAGE_URI:Cloud Storage 存储桶中有效图片文件的路径。您必须至少拥有该文件的读取权限。
示例:
gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf
- CLOUD_STORAGE_BUCKET:用于保存输出文件的 Cloud Storage 存储桶/目录,采用以下格式表示:
gs://bucket/directory/
- FEATURE_TYPE:有效的特征类型。
对于
files:asyncBatchAnnotate
请求,您可以使用以下特征类型:DOCUMENT_TEXT_DETECTION
TEXT_DETECTION
- PROJECT_ID:您的 Google Cloud 项目 ID。
特定于字段的注意事项:
inputConfig
- 用于替换其他 Vision API 请求中所用的image
字段。它包含两个子字段:gcsSource.uri
- PDF 或 TIFF 文件的 Google Cloud Storage URI(可供发出请求的用户或服务账号访问)。mimeType
- 接受的文件类型之一:application/pdf
或image/tiff
。
outputConfig
- 指定输出详细信息。它包含两个子字段:gcsDestination.uri
- 有效的 Google Cloud Storage URI。该存储桶必须可供发出请求的用户或服务账号写入。文件名为output-x-to-y
,其中x
和y
表示包含在该输出文件中的 PDF/TIFF 页码。如果该文件已存在,其内容将被覆盖。batchSize
- 指定每个输出 JSON 文件中应包含多少页输出。
HTTP 方法和网址:
POST https://REGION_ID -vision.googleapis.com/v1/projects/PROJECT_ID /locations/REGION_ID /files:asyncBatchAnnotate
请求 JSON 正文:
{ "requests":[ { "inputConfig": { "gcsSource": { "uri": "CLOUD_STORAGE_IMAGE_URI " }, "mimeType": "application/pdf" }, "features": [ { "type": "FEATURE_TYPE " } ], "outputConfig": { "gcsDestination": { "uri": "CLOUD_STORAGE_BUCKET " }, "batchSize": 1 } } ] }
如需发送请求,请选择以下方式之一:
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_ID " \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION_ID -vision.googleapis.com/v1/projects/PROJECT_ID /locations/REGION_ID /files:asyncBatchAnnotate"
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID " }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION_ID -vision.googleapis.com/v1/projects/PROJECT_ID /locations/REGION_ID /files:asyncBatchAnnotate" | Select-Object -Expand Content
如果 asyncBatchAnnotate
请求成功,返回的响应中将包含单个名称字段:
{ "name": "projects/usable-auth-library/operations/1efec2285bd442df" }
此名称表示具有一个关联 ID(例如 1efec2285bd442df
)的长时间运行的操作,您可以使用 v1.operations
API 对其进行查询。
如需检索您的 Vision 注释响应,请向 v1.operations
端点发送 GET 请求,同时在网址中传递操作 ID:
GET https://vision.googleapis.com/v1/operations/operation-id
例如:
curl -X GET -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Content-Type: application/json" \ https://vision.googleapis.com/v1/projects/project-id /locations/location-id /operations/1efec2285bd442df
如果操作正在进行:
{ "name": "operations/1efec2285bd442df", "metadata": { "@type": "type.googleapis.com/google.cloud.vision.v1.OperationMetadata", "state": "RUNNING", "createTime": "2019-05-15T21:10:08.401917049Z", "updateTime": "2019-05-15T21:10:33.700763554Z" } }
操作完成后,state
会显示为 DONE
,并且结果会写入您指定的 Google Cloud Storage 文件中:
{ "name": "operations/1efec2285bd442df", "metadata": { "@type": "type.googleapis.com/google.cloud.vision.v1.OperationMetadata", "state": "DONE", "createTime": "2019-05-15T20:56:30.622473785Z", "updateTime": "2019-05-15T20:56:41.666379749Z" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse", "responses": [ { "outputConfig": { "gcsDestination": { "uri": "gs://your-bucket-name /folder /" }, "batchSize": 1 } } ] } }
如果使用了 DOCUMENT_TEXT_DETECTION
特征,则输出文件中的 JSON 类似于图片文档文本检测响应的 JSON;如果使用了 TEXT_DETECTION
特征,则类似于图片文本检测响应的 JSON。输出将包含一个额外的 context
字段,用于显示指定的 PDF 或 TIFF 的位置以及相应文件中的页数:
output-1-to-1.json
完整文件
{ "inputConfig": { "gcsSource": { "uri": "gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf" }, "mimeType": "application/pdf" }, "responses": [ { "fullTextAnnotation": { "pages": [ { "property": { "detectedLanguages": [ { "languageCode": "en", "confidence": 0.94 } ] }, "width": 612, "height": 792, "blocks": [ { "boundingBox": { "normalizedVertices": [ { "x": 0.12908497, "y": 0.10479798 }, ... { "x": 0.12908497, "y": 0.1199495 } ] }, "paragraphs": [ { ... }, "words": [ { ... }, "symbols": [ { ... "text": "C", "confidence": 0.99 }, { "property": { "detectedLanguages": [ { "languageCode": "en" } ] }, "text": "O", "confidence": 0.99 }, ... } ] } ], "text": "CONTENTS\n.\n1-1\nII-1\nIII-1\nList of Statistical Tables... \nHow to Use This Census Report ..\nTable Finding Guide .\nUser Notes .......\nStatistical Tables.........\nAppendixes \nA Geographic Terms and Concepts .........\nB Definitions of Subject Characteristics.\nData Collection and Processing Procedures... \nQuestionnaire. ........\nE Maps .................\nF Operational Overview and accuracy of the Data.......\nG Residence Rule and Residence Situations for the \n2010 Census of the United States... \nH Acknowledgments .....\nE\n*Appendix may be found in the separate volume, CPH-1-A, Summary Population and\nHousing Characteristics, Selected Appendixes, on the Internet at <www.census.gov\n/prod/cen2010/cph-1-a.pdf>.\nContents\n" }, "context": { "uri": "gs://cloud-samples-data/vision/pdf_tiff/census2010.pdf", "pageNumber": 1 } } ] }
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Vision Go API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
在试用此示例之前,请按照Vision API 快速入门:使用客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Vision API Java 参考文档。
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Vision Node.js API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Python 设置说明进行操作。 如需了解详情,请参阅 Vision Python API 参考文档。
如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
自行试用
如果您是 Google Cloud 新手,请创建一个账号来评估 Cloud Vision API 在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
免费试用 Cloud Vision API