5

Relevant Code:

import boto3
from PIL import Image
import base64

client = boto3.client('rekognition')

filename = r'C:\Users\H-63\Pictures\scantests\Rekognition test.JPG'

with open(filename, 'rb') as image_file:
    image = image_file.read()

image = base64.b64encode(image).decode('UTF-8')

response = client.detect_text(
    Image={'Bytes': image
        })

However, When I run this, I get an error:

An error occurred (InvalidImageFormatException) when calling the DetectText operation: Request has Invalid image format

How do I get my image to be the right format for detect_text? The documentation says it has to be base64 encoding.

4

2 回答 2

7

我不确定为什么文档甚至提到了 base64,但该函数需要字节。所以只需使用:

with open(filename, 'rb') as image_file:
  image = image_file.read()
  client.detect_text(Image={'Bytes': image})
于 2017-11-28T23:45:14.020 回答
3

您可以只使用字节返回 read() https://docs.aws.amazon.com/rekognition/latest/dg/images-bytes.html

#Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import boto3

if __name__ == "__main__":

 imageFile='input.jpg'
 client=boto3.client('rekognition')

 with open(imageFile, 'rb') as image:
     response = client.detect_labels(Image={'Bytes': image.read()})

 print('Detected labels in ' + imageFile)    
 for label in response['Labels']:
     print (label['Name'] + ' : ' + str(label['Confidence']))

 print('Done...')


[1]: https://docs.aws.amazon.com/rekognition/latest/dg/images-bytes.html
于 2019-02-03T15:19:00.050 回答