在Android上将人脸匹配图像发送到AWS Rekognition时出错

时间:2019-05-28 16:50:31

标签: android amazon-web-services kotlin amazon-rekognition

在发送要识别的图像到AWS Rekognition时出现错误。 这是我使用的代码:

        val byteBuffer = ByteBuffer.allocate(facePicture.byteCount)
        facePicture.copyPixelsToBuffer(byteBuffer)
        val image = Image().withBytes(byteBuffer)

        val searchFacesByImageResult = rekognitionClient.searchFacesByImage(
            SearchFacesByImageRequest()
                .withCollectionId(collectionId)
                .withImage(image)
                .withMaxFaces(1)
                .withFaceMatchThreshold(88F)
        )

这是一个错误:

com.amazonaws.AmazonServiceException: 1 validation error detected: Value 'java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]' at 'image.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: 70a3f05c-8166-11e9-a1cb-fbae8cf4359b)
        at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:730)
        at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:405)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
        at com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:3006)
        at com.amazonaws.services.rekognition.AmazonRekognitionClient.searchFacesByImage(AmazonRekognitionClient.java:238

异常看起来ByteBuffer为空,我已经调试并检查ByteBuffer是否有效且不为空

1 个答案:

答案 0 :(得分:1)

似乎问题出在图像格式,我使用的是RAW BMP格式,即在内存中表示位图的格式。但是Amazon here

中声明了这种格式。
  

Amazon Rekognition支持PNG和JPEG图像格式。也就是说,您提供给各种API操作(例如DetectLabels和IndexFaces)的输入的图像必须采用受支持的格式之一。

要解决此问题,我将代码更改为此:

     val stream = ByteArrayOutputStream()
     facePicture.compress(Bitmap.CompressFormat.PNG, 100, stream)
     val byteBuffer = ByteBuffer.wrap(stream.toByteArray())
     val image = Image().withBytes(byteBuffer)