与 AWS SDK或DetectDocumentText一起使用 CLI - AWS SDK 代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或DetectDocumentText一起使用 CLI

以下代码示例演示如何使用 DetectDocumentText

CLI
AWS CLI

检测文档中的文本

以下 detect-document-text 示例演示了如何检测文档中的文本。

Linux/macOS:

aws textract detect-document-text \ --document '{"S3Object":{"Bucket":"bucket","Name":"document"}}'

Windows:

aws textract detect-document-text \ --document "{\"S3Object\":{\"Bucket\":\"bucket\",\"Name\":\"document\"}}" \ --region region-name

输出:

{ "Blocks": [ { "Geometry": { "BoundingBox": { "Width": 1.0, "Top": 0.0, "Left": 0.0, "Height": 1.0 }, "Polygon": [ { "Y": 0.0, "X": 0.0 }, { "Y": 0.0, "X": 1.0 }, { "Y": 1.0, "X": 1.0 }, { "Y": 1.0, "X": 0.0 } ] }, "Relationships": [ { "Type": "CHILD", "Ids": [ "896a9f10-9e70-4412-81ce-49ead73ed881", "0da18623-dc4c-463d-a3d1-9ac050e9e720", "167338d7-d38c-4760-91f1-79a8ec457bb2" ] } ], "BlockType": "PAGE", "Id": "21f0535e-60d5-4bc7-adf2-c05dd851fa25" }, { "Relationships": [ { "Type": "CHILD", "Ids": [ "62490c26-37ea-49fa-8034-7a9ff9369c9c", "1e4f3f21-05bd-4da9-ba10-15d01e66604c" ] } ], "Confidence": 89.11581420898438, "Geometry": { "BoundingBox": { "Width": 0.33642634749412537, "Top": 0.17169663310050964, "Left": 0.13885067403316498, "Height": 0.49159330129623413 }, "Polygon": [ { "Y": 0.17169663310050964, "X": 0.13885067403316498 }, { "Y": 0.17169663310050964, "X": 0.47527703642845154 }, { "Y": 0.6632899641990662, "X": 0.47527703642845154 }, { "Y": 0.6632899641990662, "X": 0.13885067403316498 } ] }, "Text": "He llo,", "BlockType": "LINE", "Id": "896a9f10-9e70-4412-81ce-49ead73ed881" }, { "Relationships": [ { "Type": "CHILD", "Ids": [ "19b28058-9516-4352-b929-64d7cef29daf" ] } ], "Confidence": 85.5694351196289, "Geometry": { "BoundingBox": { "Width": 0.33182239532470703, "Top": 0.23131252825260162, "Left": 0.5091826915740967, "Height": 0.3766750991344452 }, "Polygon": [ { "Y": 0.23131252825260162, "X": 0.5091826915740967 }, { "Y": 0.23131252825260162, "X": 0.8410050868988037 }, { "Y": 0.607987642288208, "X": 0.8410050868988037 }, { "Y": 0.607987642288208, "X": 0.5091826915740967 } ] }, "Text": "worlc", "BlockType": "LINE", "Id": "0da18623-dc4c-463d-a3d1-9ac050e9e720" } ], "DocumentMetadata": { "Pages": 1 } }

有关更多信息,请参阅《Amazon Textract 开发人员指南》中的“使用 Amazon Textract 检测文档文本”

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

从输入文档检测文本。

import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.textract.TextractClient; import software.amazon.awssdk.services.textract.model.Document; import software.amazon.awssdk.services.textract.model.DetectDocumentTextRequest; import software.amazon.awssdk.services.textract.model.DetectDocumentTextResponse; import software.amazon.awssdk.services.textract.model.Block; import software.amazon.awssdk.services.textract.model.DocumentMetadata; import software.amazon.awssdk.services.textract.model.TextractException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectDocumentText { public static void main(String[] args) { final String usage = """ Usage: <sourceDoc>\s Where: sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png).\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String sourceDoc = args[0]; Region region = Region.US_EAST_2; TextractClient textractClient = TextractClient.builder() .region(region) .build(); detectDocText(textractClient, sourceDoc); textractClient.close(); } public static void detectDocText(TextractClient textractClient, String sourceDoc) { try { InputStream sourceStream = new FileInputStream(new File(sourceDoc)); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); // Get the input Document object as bytes. Document myDoc = Document.builder() .bytes(sourceBytes) .build(); DetectDocumentTextRequest detectDocumentTextRequest = DetectDocumentTextRequest.builder() .document(myDoc) .build(); // Invoke the Detect operation. DetectDocumentTextResponse textResponse = textractClient.detectDocumentText(detectDocumentTextRequest); List<Block> docInfo = textResponse.blocks(); for (Block block : docInfo) { System.out.println("The block type is " + block.blockType().toString()); } DocumentMetadata documentMetadata = textResponse.documentMetadata(); System.out.println("The number of pages in the document is " + documentMetadata.pages()); } catch (TextractException | FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } } }

从位于 Amazon S3 桶中的文档检测文本。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.textract.model.S3Object; import software.amazon.awssdk.services.textract.TextractClient; import software.amazon.awssdk.services.textract.model.Document; import software.amazon.awssdk.services.textract.model.DetectDocumentTextRequest; import software.amazon.awssdk.services.textract.model.DetectDocumentTextResponse; import software.amazon.awssdk.services.textract.model.Block; import software.amazon.awssdk.services.textract.model.DocumentMetadata; import software.amazon.awssdk.services.textract.model.TextractException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectDocumentTextS3 { public static void main(String[] args) { final String usage = """ Usage: <bucketName> <docName>\s Where: bucketName - The name of the Amazon S3 bucket that contains the document.\s docName - The document name (must be an image, i.e., book.png).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String docName = args[1]; Region region = Region.US_WEST_2; TextractClient textractClient = TextractClient.builder() .region(region) .build(); detectDocTextS3(textractClient, bucketName, docName); textractClient.close(); } public static void detectDocTextS3(TextractClient textractClient, String bucketName, String docName) { try { S3Object s3Object = S3Object.builder() .bucket(bucketName) .name(docName) .build(); // Create a Document object and reference the s3Object instance. Document myDoc = Document.builder() .s3Object(s3Object) .build(); DetectDocumentTextRequest detectDocumentTextRequest = DetectDocumentTextRequest.builder() .document(myDoc) .build(); DetectDocumentTextResponse textResponse = textractClient.detectDocumentText(detectDocumentTextRequest); for (Block block : textResponse.blocks()) { System.out.println("The block type is " + block.blockType().toString()); } DocumentMetadata documentMetadata = textResponse.documentMetadata(); System.out.println("The number of pages in the document is " + documentMetadata.pages()); } catch (TextractException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DetectDocumentText” 中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class TextractWrapper: """Encapsulates Textract functions.""" def __init__(self, textract_client, s3_resource, sqs_resource): """ :param textract_client: A Boto3 Textract client. :param s3_resource: A Boto3 Amazon S3 resource. :param sqs_resource: A Boto3 Amazon SQS resource. """ self.textract_client = textract_client self.s3_resource = s3_resource self.sqs_resource = sqs_resource def detect_file_text(self, *, document_file_name=None, document_bytes=None): """ Detects text elements in a local image file or from in-memory byte data. The image must be in PNG or JPG format. :param document_file_name: The name of a document image file. :param document_bytes: In-memory byte data of a document image. :return: The response from Amazon Textract, including a list of blocks that describe elements detected in the image. """ if document_file_name is not None: with open(document_file_name, "rb") as document_file: document_bytes = document_file.read() try: response = self.textract_client.detect_document_text( Document={"Bytes": document_bytes} ) logger.info("Detected %s blocks.", len(response["Blocks"])) except ClientError: logger.exception("Couldn't detect text.") raise else: return response
  • 有关API详细信息,请参阅DetectDocumentText中的 AWS SDKPython (Boto3) API 参考。

SAP ABAP
SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

"Detects text in the input document." "Amazon Textract can detect lines of text and the words that make up a line of text." "The input document must be in one of the following image formats: JPEG, PNG, PDF, or TIFF." "Create an ABAP object for the Amazon S3 object." DATA(lo_s3object) = NEW /aws1/cl_texs3object( iv_bucket = iv_s3bucket iv_name = iv_s3object ). "Create an ABAP object for the document." DATA(lo_document) = NEW /aws1/cl_texdocument( io_s3object = lo_s3object ). "Analyze document stored in Amazon S3." TRY. oo_result = lo_tex->detectdocumenttext( io_document = lo_document ). "oo_result is returned for testing purposes." LOOP AT oo_result->get_blocks( ) INTO DATA(lo_block). IF lo_block->get_text( ) = 'INGREDIENTS: POWDERED SUGAR* (CANE SUGAR,'. MESSAGE 'Found text in the doc: ' && lo_block->get_text( ) TYPE 'I'. ENDIF. ENDLOOP. DATA(lo_metadata) = oo_result->get_documentmetadata( ). MESSAGE 'The number of pages in the document is ' && lo_metadata->ask_pages( ) TYPE 'I'. MESSAGE 'Detect document text completed.' TYPE 'I'. CATCH /aws1/cx_texaccessdeniedex. MESSAGE 'You do not have permission to perform this action.' TYPE 'E'. CATCH /aws1/cx_texbaddocumentex. MESSAGE 'Amazon Textract is not able to read the document.' TYPE 'E'. CATCH /aws1/cx_texdocumenttoolargeex. MESSAGE 'The document is too large.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidparameterex. MESSAGE 'Request has non-valid parameters.' TYPE 'E'. CATCH /aws1/cx_texinvalids3objectex. MESSAGE 'Amazon S3 object is not valid.' TYPE 'E'. CATCH /aws1/cx_texprovthruputexcdex. MESSAGE 'Provisioned throughput exceeded limit.' TYPE 'E'. CATCH /aws1/cx_texthrottlingex. MESSAGE 'The request processing exceeded the limit' TYPE 'E'. CATCH /aws1/cx_texunsupporteddocex. MESSAGE 'The document is not supported.' TYPE 'E'. ENDTRY.
  • 有关API详细信息,请参阅DetectDocumentText中的AWS SDK以供SAPABAPAPI参考