如何使用JAVA将本地图像而不是URL发送到Microsoft Cognitive Face API
我正在尝试使用Microsoft认知服务的Face API。我想知道如何通过RESTAPI调用将本地映像发送到Face API,并使用JAVA请求结果。有人能帮我吗如何使用JAVA将本地图像而不是URL发送到Microsoft Cognitive Face API,java,microsoft-cognitive,Java,Microsoft Cognitive,我正在尝试使用Microsoft认知服务的Face API。我想知道如何通过RESTAPI调用将本地映像发送到Face API,并使用JAVA请求结果。有人能帮我吗 微软在他们的网站上提供的测试选项只接受URL,我试图将我的本地路径转换为URL并将其作为输入,但这不起作用 一个选项是使用FileEntity类 // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomp
微软在他们的网站上提供的测试选项只接受URL,我试图将我的本地路径转换为URL并将其作为输入,但这不起作用 一个选项是使用
FileEntity
类
// 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());
}
}
}
使用FileEntity似乎是解决问题的最优雅的方式。ctrash的代码有一个小小的变化:由于构造函数
FileEntity(文件,字符串)
已被弃用,因此必须使用构造函数FileEntity(文件,内容类型)
。在这种情况下,头部和图像文件的内容类型都必须是“应用程序/八位字节流”。我让它与以下代码一起工作:
// 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());
}
}
}
正确执行后,终端将显示Face API返回的JSON对象
谢谢你,克特拉什,因为这是互联网上唯一一个我学会如何做这件事的地方 不知道你在看“他们的网站”的哪一部分。在“Microsoft认知服务-文档”中,tedetect
方法采用包含JPEG格式图像的InputSteam
。