aws-samples / aws-samples/amazon-textract-textractor

KeyError: 'Geometry' raised when an empty cell found in `response_parser.py`

Open
#230 1 comment 0 reactions 1 assignee Claimed by @Belval View on GitHub
Dominant language
Jupyter Notebook
Stars
493
Forks
163
PR merge metrics
No merged PRs in 30d

Description

This is very similar to the #195. Since that issue has been closed, I am creating a new one:

In #195, `KeyError: 'Geometry'` was addressed by creating a condition that return None if "Geometry" not in field["ValueDetection"]. The same may happen if we cann't find the box for field["LabelDetection"]. Here is the error:

```
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
in
----> 1 textract_response.expense_documents

/opt/conda/lib/python3.7/site-packages/textractor/entities/lazy_document.py in __getattr__(self, _LazyDocument__name)
61 self._textract_client,
62 )
---> 63 self._document = parse(response)
64 if self._images is not None:
65 for i, page in enumerate(self._document.pages):

/opt/conda/lib/python3.7/site-packages/textractor/parsers/response_parser.py in parse(response)
965 return parse_analyze_id_response(response)
966 if "ExpenseDocuments" in response:
--> 967 return parser_analyze_expense_response(response)
968 else:
969 return parse_document_api_response(response)

/opt/conda/lib/python3.7/site-packages/textractor/parsers/response_parser.py in parser_analyze_expense_response(response)
923 summary_fields = []
924 for summary_field in doc["SummaryFields"]:
--> 925 summary_fields.append(create_expense_from_field(summary_field, page))
926 summary_fields[-1].raw_object = summary_field
927

/opt/conda/lib/python3.7/site-packages/textractor/parsers/response_parser.py in create_expense_from_field(field, page)
877 value_expense = Expense(
878 bbox=BoundingBox.from_normalized_dict(
--> 879 field["ValueDetection"]["Geometry"]["BoundingBox"], spatial_object=page
880 ),
881 text=field["ValueDetection"]["Text"],

KeyError: 'Geometry'
```

and it seems the same condition could be applied for field["LabelDetection"] in the case that "Geometry" does not exist. The code to fix the issue would be the following:

```
if "LabelDetection" in field:
label_expense = Expense(
None if not "Geometry" in field["LabelDetection"] else
bbox=BoundingBox.from_normalized_dict(
field["LabelDetection"]["Geometry"]["BoundingBox"], spatial_object=page
),
text=field["LabelDetection"]["Text"],
confidence=field["LabelDetection"]["Confidence"],
page=page.page_num
)
label_expense.raw_object = field["LabelDetection"]
```

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.