You want to know how to actually start using SAP Leonardo Machine Learning? The Machine Learning foundation offers a wide range of functional services, from image to text classification that are easy to consume. You can find a first overview of all these services available on the SAP API Business Hub and try them out through APIs calls. The only prerequisites you will need are the SAP Cloud Platform Tools for Java in your Eclipse IDE, SAP Cloud Platform Software Development Kit, Eclipse (they can be set up and configured with this tutorial).
// create a new URLConnection object to configure our HTTP request
URLConnection connection = new URL("https://sandbox.api.sap.com/ml/prodimgclassifier/inference_sync").openConnection();
connection.setDoOutput(true); // send data through this connection --> HTTP POST request
// user authentication and authorization
connection.setRequestProperty("APIKey", "<API_KEY>");
// random boundary between different files of multipart/form-data request
String boundary = Long.toHexString(System.currentTimeMillis());
// line separator required by multipart/form-data format
String CRLF = "\r\n";
// for sending files we need multipart/form-data
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
// character encoding used in our http requests
String charset = java.nio.charset.StandardCharsets.UTF_8.name();
// the image file we want to classify
String path = "";
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
// send image file
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"files\"; filename=\"" + imgFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(imgFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Files.copy(imgFile.toPath(), output);
output.flush(); // need to flush before continuing with writer
writer.append(CRLF);
// mark end of multipart/form-data
writer.append("--" + boundary + "--").append(CRLF).flush();
}
// performing the http request against the Leonardo ML service endpoint
InputStream mlServiceResponse = connection.getInputStream();
try (Scanner scanner = new Scanner(mlServiceResponse)) {
// reading the response from the Leonardo ML service endpoint
String responseBody = scanner.useDelimiter("\\A").next();
// sending a response to the client
userResp.getWriter().println(responseBody);
}
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.nio.file.Files;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |