an AWS SDK 또는 CLIAnalyzeDocument와 함께 사용 - AWS SDK 코드 예제

AWS Doc SDK ExamplesWord AWS SDK 리포지토리에는 더 많은 GitHub 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

an AWS SDK 또는 CLIAnalyzeDocument와 함께 사용

다음 코드 예제는 AnalyzeDocument의 사용 방법을 보여 줍니다.

CLI
AWS CLI

문서의 텍스트를 분석하는 방법

다음 analyze-document 예시에서는 문서에서 텍스트를 분석하는 방법을 보여줍니다.

Linux/macOS:

aws textract analyze-document \ --document '{"S3Object":{"Bucket":"bucket","Name":"document"}}' \ --feature-types '["TABLES","FORMS"]'

Windows:

aws textract analyze-document \ --document "{\"S3Object\":{\"Bucket\":\"bucket\",\"Name\":\"document\"}}" \ --feature-types "[\"TABLES\",\"FORMS\"]" \ --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": [ "87586964-d50d-43e2-ace5-8a890657b9a0", "a1e72126-21d9-44f4-a8d6-5c385f9002ba", "e889d012-8a6b-4d2e-b7cd-7a8b327d876a" ] } ], "BlockType": "PAGE", "Id": "c2227f12-b25d-4e1f-baea-1ee180d926b2" } ], "DocumentMetadata": { "Pages": 1 } }

자세한 내용은 Amazon Textract 개발자 안내서의 Analyzing Document Text with Amazon Textract를 참조하세요.

  • API 세부 정보는 AWS CLI 명령 참조AnalyzeDocument를 참조하세요.

Java
Java 2.x용 SDK
참고

더 많은 on 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.AnalyzeDocumentRequest; import software.amazon.awssdk.services.textract.model.Document; import software.amazon.awssdk.services.textract.model.FeatureType; import software.amazon.awssdk.services.textract.model.AnalyzeDocumentResponse; import software.amazon.awssdk.services.textract.model.Block; 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.ArrayList; import java.util.Iterator; 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 AnalyzeDocument { 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(); analyzeDoc(textractClient, sourceDoc); textractClient.close(); } public static void analyzeDoc(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(); List<FeatureType> featureTypes = new ArrayList<FeatureType>(); featureTypes.add(FeatureType.FORMS); featureTypes.add(FeatureType.TABLES); AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder() .featureTypes(featureTypes) .document(myDoc) .build(); AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest); List<Block> docInfo = analyzeDocument.blocks(); Iterator<Block> blockIterator = docInfo.iterator(); while (blockIterator.hasNext()) { Block block = blockIterator.next(); System.out.println("The block type is " + block.blockType().toString()); } } catch (TextractException | FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • API 세부 정보는 AnalyzeDocument AWS SDK for Java 2.x 참조의 API를 참조하세요.

Python
Python용 SDK(Boto3)
참고

더 많은 on 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 analyze_file( self, feature_types, *, document_file_name=None, document_bytes=None ): """ Detects text and additional elements, such as forms or tables, in a local image file or from in-memory byte data. The image must be in PNG or JPG format. :param feature_types: The types of additional document features to detect. :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.analyze_document( Document={"Bytes": document_bytes}, FeatureTypes=feature_types ) logger.info("Detected %s blocks.", len(response["Blocks"])) except ClientError: logger.exception("Couldn't detect text.") raise else: return response
  • API 세부 정보는 Word for Python(Boto3) AnalyzeDocument 참조의 Word를 참조하세요. AWS SDK API

SAP ABAP
SDK for SAP ABAP
참고

더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

"Detects text and additional elements, such as forms or tables," "in a local image file or from in-memory byte data." "The image must be in PNG or JPG format." "Create ABAP objects for feature type." "Add TABLES to return information about the tables." "Add FORMS to return detected form data." "To perform both types of analysis, add TABLES and FORMS to FeatureTypes." DATA(lt_featuretypes) = VALUE /aws1/cl_texfeaturetypes_w=>tt_featuretypes( ( NEW /aws1/cl_texfeaturetypes_w( iv_value = 'FORMS' ) ) ( NEW /aws1/cl_texfeaturetypes_w( iv_value = 'TABLES' ) ) ). "Create an ABAP object for the Amazon Simple Storage Service (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->analyzedocument( "oo_result is returned for testing purposes." io_document = lo_document it_featuretypes = lt_featuretypes ). 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. MESSAGE 'Analyze document 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_texhlquotaexceededex. MESSAGE 'Human loop quota exceeded.' 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 세부 정보는 AnalyzeDocument AWS for SDK Word 참조의 API SAPABAP 참조하세요.