I have an object detection model deployed to an endpoint in azure. In python, the code works perfect and looks like this:
headers = {'Content-Type': 'application/json',
'Authorization': ('Bearer ' + api_key)}
sample_image = os.path.join(directory, filename)
# Load image data
body = open(sample_image, "rb").read()
req = urllib.request.Request(url, body, headers)
response = urllib.request.urlopen(req)
I'm trying to do the same thing in a UDJC in PDI Kettle. This is what that code looks like:
String urlString = get(Fields.In, "ENDPOINT_URL").getString(r);
HttpPost post = new HttpPost(urlString);
post.addHeader("Content-Type",get(Fields.In, "CONTENTTYPE").getString(r));
post.addHeader("Authorization",get(Fields.In, "API_KEY").getString(r));
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.STRICT);
entity.addBinaryBody("file", get(Fields.In, "IMAGEDATA").getBinary(r));
post.setEntity(entity.build());
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
I'm getting this error: http_status result -1 java.net.SocketException: Broken pipe (Write failed)
I think there's an issue in how the image is being written. How do I get java to write it the same way python is writing it? Any suggestions? Thanks
You can use following packages in java to score an endpoint.
Below is the code.
This is the endpoint responses the object detection boundaries.
And this accepts the json body like below.
Here, you need to provide base64 string in data option. If you have multiple images, you can also add them in index and data correspondingly like below.
So, in code you need to create a json object whichever your endpoint accepts. Check the auto-ml deployment details to know the input structure. In this code below part creates json object.
Output: