【问题标题】:Azure Face API not working with local fileAzure Face API 无法使用本地文件
【发布时间】:2018-03-09 09:02:27
【问题描述】:

我一直在尝试将图像从我的计算机发送到此 API,但我只收到以下错误:{"error":{"code":"InvalidImageSize","message":"Image size is too small."}}

我的代码如下。 我有一个使用这种方法的 PostRequestClass:

public void sendImageRequest(String imagePath) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        File file = new File(imagePath);
        FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(false);
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            this.responseResult = EntityUtils.toString(entity);
        }
    } catch(Exception e) {
        System.out.println(e.getMessage());
    }   
}

我的主要是这个:

public class Test {
    public static void main(String[] args) throws URISyntaxException {
        PostRequest p = new PostRequest(
          "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=emotion"
        );
        p.addHeader("Content-Type", "application/octet-stream");
        p.addHeader("Ocp-Apim-Subscription-Key", "my-api-key");
        p.sendImageRequest("/Users/user/Desktop/image.jpg");
        System.out.println(p.getResponseResult());           
    }
}

【问题讨论】:

    标签: java azure face-api


    【解决方案1】:

    我用下面的代码解决了:

    public void sendImageRequest(String imagePath) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
    
            File file = new File(imagePath);
            FileInputStream fileInputStreamReader = new FileInputStream(file);
            byte[] bytes = new byte[(int)file.length()];
            fileInputStreamReader.read(bytes);            
            ByteArrayEntity reqEntity = new ByteArrayEntity(bytes, ContentType.APPLICATION_OCTET_STREAM);
            request.setEntity(reqEntity);
    
            HttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                this.responseResult = EntityUtils.toString(entity);
            }
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }   
    }
    

    【讨论】:

      【解决方案2】:
      1. 转到https://azure.microsoft.com/en-us/services/cognitive-services/face/并点击“API参考”。

      2. 它将带您到人脸 API 参考页面https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

      Face API 文档显示“支持 JPEG、PNG、GIF(第一帧)和 BMP 格式。允许的图像文件大小为 1KB 到 4MB。”

      在“以 JSON 格式返回的错误代码和消息”标题下, 它说,“InValidImageSize”的意思是“有效的图像文件大小应该大于或等于1KB。”

      【讨论】:

      • 谢谢,我在下面尝试了我的解决方案,它奏效了。我认为我没有以正确的方式发送请求。
      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2012-08-02
      • 1970-01-01
      • 2018-02-14
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多