Project Oxford Emotion - Invalid face image

Category: project oxford

Question

iamsuperjesus on Sun, 14 Feb 2016 14:02:24


I am currently struggeling to get a local image to upload properly, i get this error for an image that the demo handles fine.

{"error":{"code":"BadBody","message":"Invalid face image."}}

here is the link to the image: http://all4desktop.com/data_images/original/4242435-face.jpg

And my code (Java):

URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/emotion/v1.0/recognize");

URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream"); //octet-stream / json
request.setHeader("Ocp-Apim-Subscription-Key", myKey)


ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    RenderedImage image = ImageIO.read(new File("C:\\face.jpg"));
    ImageIO.write(image,"jpg", baos);
} catch (IOException e) {
    e.printStackTrace();
}


StringEntity reqEntity = new StringEntity("data:image/png;base64," + DatatypeConverter.printBase64Binary(baos.toByteArray()));
request.setEntity(reqEntity);




HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();





Replies

cthrash99 on Thu, 18 Feb 2016 16:21:08


The POST body needs to be the raw image binary, whereas you've created a Data URI.  You'll want to omit the "data:..." prefix, and also skip the base-64 encoding.  My best guess is that you'll want to create a ByteArrayEntity instead of a StringEntity.

gat890 on Mon, 18 Jul 2016 19:22:23


Hello, im using a similar method with your recommendation but i have not a solution:

BufferedImage imagen = webcam.getImage();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                ImageIO.write(imagen, "jpg", baos);
            } catch (IOException ex) {
                Logger.getLogger(WebcamViewerExample.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            byte[] img = baos.toByteArray();
            ByteArrayEntity reqEntity2 = new ByteArrayEntity(img);
            System.out.println(reqEntity2.toString());
            HttpClient cliente = HttpClients.createDefault();
            
            try{
                URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/emotion/v1.0/recognize");
                
                URI uri = builder.build();
                HttpPost request = new HttpPost(uri);
                request.setHeader("Content-Type", "application/json");
                request.setHeader("Ocp-Apim-Subscription-Key", "***************480a7c176b27ed78f");
                
                //StringEntity reqEntity = new StringEntity(reqEntity2);
                request.setEntity(reqEntity2);
                
                HttpResponse response = cliente.execute(request);
                HttpEntity entity = response.getEntity();
                if (entity != null){
                    String temp = EntityUtils.toString(entity);
                    System.out.println(temp);
                    }
                }
            }
            catch (Exception es){
                System.out.println(es.getMessage());
            }


cthrash99 on Tue, 19 Jul 2016 14:48:22


The binary payload must be matched with
request.setHeader("Content-Type", "application/octet-stream");