我试图使用Microsoft Cognitive Services的Face API进行游戏。我想知道如何通过其余API调用Face API来发送本地图像,并使用JAVA来请求Face API的结果。任何人都可以帮助我吗?如何使用JAVA向Microsoft Cognitive Face API发送本地图像而不是URL
微软在其网站上提供的测试选择只需要URL,我尝试将我的本地路径转换为URL并将其作为输入,但不起作用。
我试图使用Microsoft Cognitive Services的Face API进行游戏。我想知道如何通过其余API调用Face API来发送本地图像,并使用JAVA来请求Face API的结果。任何人都可以帮助我吗?如何使用JAVA向Microsoft Cognitive Face API发送本地图像而不是URL
微软在其网站上提供的测试选择只需要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(File, String)
已被弃用,所以必须使用构造函数FileEntity(File, ContentType)
。在这种情况下,头文件和图像文件的内容类型必须是“application/octet-stream”。我得到它与这个代码一起工作:
// 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());
}
}
}
正确执行后,终端将显示面部API返回的JSON对象。
谢谢你,cthrash,因为这是互联网上唯一的地方,我学会了如何做到这一点!
不确定你正在查看“他们的网站”的哪一部分。从“Microsoft Cognitive Services - Documentation”,https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/get-started-with-face-api/gettingstartedwithfaceapiinjavaforandroid,它看起来像te' detect'方法需要一个包含JPEG格式图像的'InputSteam'。 – david