python - AWS Rekognition 检测标签无效图像编码错误

标签 python boto3 amazon-rekognition

我正在使用 boto3 调用识别的检测标签方法,该方法将图像(以 base64 编码字节的形式)作为输入。但是我不断收到 InvalidImageFormatException ,我不明白为什么。我已阅读文档并查看了一些示例,但我真的无法弄清楚为什么我会收到此错误。

下面是我的代码和到目前为止我尝试过的

self.rekog_client = boto3.client('rekognition', 'us-east-1')
with open('abc100.jpg', "rb") as cf:
    base64_image=base64.b64encode(cf.read()).decode("ascii")
    #also tried this) ==> base64_image=base64.b64encode(cf.read())
resp = self.rekog_client.detect_labels(Image={'Bytes': base64_image})

输出/异常:
botocore.errorfactory.InvalidImageFormatException: An error occurred(InvalidImageFormatException) when calling the DetectLabels operation: Invalid image encoding

最佳答案

想通了,该方法实际上需要base64编码二进制 文档中没有真正指定的数据,文档只是说 base64 编码的字节。

self.rekog_client = boto3.client('rekognition', 'us-east-1')
with open('cat_pic600.jpg', "rb") as cf:
        base64_image=base64.b64encode(cf.read())
        base_64_binary = base64.decodebytes(base64_image)
resp = self.rekog_client.detect_labels(Image={'Bytes': base_64_binary})

关于python - AWS Rekognition 检测标签无效图像编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959646/

相关文章:

python - 如何使用初始值预先填充 tensorflow 不规则张量

python - 迭代器 python

python - 使用 Python Boto3+DjangoRest+React Redux Saga 对 URL 进行签名以上传对象

ios - AWS Recognition 使用 IOS Swift 比较人脸

face-recognition - 是否可以使用 Google 的 Vision API 或 Amazon 的 Rekognition 获取对象的数量?

Python/Numpy - 屏蔽数组非常慢

python - 如何查找以ing结尾的单词

python - 使用 Boto3 和 IAM 角色的 Python 中的 RDS 连接

python-3.x - 如何根据文件修改日期从 s3 存储桶下载文件?

reactjs - AWS Rekognition JS SDK 无效图像编码错误