如何在不使用 S3 检测图像文本的情况下在本地测试来自 AWS 的 Rekognition

How to test Rekognition from AWS locally without using S3 in detecting texts from images

我正在尝试扫描图像中的文本,但在不使用 S3 存储桶的情况下无法找到源代码。这是我找到的唯一源代码,但它使用了 S3。我正在为这个项目使用 python。

https://docs.aws.amazon.com/rekognition/latest/dg/text-detecting-text-procedure.html

import boto3

if __name__ == "__main__":

bucket='bucket'
photo='text.png'

client=boto3.client('rekognition')


response=client.detect_text(Image={'S3Object':{'Bucket':bucket,'Name':photo}})

textDetections=response['TextDetections']
print ('Detected text')
for text in textDetections:
        print ('Detected text:' + text['DetectedText'])
        print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%")
        print ('Id: {}'.format(text['Id']))
        if 'ParentId' in text:
            print ('Parent Id: {}'.format(text['ParentId']))
        print ('Type:' + text['Type'])
        print

在这里找到一个 Can I use Amazon Rekognition without an S3 bucket? 和 运行 它与我需要的不同,因为它只检测标签。

Rekognition API 中的 DetectText 方法(对于 boto,detect_text)可以采用以下参数之一:

  • 对 Amazon S3 存储桶中图像的引用
  • base64 编码图像字节

因此,如果您不使用 S3 存储桶,则必须提供其 字节 docs中没有提到第三种方式。输入结构如下所示:

{
  "Image": { 
    "Bytes": blob,
    "S3Object": { 
      "Bucket": "string",
       "Name": "string",
       "Version": "string"
     }
  }
}

并且,获取非S3图片的字节流;您可以从 this answer:

复制实现
client = boto3.client('rekognition')

image_path='images/4.jpeg'
image = Image.open(image_path)

stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()

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