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

时间:2019-02-13 14:24:17

标签: python amazon-web-services amazon-s3 amazon-rekognition

我正在尝试从图像中扫描文本,但是如果不使用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?处找到了一个,并且运行它与我需要的不同,因为它仅检测标签。

1 个答案:

答案 0 :(得分:1)

Rekognition API中的<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" /> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script> <body> <div class="container-fluid"> <div class="row"> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> </div> <div class="row"> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> <div class="col" style="margin: 0; padding: 0;"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1734469/windows_wallpaper.png" class="projectimg" alt="" style="width: 70%"> </div> </div> </div> </body>方法(对于boto,DetectText)可以采用以下参数之一:

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

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

detect_text

并且,获取非S3图像的字节流;您可以从this answer复制实施:

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