I tried text extraction from an image with AWS Textract
Generate an access key in IAM from the AWS console
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
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
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 |
---|---|
Thank you for your hard work.
Recommended Posts