如何使用JAVA将本地图像而不是URL发送到Microsoft Cognitive Face API [英] How to send a local image instead of URL to Microsoft Cognitive Face API using JAVA

查看:69
本文介绍了如何使用JAVA将本地图像而不是URL发送到Microsoft Cognitive Face API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试使用Microsoft Cognitive Services的Face API.我想知道如何通过其余API调用将本地图像发送到Face API,并使用 JAVA 从中请求结果.有人可以帮我吗?

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?

Microsoft在其网站上提供的测试"选项仅采用URL,我试图将本地路径转换为URL并将其作为输入提供,但这是行不通的.

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.

推荐答案

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

这篇关于如何使用JAVA将本地图像而不是URL发送到Microsoft Cognitive Face API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆