java - Microsoft Face API 中的 "Decoding error, image format unsupported"

标签 java api face

我正在尝试使用 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

我的字节数组转成base64字符串的String body也是可以的,我在这个网站测试过:http://codebeautify.org/base64-to-image-converter

错误信息很简单,但我看不出我错在哪里。任何人都可能知道问题出在哪里?

更新

变量img:

img = Files.readAllBytes(Paths.get(imgPath));

最佳答案

我设法发现了问题...而不是:

String body = Base64.encodeBase64String(img);
StringEntity reqEntity = new StringEntity(body);
request.setEntity(reqEntity);

我需要做的:

ByteArrayEntity reqEntity = new ByteArrayEntity(img, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);

我认为文档已过时...

关于java - Microsoft Face API 中的 "Decoding error, image format unsupported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120746/

相关文章:

api - "reasonable"使用web API同步数据

api - postman 为 POST 请求解析 "Invalid CORS request"

javascript - THREE.js 从 raycaster 交点检测相邻面

java - Libgdx-在指针后面画一条线

java - Camel Wire Tap - 用于接受两个输入

java - 项目树更改后的 Spring UnsatisfiedDependencyException

java - 重复使用while循环的问题

javascript - Linkedin People Search Javascript API 未返回任何结果

c++ - OpenCV 人脸检测 ROI 断言失败

java - FaceDetector 无法解析为类型