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
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With