Dhruv Khurana
Dhruv Khurana

Reputation: 27

Error on CompareFacesRequest as part of AWS's Rekognize on Android

I apologize if my question is not worded properly, I don't post here very often.

I am having difficulty with launching my request to match 2 faces in 2 different images using Amazon Web Services on my Android application. My code is provided below:

    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(), // Context
                "xxxxxxx", // Identity Pool ID
                Regions.US_EAST_1 // Region
        );

    AmazonRekognitionClient client = new AmazonRekognitionClient(credentialsProvider);

    ByteBuffer buffer = ByteBuffer.allocate(croppedBitmap.getByteCount()); //Create a new buffer
        croppedBitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

try {
    //get first image from phone
    File dhruv = new File("/sdcard/temp/dhruv.jpg");
    InputStream inputStream = new 
    FileInputStream(dhruv.getAbsolutePath().toString());

    //convert to ByteBuffer
    ByteBuffer byteBuffer = 
    ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
    Log.d("lol", Arrays.toString(byteBuffer.array()));

    Image image = new Image();
    Image image2 = new Image();
    image.withBytes(buffer);
    image2.withBytes(byteBuffer);

    CompareFacesRequest compare = new CompareFacesRequest();
    compare.withSourceImage(image);
    compare.withTargetImage(image2);

    CompareFacesResult result = client.compareFaces(compare);
    result.getFaceMatches();
} catch(...) {} // catched the error

The error I am getting is this:

3247/com.busradeniz.detection D/lol: 1 validation error detected: Value 
'java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]' at 'sourceImage.bytes' failed 
to satisfy constraint: Member must have length greater than or equal to 1 
(Service: AmazonRekognition; Status Code: 400; Error Code: 
ValidationException; Request ID: a0489079-2c17-11e8-b8b8-23c9eaea153d)

What is happening in my code is I am converting an image taken from my file path on the android (which is confirmed to be correct) and converting it to a ByteBuffer so that I can pass it Image object created by AWS using withBytes. I did the same for another ByteBuffer, except I converted a BitMap to a ByteBuffer instead (this is not shown in the code). Through debugging I have logged and found that both ByteBuffers are non-empty and fully functional. I have also already tried passing the images in the CompareFacesRequest constructor instead of using the withSource and withTarget image methods. I have also tried calling getBytes on both the Image objects to see if the ByteBuffers really did pass through.

The error hints at me that I am passing 2 empty Image objects with the request, hence it says that there has to be one or more bytes to pass in the Image objects. But I am not sure this is the case. I can't for the life of me figure out why this is happening, it seems to work everywhere else. I would really really appreciate if someone could help me and determine an answer??

Thanks so much, Dhruv

Upvotes: 0

Views: 1144

Answers (1)

Bommas
Bommas

Reputation: 238

When you mention in your post that you converted a bitmap to a byte buffer, was the source image a ".bmp" file? Can you share that code as well, to help debug your issue?

Please note that Rekognition supports only PNG and JPEG file formats ( see https://docs.aws.amazon.com/rekognition/latest/dg/limits.html).

Upvotes: 1

Related Questions