[在此处输入图像描述][1]我一直在尝试使用 azure 认知“面部检测”服务。在将图像作为 url 传递时,我能够从服务获得积极响应,但是当以字节为单位转换图像后传递时,服务确实会抛出错误: { "code": "无效图像大小", "message": "图片尺寸太小。" } 我确实确保(在 Debug模式下)转换后的字节大小为 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]: /image/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}}]
关于java - 如何修复 "InvalidImageSize","message":"Image size is too small.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137882/