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

smr*_*iti 4 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})
Run Code Online (Sandbox Code Playgroud)

输出/异常:

botocore.errorfactory.InvalidImageFormatException: An error occurred(InvalidImageFormatException) when calling the DetectLabels operation: Invalid image encoding
Run Code Online (Sandbox Code Playgroud)

smr*_*iti 8

想通了,该方法实际上需要 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})
Run Code Online (Sandbox Code Playgroud)