AWS Rekognition detect label Invalid image encoding error
Asked Answered
I

1

5

I am using boto3 to make calls to recognition's detect label method which takes an image (in form of base64-encoded bytes) as an input. However I keep getting InvalidImageFormatException and I don't see why. I have read the documentation and looked at some examples but I really can't figure out why I am receiving this error.

Below is my code and what I've tried so far

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})

Output/Exception:

botocore.errorfactory.InvalidImageFormatException: An error occurred(InvalidImageFormatException) when calling the DetectLabels operation: Invalid image encoding
Intrepid answered 22/8, 2018 at 3:56 Comment(0)
I
14

Figured it out, the method actually required base64 encoded binary data, which wasn't really specified in the docs, the doc just said base64-encoded bytes.

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})
Intrepid answered 22/8, 2018 at 14:4 Comment(3)
This doesn't seem to work for me. It throws this error: InvalidImageFormatException: An error occurred (InvalidImageFormatException) when calling the DetectText operation: Request has invalid image format. Maybe this has changed since you wrote this answer.Lotion
I get the same as @Antimony. I've been chasing my tail for an hour on this. Posts say to use the raw .read() object, to encode with cv2, then base64 encode... to encode with base64, then decode to utf-8. Numerous incantations result in a type of bytes, yet rekog rejects them.Contribution
base64_image=base64.b64encode(cf.read()); base_64_binary = base64.decodebytes(base64_image) is the same as base_64_binary=cf.read()Ikkela

© 2022 - 2024 — McMap. All rights reserved.