Where do I run this AWS rekognition code and do I

2019-09-20 09:36发布

`
import boto3

 if __name__ == "__main__":

    bucket='random_name'
    photo='b4.png'

    client=boto3.client('rekognition')


    response=client.detect_text(Image={'S3Object': 
    {'random_name':bucket,'b4.png':photo}})


    textDetections=response['TextDetections']
    print(response)
    print('Matching faces')
    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()`

This is the code recognizing images(OCR) yet I do not know where I should paste this code to run. Do I run this in Jupyter notebooks and would I need to install extra things? Do I run it in the Anaconda Prompt? I've tried both. In Jupyter, I get an error: |ParamValidationError: Parameter validation failed: Unknown parameter in Image.S3Object: "random_name", must be one of: Bucket, Name, Version Unknown parameter in Image.S3Object: "b4.png", must be one of: Bucket, Name, Version| and Anaconda prompt has much more errors. I've installed AWS already and curious whether there is more to install. It would be greatly appreciated if anyone helped me.

1条回答
干净又极端
2楼-- · 2019-09-20 10:03

Code that calls an AWS API (such as client.detect_text()) can be run from anywhere on the Internet. You have shown some Python code that can be run on a server, on a laptop, an EC2 instance or as a Lambda function (with a bit of a cleanup).

The only additional thing it needs is a set of credentials so that it can connect to your AWS account.

  • If you are running the code on an Amazon EC2 instance or as a lambda function, simply assign the instance/function an appropriate IAM Role and the code will automatically receive credentials.
  • If you are running the code on your own computer, first run the aws configure command and provide your IAM User credentials.

Also, please note that the format of detect_text() is:

response = client.detect_text(
    Image={
        'Bytes': b'bytes',
        'S3Object': {
            'Bucket': 'string',
            'Name': 'string',
            'Version': 'string'
        }
    }
)

Therefore, your line of code should be:

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

Also, I wouldn't say that this function is true OCR. Rather, it finds bits of text in a picture, such as words on a sign. It would not be suitable for reading a page full of text, which is done by traditional OCR methods.

查看更多
登录 后发表回答