Using SAP Leonardo as a keyword to search on WeChat, you can find many articles. But I looked at it. I didn’t seem to have introduced it from a specific programming point of view. So I’ll make a contribution.
Developing a Java program, the user can specify a picture. The Java program calls a trained machine on SAP Leonardo to learn API. The API will identify the picture, return a text message to the user, and tell the user to identify the results.
- Visit https://api.sap.com, click API:
SAP Leonardo Machine Learning – Functional Services
Click Product Image Classification API:
You will see the following interface. The interface contains the Model Schema introduction of this API, that is, what fields are included in the response structure that you return after you call the API, and the corresponding types (objects or arrays). The interface contains a small API call console, which you can directly connect to.Select a local graphics file from the console, then click the Try it out button to experience the effect of API.
For example, I used the following picture to test:
In the console, we saw that the request processing was successful. API judged that the picture had a 97% probability of being a notebook.
Next we need to use the Java program to call API.
- In the upper right corner of the API console, there is a button Download SDK. After clicking, the SDK is downloaded to the local:
You can see that the SDK is a Gradle based Java project. You need to download Maven and Gradle and complete the configuration of environment variables. These configurations are very basic. There is a lot of information on the Internet.
After completing the configuration, use Eclipse’s import function to import SDK into Eclipse.
When the import is completed, as a diagram. The red area is the code that comes with SDK. The blue area is the code created by Jerry manually, which is used to call API and print the result.
Double click the pom.xml in the root directory to maintain the following dependencies:
Execute the Maven command MVN install under the project root directory to ensure that the project is successful build. At this point, you can start writing Java code and use the JDK provided by SAP to call the machine to learn API.
- Use the Java code of the following figure to call SDK to consume API. SDK encapsulates the details of sending and responding the underlying HTTP request, which is very simple to use.
For simplicity, in the 13 row above, I hardcoded the absolute address of the local image file. The hard coded API key in the eighth row above is from the API console:
At this point, the work is finished. What happened?
Having read the error message carefully, Jerry soon found out where the problem was. The blue underline of the above picture suggests the root of the error. SAP SDK uses an open source Java library Gson released by Google to reverse the response returned by API to Java objects. With GSon, application developers do not need to duplicate wheels, only need to define some Java classes, as containers to store Gson anti serialization execution results.
Let’s go back to the API console, and the type of the request field in the API response structure is declared string.
However, through testing, it is found that the content of the request field is actually a Json object:
From the code in SDK downloaded by 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 aString but was BEGIN_OBJECT at line 31 column 15 path $.request
If you know the root of the problem, it’s easy to change it. Create a new Gson container class Request.java, and then maintain the corresponding member variables for Request.java according to the structure of the request field observed in the console.
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
Once executed, you can see the expected output result: calling API through SDK and printing results.
If you don’t want to manually modify it, you can download the revised code directly from my GitHub, only to replace the hard coded key in your code with your own API key.
More Leonardo wonderful, at https://api.sap.com!
To get more original technical articles of Jerry, please pay attention to the public number “Wang Zixi” or scan the following two-dimensional code: