【问题标题】:How to fix "InvalidImageSize","message":"Image size is too small."如何修复“InvalidImageSize”、“消息”:“图像尺寸太小。”
【发布时间】:2020-01-27 23:26:46
【问题描述】:

[在此处输入图像描述][1]我一直在尝试使用 azure 认知“面部检测”服务。在将图像作为 url 传递时,我能够从服务中获得肯定的响应,但是在将图像转换为字节后传递时,服务确实会抛出错误: { "code": "InvalidImageSize", "message": "图像尺寸太小。" } 我确实确保(在调试模式下)转换后的字节大小为 1194Kb,远低于限制(1Kb 到 6Mb)。虽然我不确定我做错了什么:|

我确实尝试过以多种方式将图像转换为字节,但都是徒劳的。

我的最终目标是:我需要接受图像的 base64 表示并调用此人脸检测服务,而不是从本地读取图像。

任何帮助将不胜感激,谢谢。

String photo = "C:\\dev\\check.jpeg";
        try {
            byte[] readAllBytes = Files.readAllBytes(Paths.get(photo));
            ByteArrayEntity reqEntity = new ByteArrayEntity(readAllBytes, ContentType.APPLICATION_OCTET_STREAM);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.set("Ocp-Apim-Subscription-Key", "xxxxxxxxxxxx");

            Map<String, String> params = new HashMap<>();
            params.put("returnFaceId", "true");
            params.put("recognitionModel", "recognition_02");
            params.put("detectionModel", "detection_02");

            ResponseEntity<List<DetectFaceRes>> exchange = restTemplateFaceApiService.exchange(getUri(DETECT_FACE.getMapping()), HttpMethod.POST, new HttpEntity<>(reqEntity, headers), new ParameterizedTypeReference<List<DetectFaceRes>>(){}, params);
            if(responseHasTargetFace(exchange)) {
                return exchange.getBody();
            }
            log.error("some error");
            throw someExpception()
        }

Error:
{
    "code": "InvalidImageSize",
    "message": "Image size is too small."
}


  [1]: https://i.stack.imgur.com/JJH8U.jpg

【问题讨论】:

  • 这是Spring吗?如果是这样,包含该声明将是有益的
  • @cthrash:是的,它与 Spring 一起使用。已添加标签。谢谢!
  • 我的意思是实现exchange的那段代码。我不是 Spring 专家,但我怀疑您获得了分块传输,该服务不支持。您应该能够通过添加 Content-Length 标头 IIUC 来禁用分块。看看stackoverflow.com/questions/50110138/…
  • 感谢@cthrash 的评论,我之前确实尝试过关闭分块,但得到了相同的响应。甚至在请求标头中传递了内容大小但是:(
  • 我直接使用字节数组而不是apache中的ByteArrayEntity,并获得了成功。检查我的答案,看看它是否有帮助。谢谢。

标签: java spring azure-cognitive-services face-api


【解决方案1】:

为了简化测试,我只是将响应读取为字符串。我下载你的图片并在我的代码中使用它。

我通过以下代码获得了成功:

    public static void TestRestTemplateExchange() {
        RestTemplate rt = new RestTemplate();

        String photo = "C:\\Users\\v-linjji\\Pictures\\test.jpg";
        byte[] readAllBytes = null;
        try {
            readAllBytes = Files.readAllBytes(Paths.get(photo));
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.set("Ocp-Apim-Subscription-Key","1fb1*********eb8b");

        Map<String, String> params = new HashMap<>();
        params.put("returnFaceId", "true");
        params.put("recognitionModel", "recognition_02");
        params.put("detectionModel", "detection_02");

        String url = "https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect";
        ResponseEntity<String> responseEntity = null;
        String responseBody = null;
        try {
            responseEntity = rt.exchange(url,HttpMethod.POST, new HttpEntity<>(readAllBytes, headers), String.class,params);
            responseBody = responseEntity.getBody();
            System.out.println(responseBody);
        }catch (HttpClientErrorException e){
            System.out.println(e.getResponseBodyAsString());
        }
    }

回复:

[{"faceId":"75c3c9dc-be49-4c16-a54c-a1710062967b","faceRectangle":{"top":570,"left":360,"width":444,"height":444}}]

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,因为我在识别之前打开了照片。所以我删除了打开并且代码有效。我在用 Python 编程。

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2020-06-05
      • 2011-08-02
      • 2018-11-09
      • 2020-05-03
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多