Easily parse JSON returned by Amazon Textract.
Project description
Textract Response Parser
You can use Textract response parser library to easily parser JSON returned by Amazon Textract. Library parses JSON and provides programming language specific constructs to work with different parts of the document. textractor is an example of PoC batch processing tool that takes advantage of Textract response parser library and generate output in multiple formats.
Installation
python -m pip install amazon-textract-response-parser
Pipeline and Serializer/Deserializer
Serializer/Deserializer
Based on the marshmallow framework, the serializer/deserializer allows for creating an object represenation of the Textract JSON response.
Deserialize Textract JSON
# j holds the Textract JSON
from trp.trp2 import TDocument, TDocumentSchema
t_doc = TDocumentSchema().load(json.loads(j))
Serialize Textract
from trp.trp2 import TDocument, TDocumentSchema
t_doc = TDocumentSchema().dump(t_doc)
Pipeline order blocks
By default Textract does not put the elements identified in an order in the JSON response.
The sample implementation order_blocks_by_geo
of a function using the Serializer/Deserializer shows how to change the structure and order the elements while maintaining the schema. This way no change is necessary to integrate with existing processing.
# the sample code below makes use of the amazon-textract-caller
python -m pip install amazon-textract-caller
from textractcaller.t_call import call_textract, Textract_Features
from trp.trp2 import TDocument, TDocumentSchema
from trp.t_pipeline import order_blocks_by_geo
import trp
import json
j = call_textract(input_document="path_to_some_document (PDF, JPEG, PNG)", features=[Textract_Features.FORMS, Textract_Features.TABLES])
# the t_doc will be not ordered
t_doc = TDocumentSchema().load(json.loads(j))
# the ordered_doc has elements ordered by y-coordinate (top to bottom of page)
ordered_doc = order_blocks_by_geo(t_doc)
# send to trp for further processing logic
trp_doc = trp.Document(TDocumentSchema().dump(ordered_doc))
Python Usage
# the sample code below makes use of the amazon-textract-caller
python -m pip install amazon-textract-caller
from textractcaller.t_call import call_textract, Textract_Features
Parse JSON response from Textract
from trp import Document doc = Document(response)
Iterate over elements in the document
for page in doc.pages: # Print lines and words for line in page.lines: print("Line: {}--{}".format(line.text, line.confidence)) for word in line.words: print("Word: {}--{}".format(word.text, word.confidence))
# Print tables
for table in page.tables:
for r, row in enumerate(table.rows):
for c, cell in enumerate(row.cells):
print("Table[{}][{}] = {}-{}".format(r, c, cell.text, cell.confidence))
# Print fields
for field in page.form.fields:
print("Field: Key: {}, Value: {}".format(field.key.text, field.value.text))
# Get field by key
key = "Phone Number:"
field = page.form.getFieldByKey(key)
if(field):
print("Field: Key: {}, Value: {}".format(field.key, field.value))
# Search fields by key
key = "address"
fields = page.form.searchFieldsByKey(key)
for field in fields:
print("Field: Key: {}, Value: {}".format(field.key, field.value))
## Test
- Clone the repo and run pytest
python -m pip install pytest git clone https://github.com/aws-samples/amazon-textract-response-parser.git cd amazon-textract-response-parser pytest
## Other Resources
- [Large scale document processing with Amazon Textract - Reference Architecture](https://github.com/aws-samples/amazon-textract-serverless-large-scale-document-processing)
- [Batch processing tool](https://github.com/aws-samples/amazon-textract-textractor)
- [Code samples](https://github.com/aws-samples/amazon-textract-code-samples)
## License Summary
This sample code is made available under the Apache License Version 2.0. See the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for amazon-textract-response-parser-0.1.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5142f3b981e4d9406d2a3ef7c3884a03ced979f4c9243106aa1008f8b5b7e45 |
|
MD5 | 21c07d4f719342e71ce04a7ebf3e416d |
|
BLAKE2b-256 | 6497850198da9150a3fec38fb16f92624622eed04baa8e07f6ee4785f2face57 |
Hashes for amazon_textract_response_parser-0.1.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 452dda8825979b1533b1f21afb2c569efb5b502ebf64277d72eb66518be5c4e4 |
|
MD5 | ba4237e773cd86bc4ad060e95b4e38e8 |
|
BLAKE2b-256 | e175717ed305014cd2304f93cf98d306e0e9fae948276c0e3bbd637ca3aeb6c5 |