Amazon Textract とAWS SDK を使用してドキュメント内のテキストを検出する - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Textract とAWS SDK を使用してドキュメント内のテキストを検出する

次のコード例は、Amazon Textract を使用してドキュメント内のテキストを検出する方法を示しています。

Java
SDK for Java 2.x
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

入力ドキュメントからテキストを検出します。

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 バケット内のテキストを検出します。

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.xAPI DetectDocumentTextリファレンスのを参照してください

Python
SDK for 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 の詳細については、「AWSSDK for Python (Boto3) API リファレンス」を参照してくださいDetectDocumentText

SAP ABAP
SDK for SAP ABAP
注記

これはデベロッパープレビューリリースの SDK に関するドキュメントです。SDK は変更される場合があるため、本稼働環境での使用はお勧めできません。

注記

他にもあります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." DATA lo_document TYPE REF TO /aws1/cl_texdocument. DATA lo_s3object TYPE REF TO /aws1/cl_texs3object. "Create an ABAP object for the Amazon S3 object." CREATE OBJECT lo_s3object EXPORTING iv_bucket = iv_s3bucket iv_name = iv_s3object. "Create an ABAP object for the document." CREATE OBJECT lo_document EXPORTING 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." 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 isn't 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 の詳細については、「DetectDocumentTextSDK for SAP ABAP API リファレンス」の「AWSSDK for SAP ABAP API リファレンス」の