簡體   English   中英

如何修復“InvalidImageSize”、“消息”:“圖像尺寸太小”。

[英]How to fix “InvalidImageSize”,“message”:“Image size is too small.”

[在此處輸入圖片描述][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

為了簡化測試,我只是將響應讀取為字符串。 我下載你的圖片並在我的代碼中使用它。

我使用以下代碼獲得了成功:

    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}}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM