Amazon Rekognition examples using SDK for SAP ABAP - AWS SDK for SAP ABAP

Amazon Rekognition examples using SDK for SAP ABAP

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for SAP ABAP with Amazon Rekognition.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CompareFaces.

For more information, see Comparing faces in images.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the source image DATA(lo_source_s3obj) = NEW /aws1/cl_reks3object( iv_bucket = iv_source_s3_bucket iv_name = iv_source_s3_key ). " Create source image object DATA(lo_source_image) = NEW /aws1/cl_rekimage( io_s3object = lo_source_s3obj ). " Create S3 object reference for the target image DATA(lo_target_s3obj) = NEW /aws1/cl_reks3object( iv_bucket = iv_target_s3_bucket iv_name = iv_target_s3_key ). " Create target image object DATA(lo_target_image) = NEW /aws1/cl_rekimage( io_s3object = lo_target_s3obj ). " Compare faces oo_result = lo_rek->comparefaces( io_sourceimage = lo_source_image io_targetimage = lo_target_image iv_similaritythreshold = iv_similarity ). DATA(lt_face_matches) = oo_result->get_facematches( ). DATA(lt_unmatched_faces) = oo_result->get_unmatchedfaces( ). " Get counts of matched and unmatched faces DATA(lv_matched_count) = lines( lt_face_matches ). DATA(lv_unmatched_count) = lines( lt_unmatched_faces ). " Output detailed comparison results DATA(lv_message) = |Face comparison completed: | && |{ lv_matched_count } matched face(s), | && |{ lv_unmatched_count } unmatched face(s).|. MESSAGE lv_message TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see CompareFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use CreateCollection.

For more information, see Creating a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->createcollection( iv_collectionid = iv_collection_id ). MESSAGE 'Collection created successfully.' TYPE 'I'. CATCH /aws1/cx_rekresrcalrdyexistsex. MESSAGE 'Collection already exists.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.

The following code example shows how to use DeleteCollection.

For more information, see Deleting a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. lo_rek->deletecollection( iv_collectionid = iv_collection_id ). MESSAGE 'Collection deleted successfully.' TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.

The following code example shows how to use DeleteFaces.

For more information, see Deleting faces from a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->deletefaces( iv_collectionid = iv_collection_id it_faceids = it_face_ids ). DATA(lt_deleted_faces) = oo_result->get_deletedfaces( ). DATA(lv_deleted_count) = lines( lt_deleted_faces ). DATA(lv_msg6) = |{ lv_deleted_count } face(s) deleted successfully.|. MESSAGE lv_msg6 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see DeleteFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DescribeCollection.

For more information, see Describing a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->describecollection( iv_collectionid = iv_collection_id ). DATA(lv_face_count) = oo_result->get_facecount( ). DATA(lv_msg) = |Collection described: { lv_face_count } face(s) indexed.|. MESSAGE lv_msg TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.

The following code example shows how to use DetectFaces.

For more information, see Detecting faces in an image.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Detect faces in the image with all attributes DATA(lt_attributes) = VALUE /aws1/cl_rekattributes_w=>tt_attributes( ). DATA(lo_attr_wrapper) = NEW /aws1/cl_rekattributes_w( iv_value = 'ALL' ). INSERT lo_attr_wrapper INTO TABLE lt_attributes. oo_result = lo_rek->detectfaces( io_image = lo_image it_attributes = lt_attributes ). DATA(lt_face_details) = oo_result->get_facedetails( ). DATA(lv_detected_count) = lines( lt_face_details ). DATA(lv_msg8) = |{ lv_detected_count } face(s) detected in image.|. MESSAGE lv_msg8 TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see DetectFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DetectLabels.

For more information, see Detecting labels in an image.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Detect labels in the image oo_result = lo_rek->detectlabels( io_image = lo_image iv_maxlabels = iv_max_labels ). DATA(lt_labels) = oo_result->get_labels( ). DATA(lv_label_count) = lines( lt_labels ). DATA(lv_msg9) = |{ lv_label_count } label(s) detected in image.|. MESSAGE lv_msg9 TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see DetectLabels in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DetectModerationLabels.

For more information, see Detecting inappropriate images.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Detect moderation labels oo_result = lo_rek->detectmoderationlabels( io_image = lo_image ). DATA(lt_moderation_labels) = oo_result->get_moderationlabels( ). DATA(lv_mod_count) = lines( lt_moderation_labels ). DATA(lv_msg10) = |{ lv_mod_count } moderation label(s) detected.|. MESSAGE lv_msg10 TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.

The following code example shows how to use DetectText.

For more information, see Detecting text in an image.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Detect text in the image oo_result = lo_rek->detecttext( io_image = lo_image ). DATA(lt_text_detections) = oo_result->get_textdetections( ). DATA(lv_text_count) = lines( lt_text_detections ). DATA(lv_msg11) = |{ lv_text_count } text detection(s) found.|. MESSAGE lv_msg11 TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see DetectText in AWS SDK for SAP ABAP API reference.

The following code example shows how to use IndexFaces.

For more information, see Adding faces to a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Index faces in the image oo_result = lo_rek->indexfaces( iv_collectionid = iv_collection_id io_image = lo_image iv_externalimageid = iv_external_id iv_maxfaces = iv_max_faces ). DATA(lt_face_records) = oo_result->get_facerecords( ). DATA(lv_indexed_count) = lines( lt_face_records ). DATA(lv_msg2) = |{ lv_indexed_count } face(s) indexed successfully.|. MESSAGE lv_msg2 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see IndexFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListCollections.

For more information, see Listing collections.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->listcollections( iv_maxresults = iv_max_results ). DATA(lt_collection_ids) = oo_result->get_collectionids( ). DATA(lv_coll_count) = lines( lt_collection_ids ). DATA(lv_msg7) = |{ lv_coll_count } collection(s) found.|. MESSAGE lv_msg7 TYPE 'I'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see ListCollections in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListFaces.

For more information, see Listing faces in a collection.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->listfaces( iv_collectionid = iv_collection_id iv_maxresults = iv_max_results ). DATA(lt_faces) = oo_result->get_faces( ). DATA(lv_face_count2) = lines( lt_faces ). DATA(lv_msg3) = |{ lv_face_count2 } face(s) found in collection.|. MESSAGE lv_msg3 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see ListFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use RecognizeCelebrities.

For more information, see Recognizing celebrities in an image.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Recognize celebrities oo_result = lo_rek->recognizecelebrities( io_image = lo_image ). DATA(lt_celebrity_faces) = oo_result->get_celebrityfaces( ). DATA(lv_celeb_count) = lines( lt_celebrity_faces ). DATA(lv_msg12) = |{ lv_celeb_count } celebrity/celebrities recognized.|. MESSAGE lv_msg12 TYPE 'I'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.

The following code example shows how to use SearchFaces.

For more information, see Searching for a face (face ID).

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_rek->searchfaces( iv_collectionid = iv_collection_id iv_faceid = iv_face_id iv_facematchthreshold = iv_threshold iv_maxfaces = iv_max_faces ). DATA(lt_face_matches) = oo_result->get_facematches( ). DATA(lv_match_count2) = lines( lt_face_matches ). DATA(lv_msg5) = |Face search completed: { lv_match_count2 } match(es) found.|. MESSAGE lv_msg5 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection or face not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.
  • For API details, see SearchFaces in AWS SDK for SAP ABAP API reference.

The following code example shows how to use SearchFacesByImage.

For more information, see Searching for a face (image).

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " Create S3 object reference for the image DATA(lo_s3object) = NEW /aws1/cl_reks3object( iv_bucket = iv_s3_bucket iv_name = iv_s3_key ). " Create image object DATA(lo_image) = NEW /aws1/cl_rekimage( io_s3object = lo_s3object ). " Search for matching faces oo_result = lo_rek->searchfacesbyimage( iv_collectionid = iv_collection_id io_image = lo_image iv_facematchthreshold = iv_threshold iv_maxfaces = iv_max_faces ). DATA(lt_face_matches) = oo_result->get_facematches( ). DATA(lv_match_count) = lines( lt_face_matches ). DATA(lv_msg4) = |Face search completed: { lv_match_count } match(es) found.|. MESSAGE lv_msg4 TYPE 'I'. CATCH /aws1/cx_rekresourcenotfoundex. MESSAGE 'Collection not found.' TYPE 'E'. CATCH /aws1/cx_rekinvalids3objectex. MESSAGE 'Invalid S3 object.' TYPE 'E'. CATCH /aws1/cx_rekinvalidparameterex. MESSAGE 'Invalid parameter value.' TYPE 'E'. ENDTRY.