我在将图像发送到 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/