Esempi di Amazon Textract con SDK per SAP ABAP - AWS Esempi di codice SDK

Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Esempi di Amazon Textract con SDK per SAP ABAP

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando l' AWS SDK per SAP ABAP con Amazon Textract.

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati e negli esempi tra servizi.

Scenari: esempi di codice che mostrano come eseguire un'attività specifica richiamando più funzioni all'interno dello stesso servizio.

Ogni esempio include un collegamento a GitHub, dove puoi trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

Il seguente esempio di codice mostra come utilizzareAnalyzeDocument.

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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.
  • Per i dettagli sulle API, AnalyzeDocumentconsulta AWS SDK for SAP ABAP API reference.

Il seguente esempio di codice mostra come utilizzare. DetectDocumentText

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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.
  • Per i dettagli sulle API, DetectDocumentTextconsulta AWS SDK for SAP ABAP API reference.

Il seguente esempio di codice mostra come utilizzare. GetDocumentAnalysis

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

"Gets the results for an Amazon Textract" "asynchronous operation that analyzes text in a document." TRY. oo_result = lo_tex->getdocumentanalysis( iv_jobid = iv_jobid ). "oo_result is returned for testing purposes." WHILE oo_result->get_jobstatus( ) <> 'SUCCEEDED'. IF sy-index = 10. EXIT. "Maximum 300 seconds. ENDIF. WAIT UP TO 30 SECONDS. oo_result = lo_tex->getdocumentanalysis( iv_jobid = iv_jobid ). ENDWHILE. DATA(lt_blocks) = oo_result->get_blocks( ). LOOP AT lt_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 'Document analysis retrieved.' TYPE 'I'. CATCH /aws1/cx_texaccessdeniedex. MESSAGE 'You do not have permission to perform this action.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidjobidex. MESSAGE 'Job ID is not valid.' TYPE 'E'. CATCH /aws1/cx_texinvalidkmskeyex. MESSAGE 'AWS KMS key is not valid.' 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'. ENDTRY.

Il seguente esempio di codice mostra come utilizzare. StartDocumentAnalysis

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

"Starts the asynchronous analysis of an input document for relationships" "between detected items such as key-value pairs, tables, and selection elements." "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 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_documentlocation) = NEW /aws1/cl_texdocumentlocation( io_s3object = lo_s3object ). "Start async document analysis." TRY. oo_result = lo_tex->startdocumentanalysis( "oo_result is returned for testing purposes." io_documentlocation = lo_documentlocation it_featuretypes = lt_featuretypes ). DATA(lv_jobid) = oo_result->get_jobid( ). MESSAGE 'Document analysis started.' 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_texidempotentprmmis00. MESSAGE 'Idempotent parameter mismatch exception.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidkmskeyex. MESSAGE 'AWS KMS key is not valid.' 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_texlimitexceededex. MESSAGE 'An Amazon Textract service limit was exceeded.' 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.

Il seguente esempio di codice mostra come utilizzare. StartDocumentTextDetection

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

"Starts the asynchronous detection of text in a document." "Amazon Textract can detect lines of text and the words that make up a line of text." "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_documentlocation) = NEW /aws1/cl_texdocumentlocation( io_s3object = lo_s3object ). "Start document analysis." TRY. oo_result = lo_tex->startdocumenttextdetection( io_documentlocation = lo_documentlocation ). DATA(lv_jobid) = oo_result->get_jobid( ). "oo_result is returned for testing purposes." MESSAGE 'Document analysis started.' 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_texidempotentprmmis00. MESSAGE 'Idempotent parameter mismatch exception.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidkmskeyex. MESSAGE 'AWS KMS key is not valid.' 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_texlimitexceededex. MESSAGE 'An Amazon Textract service limit was exceeded.' 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.

Scenari

L'esempio di codice seguente mostra come:

  • Avvia l'analisi asincrona.

  • Ottieni l'analisi dei documenti.

SDK per SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

"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_documentlocation) = NEW /aws1/cl_texdocumentlocation( io_s3object = lo_s3object ). "Start document analysis." TRY. DATA(lo_start_result) = lo_tex->startdocumentanalysis( io_documentlocation = lo_documentlocation it_featuretypes = lt_featuretypes ). MESSAGE 'Document analysis started.' 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_texidempotentprmmis00. MESSAGE 'Idempotent parameter mismatch exception.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidkmskeyex. MESSAGE 'AWS KMS key is not valid.' 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_texlimitexceededex. MESSAGE 'An Amazon Textract service limit was exceeded.' 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. "Get job ID from the output." DATA(lv_jobid) = lo_start_result->get_jobid( ). "Wait for job to complete." oo_result = lo_tex->getdocumentanalysis( iv_jobid = lv_jobid ). " oo_result is returned for testing purposes. " WHILE oo_result->get_jobstatus( ) <> 'SUCCEEDED'. IF sy-index = 10. EXIT. "Maximum 300 seconds." ENDIF. WAIT UP TO 30 SECONDS. oo_result = lo_tex->getdocumentanalysis( iv_jobid = lv_jobid ). ENDWHILE. DATA(lt_blocks) = oo_result->get_blocks( ). LOOP AT lt_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.