我正在尝试使用 Microsoft Face API。为此,我有 Microsoft 提供的以下代码作为示例(在本页末尾)https://dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236):
HttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
builder.setParameter("returnFaceId", "false");
builder.setParameter("returnFaceLandmarks", "false");
builder.setParameter("returnFaceAttributes", "age,gender");
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "...");
String body = Base64.encodeBase64String(img);
StringEntity reqEntity = new StringEntity(body);
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
return JsonParser.parse(EntityUtils.toString(entity));
}
} catch (URISyntaxException | IOException | ParseException e) {
System.out.println(e.getMessage());
}
return null;
但我收到以下错误:
{"error":{"code":"InvalidImage","message":"Decoding error, image format unsupported."}}
我用于测试的图像是这样的:http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg(在网上快速搜索找到的)
它尊重 Microsoft 设置的所有要求、大小和格式...如果我在网站中使用它,它就可以工作https://www.projectoxford.ai/demo/face#detection
The String body
从我的字节数组转换为base64中的字符串也可以,我在这个网站上测试它:http://codebeautify.org/base64-to-image-converter
错误消息很简单,但我看不出我错在哪里。任何人都可能知道问题出在哪里?
UPDATE
变量img
:
img = Files.readAllBytes(Paths.get(imgPath));