android - 将用于面部匹配的图像发送到 Android 上的 AWS Rekognition 时出错

标签 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是否有效且不为空

最佳答案

问题似乎出在图像格式上,我使用的是 RAW BMP 格式,即位图在内存中表示的格式。但亚马逊不支持这种格式,文档here中有说明

Amazon Rekognition supports the PNG and JPEG image formats. That is, the images you provide as input to various API operations, such as DetectLabels and IndexFaces must be in one of the supported formats.

为了解决这个问题,我已将代码更改为:

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

关于android - 将用于面部匹配的图像发送到 Android 上的 AWS Rekognition 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346851/

相关文章:

java - 来自 google 开发者页面 android 的位置示例

amazon-web-services - AWS Data Lake Dynamo 与 ElasticSearch

gradle - 使用Kotlin 1.2+版本,但仍会收到有关 `use`的编译器错误

amazon-web-services - 如何将来自不同 AWS VPC 和区域的目标组添加到 AWSALB?

kotlin - kotlin 中的 javascript 匿名对象

android - 尽管对象存在 Kotlin\Room,SELECT 查询返回 null

java - 转换为国际字符不适用于 jsonobject.tostring 但适用于字符串文字?

android - 另一个 CoordinatorLayout 中的 CoordinatorLayout

android - 如何解决新出现的action "California Consumer Privacy Act (CCPA)"?

amazon-web-services - AWS 在多个安全组中重用 IP