Reputation: 61
Am trying to play with Face API of Microsoft Cognitive Services. Am wondering how to send a local image through rest API calls to Face API and request for the results from it using JAVA. Can anyone help me with this please?
The Testing opting provided by Microsoft on their site only takes URL, I Tried to convert my local path to URL and give it as input but that doesn't work.
Upvotes: 5
Views: 3232
Reputation: 11
Working with FileEntity seems to be the most elegant way of solving the problem. There has been a small change to ctrash's code: Since the constructor FileEntity(File, String)
has deprecated, the constructor FileEntity(File, ContentType)
has to be used. In this case, the Content Type of both the header and the image file has to be "application/octet-stream". I got it to work with this Code:
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
import java.io.File;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class PostTest
{
public static void main(String[] args)
{
CloseableHttpClient httpclient = HttpClients.createDefault();
try
{
URIBuilder uriBuilder = new URIBuilder("https://westus.api.cognitive.microsoft.com/face/v1.0/detect");
uriBuilder.setParameter("returnFaceId", "true");
uriBuilder.setParameter("returnFaceLandmarks", "false");
uriBuilder.setParameter("returnFaceAttributes", "age");
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
// Request headers. Replace the example key below with your valid subscription key.
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "YOUR_SUBSCRIPTION_KEY");
// Request body
File file = new File("YOUR_IMAGE_FILE");
FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
After correct execution, the terminal will show the JSON object the Face API returns.
Thank you, cthrash, for this is the only place in the internet where I learned how to do this!
Upvotes: 0
Reputation: 2973
One option is to use the FileEntity
class.
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
import java.io.File;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Face
{
public static void main(String[] args)
{
HttpClient httpclient = HttpClients.createDefault();
try
{
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
builder.setParameter("returnFaceId", "true");
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "YOUR_KEY");
// Request body
File file = new File("YOUR_FILE");
FileEntity reqEntity = new FileEntity(file, "application/octet-stream");
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
Upvotes: 1