【问题标题】:"Decoding error, image format unsupported" in Microsoft Face APIMicrosoft Face API 中的“解码错误,图像格式不受支持”
【发布时间】:2016-07-07 08:30:12
【问题描述】:

我正在尝试使用 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 (在互联网上快速搜索找到它)

它尊重微软设定的所有要求、大小和格式...如果我在网站上使用它,它可以工作https://www.projectoxford.ai/demo/face#detection

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

错误信息很简单,但我看不到我穿在哪里。有谁知道问题出在哪里?

更新

变量img

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

【问题讨论】:

  • 您正在编码的img。一切都好吗?我看到您正在使用 Apache Commons Base64。仅供参考,Java 1.8 有 [Base64]。
  • 不知道,我把代码改成使用Java Base64,我也遇到了同样的问题。
  • 我在问你变量img。您没有提供任何相关信息。
  • 如果我 Base64 解码一个不正确的字符串并发送它,它显然是行不通的。这就是我所关心的。您是否正确初始化img
  • 是的,我的错,我更新了我的帖子。

标签: java api face


【解决方案1】:
import okhttp3.*;

import java.io.File;
import java.io.IOException;


public class Main {


    public static void main(String[] args) {
        try {
            doRequest();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void doRequest() throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"),
                new File(".//src//main//java//Archivo_001.png"));

        Request request = new Request.Builder()
                .url("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise")
                .post(body)
                .addHeader("Ocp-Apim-Subscription-Key", "1d88f949af3443ea8cc16b7146bd7501")
                .addHeader("Content-Type", "application/json")
                .addHeader("cache-control", "no-cache")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());

    }


}

【讨论】:

    【解决方案2】:

    您可以查看CognitiveJ,这是一个开源库,将处理与 MS Face API 的通信和交互。如果您不想使用该库,那么您可以查看代码以了解 REST API 的预期。

    (披露 - 我是图书馆的作者)。

    【讨论】:

      【解决方案3】:

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

      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);
      

      我认为文档已经过时...

      【讨论】:

        【解决方案4】:

        我做了以下更改。我没有发送编码图像,而是发送图像的 URL。

        request.setHeader("Content-Type", "application/json");
        request.setHeader("Ocp-Apim-Subscription-Key", "{YOUR_FACES_API_KEY}");
        
        StringEntity reqEntity = new StringEntity("{ \"url\":\"http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg\" }");
        request.setEntity(reqEntity);
        

        这会得到响应:

        [{"faceRectangle":{"top":878,"left":2718,"width":312,"height":312},"faceAttributes":{"gender":"male","age":28.5}},{"faceRectangle":{"top":593,"left":573,"width":310,"height":310},"faceAttributes":{"gender":"male","age":27.5}},{"faceRectangle":{"top":1122,"left":1014,"width":294,"height":294},"faceAttributes":{"gender":"female","age":27.7}},{"faceRectangle":{"top":915,"left":1773,"width":277,"height":277},"faceAttributes":{"gender":"female","age":36.7}},{"faceRectangle":{"top":566,"left":1276,"width":269,"height":269},"faceAttributes":{"gender":"male","age":40.7}},{"faceRectangle":{"top":677,"left":2134,"width":257,"height":257},"faceAttributes":{"gender":"female","age":35.2}}]
        

        将尽快发送编码图像。将相应地更新此帖子。

        编辑:

        从 URL 下载图片

        String base64Img = null;
        byte[] bytes = null;
        String imgBinaryString = null;
        String base64ImgBinaryString = null;
        try {
            URL url = new URL("http://www.businessstudynotes.com/wp-content/uploads/2015/09/Role-of-Group.jpg");
            //"http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg");
            BufferedImage image = ImageIO.read(url);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);
            bytes = baos.toByteArray();
            StringBuilder sb = new StringBuilder();
            for (byte by: bytes)
                sb.append(Integer.toBinaryString(by & 0xFF));
            imgBinaryString = sb.toString();
        
            base64Img = Base64.getEncoder().encodeToString(bytes);
            byte[] base64Bytes = base64Img.getBytes("UTF-8");
            sb = new StringBuilder();
            for (byte by: base64Bytes) {
                sb.append(Integer.toBinaryString(by & 0xFF));
            }
            base64ImgBinaryString = sb.toString();
        
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            System.out.println("Download issue");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("ImageIO issue");
            e.printStackTrace();
        }
        

        imgBinaryString 包含图像的二进制表示; base64ImgBinaryString 包含图像的 Base 64 表示的二进制表示。

        要上传这张图片...

        URI uri = builder.build(); // builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/octet-stream");
        request.setHeader("Ocp-Apim-Subscription-Key", "{YOUR_FACES_API_KEY");
        
        StringEntity reqEntity = new StringEntity(base64ImgBinaryString);
        request.setEntity(reqEntity);
        
        HttpResponse response = httpclient.execute(request);
        

        StringEntity 设置为imgBinaryStringbase64ImgBinaryString 会产生相同的响应...

        {"error":{"code":"InvalidImage","message":"Decoding error, image format unsupported."}}

        现在,好东西。这行得通...

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

        其中bytes 是图像的字节数组;但是对此的 Base64 表示不起作用。有人真的需要更新文档。

        【讨论】:

        • 是的,url 工作正常,问题出在编码图像上。这是我第一次使用编码图像,所以在尝试了一段时间后,我决定寻求帮助。等待你的更新,ty。
        • 我一直在尝试通过这个工具使用二进制数据:dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/… 但我遇到了同样的问题。
        • @lulas 是的!令人惊讶的是,没有任何工作,规范本身并不清楚他们如何接受二进制数据的格式。我已经问过我在微软认识的人来找出有关这个问题的一些信息。让我们看看情况如何。
        • @lulas 工作。最后。 :)
        • @lulas 我刚刚注意到你也添加了一个答案哈哈
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        • 2016-07-28
        • 1970-01-01
        • 2017-07-14
        • 2013-09-28
        • 2017-04-03
        • 1970-01-01
        相关资源
        最近更新 更多