Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Rekognition detect label Invalid image encoding error

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
like image 964
smriti Avatar asked Aug 22 '18 03:08

smriti


1 Answers

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})
like image 123
smriti Avatar answered Oct 26 '22 09:10

smriti