enter image description hereI一直在尝试使用azure认知“人脸检测”服务。当将图片作为url传递时,我可以从服务获得肯定的响应,但在以字节为单位转换图像后,服务确实抛出错误:{“代码”:"InvalidImageSize",“消息”:“图像大小太小。”}我确实确保(在调试模式下)转换后的字节大小为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发布于 2019-10-02 11:46:31
为了简化测试,我只是将响应作为字符串读取。我下载您的图片并在我的代码中使用它。
我使用下面的代码获得了成功:
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}}]发布于 2021-09-02 17:46:30
我收到了同样的错误,因为我是在识别之前打开照片的。所以我删除了打开的部分,代码就可以工作了。我是用Python编程的。
https://stackoverflow.com/questions/58137882
复制相似问题