Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Discover how to implement SAP Leonardo Machine Learning Functional Service in ABAP environment

The below tutorial explains the steps to implement SAP Leonardo Machine Learning Functional Service  using SAPUI5

https://www.sap.com/india/developer/tutorials/ml-fs-sapui5-img-classification.html

 

You can explore various Machine Learning API from  SAP Leonardo Machine Learning   Foundation using link below

https://api.sap.com/shell/discover/contentpackage/SAPLeonardoMLFunctionalServices?section=ARTIFACTS



 

Since  SAP Leonardo Machine Learning Functional Service are implemented as REST API, the same can be called from ABAP environment too. This blog post will explain the same.

Before I go into details of the implementation let me explain the use case.

We will be building a simple ABAP application to ask user for an image  input and pass it to REST API using  class CL_REST_HTTP_CLIENT. The Image Classification Service API will return us the classification of images with probabilities.

In my program I tested it using different types of images and the results were quite accurate.

Input to ABAP program



Output

 

 

 

 



 

It can bee seen the Machine Learning API was able to predict the image with good accuracy.

Now coming to the actual steps to implement the same

Step1:Get the API key 

Go to below URL and get your API key. You will have to register to get the API ket

https://api.sap.com/shell/discover/contentpackage/SAPLeonardoMLFunctionalServices/api/image_classifi...

The link to get API key is on top left corner



Step2: Create a ABAP report program

Copy the code below as a new report program.
*&---------------------------------------------------------------------*
*& Report ZML_IMAGE_CLASSIF
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zml_image_classif.

PARAMETERS: p_fname TYPE rlgrap-filename.


TYPES: BEGIN OF ty_filetab,
value TYPE x,
END OF ty_filetab.

DATA: lv_url TYPE string,
lt_filetable TYPE filetable,
lv_rc TYPE i,
lv_file_name TYPE string,
lt_file TYPE STANDARD TABLE OF ty_filetab,
lv_file_content TYPE xstring,
lv_file_form_data TYPE string.



AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.



CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Choose a file'
CHANGING
file_table = lt_filetable
rc = lv_rc.

lv_file_name = lt_filetable[ 1 ]-filename.
p_fname = lv_file_name.

AT SELECTION-SCREEN.


*Covert file to binary format
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_file_name
filetype = 'BIN'
IMPORTING
filelength = DATA(lv_input_len)
CHANGING
data_tab = lt_file.


*convert file to XSTRING
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_input_len
IMPORTING
buffer = lv_file_content
TABLES
binary_tab = lt_file.

************************Initailize HTTP resquest creation************************************

lv_url = 'https://sandbox.api.sap.com/ml/imageclassifier/inference_sync'.


cl_http_client=>create_by_url( EXPORTING url = lv_url
IMPORTING client = DATA(lo_http_client) ).

lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.

lo_http_client->request->set_method( if_http_request=>co_request_method_post ).


***********************************Request Headers*********************************************

* get data in JSON format
lo_http_client->request->set_header_field( name = 'Accept'
value = 'application/json' ).

* Set API key for image classification API
lo_http_client->request->set_header_field( name = 'APIKey'
value = 'Your Own API Key' ).

DATA(lo_rest_client) = NEW cl_rest_http_client( lo_http_client ).

DATA(lo_request) = lo_rest_client->if_rest_client~create_request_entity( ).

lo_request->set_content_type( if_rest_media_type=>gc_multipart_form_data ).

DATA(lo_post_file) = NEW cl_rest_multipart_form_data( lo_request ).

* send Image file to API
lo_post_file->set_file( iv_name = 'files' "This is the name of fied which API expects: See API documentation
iv_filename = lv_file_name
iv_type = 'image/jpeg'
iv_data = lv_file_content ).

lo_post_file->if_rest_entity_provider~write_to( lo_request ).

lo_rest_client->if_rest_resource~post( lo_request ).

*******************get Response******************************************
DATA(lo_response) = lo_rest_client->if_rest_client~get_response_entity( ).

DATA(lv_response) = lo_response->get_string_data( ).

cl_demo_output=>display( lv_response ).


*

Step 3: Replace  your own API key in the copied code.

Find the below code and replace it with your own API Key
* Set API key for image classification API
lo_http_client->request->set_header_field( name = 'APIKey'
value = 'Your Own API Key' ).

 

Step 4: Run and test your code.

Congratulations you have complete all steps and now you can test your program using different types of images.
8 Comments