如何使用JAVA

时间:2016-09-17 00:10:47

标签: java microsoft-cognitive

我正在尝试使用Microsoft认知服务的Face API。我想知道如何通过对Face API的其他API调用发送本地图像,并使用 JAVA 从中请求结果。有人可以帮我这个吗?

Microsoft在其网站上提供的测试选择仅使用URL,我尝试将我的本地路径转换为URL并将其作为输入提供但不起作用。

2 个答案:

答案 0 :(得分:1)

一种选择是使用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());
        }
    }
}

答案 1 :(得分:0)

使用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());
        }
    }
}

正确执行后,终端将显示Face API返回的JSON对象。

谢谢你,cthrash,因为这是互联网上我学习如何做到这一点的唯一地方!