aws-samples / aws-samples/amazon-textract-response-parser

How to print all multi-column variable text in reading order

Open
#65 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
236
Forks
97
PR merge metrics
No merged PRs in 30d

Description

Hi
I've been trying to extract only the text in reading order for multi-column cases with the code below.
His problem is that the number of columns is manual.
I've been trying to deploy the response-parser in this code but I couldn't, could you give an example of how to do it?
What I've achieved so far with amazon-textracr-response-parser keeps mixing up the reading order.

```
# Document
s3BucketName = "your-bucket-name"
documentName = "your-image.png"

# Call Amazon Textract
response = textract.detect_document_text(
Document={
'S3Object': {
'Bucket': s3BucketName,
'Name': documentName
}
})

print(response)

# Detect columns and print lines
columns = []
lines = []
for item in response["Blocks"]:
if item["BlockType"] == "LINE":
column_found=False
for index, column in enumerate(columns):
bbox_left = item["Geometry"]["BoundingBox"]["Left"]
bbox_right = item["Geometry"]["BoundingBox"]["Left"] + item["Geometry"]["BoundingBox"]["Width"]
# Divide by the number of existing columns
# manual input that I need to resolve to sort either with 1, 2, 3, 4 or 5 columns
bbox_centre = item["Geometry"]["BoundingBox"]["Left"] + item["Geometry"]["BoundingBox"]["Width"]/2
column_centre = column['left'] + column['right']/2

if (bbox_centre > column['left'] and bbox_centre < column['right']) or (column_centre > bbox_left and column_centre < bbox_right):
#Bbox appears inside the column
lines.append([index, item["Text"]])
column_found=True
break
if not column_found:
columns.append({'left':item["Geometry"]["BoundingBox"]["Left"], 'right':item["Geometry"]["BoundingBox"]["Left"] + item["Geometry"]["BoundingBox"]["Width"]})
lines.append([len(columns)-1, item["Text"]])

lines.sort(key=lambda x: x[0])
for line in lines:
print (line[1])
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.