0

如果我的问题措辞不当,我深表歉意,我不经常在这里发帖。

我在我的 Android 应用程序上使用 Amazon Web Services 启动我的请求以匹配 2 张不同图像中的 2 张面孔时遇到困难。我的代码提供如下:

    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

我得到的错误是:

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)

我的代码中发生的事情是我正在转换从我在 android 上的文件路径获取的图像(已确认是正确的)并将其转换为 ByteBuffer,以便我可以将 AWS 使用 withBytes 创建的图像对象传递给它。我对另一个 ByteBuffer 做了同样的事情,只是我将 BitMap 转换为 ByteBuffer(代码中没有显示)。通过调试,我记录并发现两个 ByteBuffers 都是非空的并且功能齐全。我也已经尝试在 CompareFacesRequest 构造函数中传递图像,而不是使用 withSource 和 withTarget 图像方法。我还尝试在两个 Image 对象上调用 getBytes 以查看 ByteBuffers 是否真的通过了。

该错误提示我正在通过请求传递 2 个空的 Image 对象,因此它表示必须有一个或多个字节才能传递 Image 对象。但我不确定情况是否如此。我一生都无法弄清楚为什么会发生这种情况,它似乎在其他任何地方都有效。如果有人可以帮助我并确定答案,我将非常感激?

非常感谢,德鲁夫

4

1 回答 1

1

当您在帖子中提到您将位图转换为字节缓冲区时,源图像是“.bmp”文件吗?您也可以共享该代码以帮助调试您的问题吗?

请注意,Rekognition 仅支持 PNG 和 JPEG 文件格式(请参阅https://docs.aws.amazon.com/rekognition/latest/dg/limits.html)。

于 2018-03-24T05:14:08.440 回答