Microsoft Face API中的“解码错误,图像格式不受支持”(“Decoding error, image format unsupported” in Microsoft Face API

编程入门 行业动态 更新时间:2024-10-28 00:25:16
Microsoft Face API中的“解码错误,图像格式不受支持”(“Decoding error, image format unsupported” in Microsoft Face API)

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

错误信息很简单,但我看不到我在哪里。 任何人都可能知道这个问题是什么?

UPDATE

变量img :

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

I'm trying to use Microsoft Face API. For that I have the following code that was given by Microsoft as a sample (at the end of this page 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;

but I get the following error:

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

The image that I am using for tests is this one: http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg (found it on the internet in a quick search)

It respect all the requisits set by Microsoft, size and format... If I use it in the site it works https://www.projectoxford.ai/demo/face#detection

The String body from the convertion of my array of bytes to a string in base64 is also ok, I test it in this website: http://codebeautify.org/base64-to-image-converter

The error message it's quite simple, but I fail to see where I am worng. Anyone might know whats the problem?

UPDATE

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

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

I managed to discover the problem... Instead of:

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

I needed to do:

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

I think that the Documentation is outdated...

更多推荐

本文发布于:2023-07-05 07:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不受   图像格式   错误   API   Face

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!