Text extraction with AWS Textract (Python3.6)

Introduction

I tried text extraction from an image with AWS Textract

Development environment

Introduction

    1. install awscli AWSCLIV2. Refer to Installing, updating, and uninstalling AWS CLI version 2 on Windows. Install msi
  1. Generate an access key in IAM from the AWS console image.png

    1. Set awscli at the command prompt

Enter the Access Key and Secret Access Key obtained when the access key was generated. For the default region name, specify ap-south-1 (Asia Pacific Mumbai) where Textract can be used. The output format is json.

$ aws configure 
AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: XXXX
Default region name [None]: ap-south-1
Default output format [None]: json

-Regions and Zones -Configuration and Credential File Settings

    1. Add the policy "AmazonTextractFullAccess" from Add IAM Permissions image.png

Four. Open the anaconda prompt and create a Python 3.6 environment.

$ conda create -n py36 python=3.6
$ conda activate py36

Five. Install the library

$ pip install boto3
$ pip install opencv-python
  1. Let's run the code below
import boto3
import cv2
import os.path

def process_text_analysis(image):
    client = boto3.client('textract')
    image_data = cv2.imencode(".png ", image)[1].tostring()
    response = client.analyze_document(Document={'Bytes': image_data}, FeatureTypes=["TABLES", "FORMS"])
    blocks = response['Blocks']
    return blocks

def draw_blocks(image, blocks):
    height, width = image.shape[:2]
    draw = image.copy()
    for block in blocks:
        if block['BlockType'] == "WORD":
            vertices = [(int(width * block['Geometry']['Polygon'][i]['X']), int(height * block['Geometry']['Polygon'][i]['Y'])) for i in range(len(block['Geometry']['Polygon']))]
            cv2.putText(draw, block['Text'], vertices[0], cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA)
            cv2.rectangle(draw, vertices[0], vertices[2], (0, 255, 0))
    return draw

filename = "338px-Atomist_quote_from_Democritus.png "
image = cv2.imread(filename, cv2.IMREAD_COLOR)

blocks = process_text_analysis(image)
print("Blocks detected: " + str(len(blocks)))

draw = draw_blocks(image, blocks)
cv2.imshow("draw", draw)
cv2.waitKey(0)
input output
338px-Atomist_quote_from_Democritus.png awsocr.png

Thank you for your hard work.

Recommended Posts

Text extraction with AWS Textract (Python3.6)
AWS CDK with Python
Text extraction with GCP Cloud Vision API (Python3.6)
Text extraction (Read API) with Azure Computer Vision API (Python3.6)
Text mining with Python ① Morphological analysis
Enable Python raw_input with Sublime Text 3
Speak Japanese text with OpenJTalk + python
Notify HipChat with AWS Lambda (Python)
[AWS] Using ini files with Lambda [Python]
Easy keyword extraction with TermExtract for Python
English speech recognition with python [speech to text]
I want to play with aws with python
Connect to s3 with AWS Lambda Python
Text mining with Python ② Visualization with Word Cloud
Read text in images with python OCR
Touch AWS with Serverless Framework and Python
Python + Selenium + Headless Chromium with aws lambda
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Extract text from PowerPoint with Python! (Compatible with tables)
Create a python3 build environment with Sublime Text3
LINE BOT with Python + AWS Lambda + API Gateway
Image processing from scratch with python (4) Contour extraction
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Manage AWS nicely with the Python library Boto
Getting started with AWS IoT easily in Python
Wav file generation from numeric text with python
Serial communication with Python
Clustering text in Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Text mining with Python ① Morphological analysis (re: Linux version)
Try scraping with Python.
Dynamic HTML pages made with AWS Lambda and Python
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1