Use Java programs to consume SAP Leonardo's machine learning API

Source: Internet
Author: User

With SAP Leonardo as the key word on the search, can search a lot of articles. But I looked at it as if I had not found a specific programming perspective to introduce. So I'm going to contribute a piece.

Demand

Develop a Java program, the user can specify a picture, the Java program calls the SAP Leonardo training machine learning API, the API will recognize the picture, give the user to return a text message, tell the user to identify the results.

    1. To access https://api.sap.com, click API:


SAP Leonardo Machine learning-functional Services

Click the product Image classification API:

You will see the following interface. The interface contains the model schema for this API, which is what fields are included in the response structure that is returned after you call the API, and the corresponding type (object or array). The interface contains a small API call console, you can select a local graphics file directly in the console, and then click the Try it out button to experience the effect of the API.

For example, I used the following image to test:

In the console to see the request processing success, the API to determine that the picture has a 97% probability is a notebook.


Below we need to use a Java program to invoke the API.

    1. In the upper right corner of the API console in the previous step there is a button download SDK, which downloads the SDK to local after clicking:


You can see that the SDK is a Java project based on Gradle. You need to download Maven and Gradle and complete the configuration of the environment variables, which are very basic and have a lot of information on the web, which is not repeated here.
After the configuration is complete, import the SDK into Eclipse using the import feature of Eclipse.

After the import is complete. The red area is the code that comes with the SDK, and the blue area is the code that Jerry created manually to invoke the API and print the results.

Double-click Pom.xml in the root directory to maintain the following dependencies:


Execute the MAVEN command mvn install under the project root to ensure a successful build of the project. At this point, you can start writing Java code to invoke the Machine learning API using the JDK provided by SAP.

    1. Use Java code to call the SDK to consume the API. The SDK encapsulates the details of sending and responding to the underlying HTTP request, which is very straightforward to use.

For simplicity, 13 lines I hard-coded the absolute address of the local picture file. The eighth line of hard-coded API keys comes from the API console:

At this point, you are done, run the program ... What's the situation?

Error analysis

Looking carefully at the error message, Jerry soon found out where the problem was. The blue underline hints at the source of the error. The SAP SDK uses an open source Java library Gson published by Google to deserialize the response returned by the API into a Java object. With Gson, application developers do not have to reinvent the wheel, they only need to define some Java classes as container storage Gson the execution results of deserialization.

We go back to the API console, where the request field type is declared as string in the API response structure.

However, the test found that the content of the request field is actually a JSON object:

The code in the SDK downloaded from http://api.sap.com, the Request field type declared in the Gson container class Response.java is string, not an object, so Gson throws an exception: expected a string but is Begin_object at line-column $.request

Knowing the root of the problem makes it easier to change it. Create a new Gson container class Request.java, and then follow the structure of the request field observed in the console to maintain the corresponding member variables for Request.java.
For example, Tenantname is a String in the API response structure, so the type in the Java class is string;files is an array in the API response structure, so the type in the Java class is list<string>.


Once the adjustment is complete, you can see the expected output: The API is called through the SDK and the results are printed.


If you prefer not to manually modify it yourself, you can download the modified code directly from my github and simply replace the hard-coded key in your code with your own API key.

Https://github.com/i042416/ProductImageMLService

More Leonardo wonderful, in https://api.sap.com!

To get more original Jerry's technical articles, please follow the public number "Wang Zixi" or scan the QR code below:

Use Java programs to consume SAP Leonardo's machine learning API

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.