forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[formrecognizer] adds samples for labeled tables and addresses sample…
…/doc feedback (Azure#18409) * add snippet about where to find data in RecognizedForm docstring * add tables output to the recognize custom form samples * create labeled tables samples and add training docs * catalina's feedback * mari's feedback
- Loading branch information
1 parent
2cae721
commit 70406b4
Showing
41 changed files
with
14,737 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...-formrecognizer/samples/async_samples/sample_differentiate_output_labeled_tables_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# coding: utf-8 | ||
|
||
# ------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
|
||
""" | ||
FILE: sample_differentiate_output_labeled_tables_async.py | ||
DESCRIPTION: | ||
This sample demonstrates the differences in output that arise when begin_recognize_custom_forms | ||
is called with custom models trained with fixed vs. dynamic table tags. | ||
The models used in this sample can be created in the sample_train_model_with_labels.py using the | ||
training files in | ||
https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_forms/labeled_tables | ||
Note that Form Recognizer automatically finds and extracts all tables in your documents whether the tables | ||
are tagged/labeled or not. Tables extracted automatically by Form Recognizer will be included in the | ||
`tables` property under `RecognizedForm.pages`. | ||
A conceptual explanation of using table tags to train your custom form model can be found in the | ||
service documentation: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/supervised-table-tags | ||
USAGE: | ||
python sample_differentiate_output_labeled_tables_async.py | ||
Set the environment variables with your own values before running the sample: | ||
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. | ||
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key | ||
3) MODEL_ID_FIXED_ROW_TABLES - the ID of your custom model trained with labels on fixed row tables | ||
4) MODEL_ID_DYNAMIC_ROW_TABLES - the ID of your custom model trained with labels on dynamic row tables | ||
""" | ||
|
||
import os | ||
import asyncio | ||
|
||
|
||
class TestDifferentiateOutputLabeledTablesAsync(object): | ||
|
||
async def test_recognize_tables_fixed_rows_async(self): | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.formrecognizer.aio import FormRecognizerClient | ||
|
||
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
model_id_fixed_rows_table = os.environ["MODEL_ID_FIXED_ROW_TABLES"] | ||
|
||
form_recognizer_client = FormRecognizerClient( | ||
endpoint=endpoint, credential=AzureKeyCredential(key) | ||
) | ||
|
||
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), | ||
"..", "..", "./sample_forms/forms/label_table_fixed_rows1.pdf")) | ||
with open(path_to_sample_forms, "rb") as f: | ||
form = f.read() | ||
|
||
async with form_recognizer_client: | ||
poller = await form_recognizer_client.begin_recognize_custom_forms( | ||
model_id=model_id_fixed_rows_table, form=form | ||
) | ||
|
||
result = await poller.result() | ||
|
||
print("\n--------Recognizing labeled table with fixed rows--------\n") | ||
for form in result: | ||
for name, field in form.fields.items(): | ||
# substitute "table" for the label given to the table tag during training | ||
# (if different than sample training docs) | ||
if name == "table": | ||
for row_name, column in field.value.items(): | ||
print("Row '{}' has columns:".format(row_name)) | ||
for column_name, column_value in column.value.items(): | ||
print("...Column '{}' with value '{}' and a confidence score of {}".format( | ||
column_name, column_value.value, column_value.confidence | ||
)) | ||
else: # non-table tagged FormField | ||
print("...Field '{}' has value '{}' with a confidence score of {}".format( | ||
name, | ||
field.value, | ||
field.confidence | ||
)) | ||
|
||
async def test_recognize_tables_dynamic_rows_async(self): | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.formrecognizer.aio import FormRecognizerClient | ||
|
||
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
model_id_dynamic_rows_table = os.environ["MODEL_ID_DYNAMIC_ROW_TABLES"] | ||
|
||
form_recognizer_client = FormRecognizerClient( | ||
endpoint=endpoint, credential=AzureKeyCredential(key) | ||
) | ||
|
||
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), | ||
"..", "..", "./sample_forms/forms/label_table_dynamic_rows1.pdf")) | ||
with open(path_to_sample_forms, "rb") as f: | ||
form = f.read() | ||
|
||
async with form_recognizer_client: | ||
poller = await form_recognizer_client.begin_recognize_custom_forms( | ||
model_id=model_id_dynamic_rows_table, form=form | ||
) | ||
|
||
result = await poller.result() | ||
|
||
print("\n\n--------Recognizing labeled table with dynamic rows--------\n") | ||
for form in result: | ||
for name, field in form.fields.items(): | ||
# substitute "table" for the label given to the table tag during training | ||
# (if different than sample training docs) | ||
if name == "table": | ||
for idx, row in enumerate(field.value): | ||
print("Row {}".format(idx+1)) | ||
for column_name, row_value in row.value.items(): | ||
print("...Column '{}' with value '{}' and a confidence score of {}".format( | ||
column_name, row_value.value, row_value.confidence | ||
)) | ||
else: # non-table tagged FormField | ||
print("...Field '{}' has value '{}' with a confidence score of {}".format( | ||
name, | ||
field.value, | ||
field.confidence | ||
)) | ||
|
||
|
||
async def main(): | ||
sample = TestDifferentiateOutputLabeledTablesAsync() | ||
await sample.test_recognize_tables_fixed_rows_async() | ||
await sample.test_recognize_tables_dynamic_rows_async() | ||
|
||
|
||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...mrecognizer/azure-ai-formrecognizer/samples/sample_differentiate_output_labeled_tables.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# coding: utf-8 | ||
|
||
# ------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
|
||
""" | ||
FILE: sample_differentiate_output_labeled_tables.py | ||
DESCRIPTION: | ||
This sample demonstrates the differences in output that arise when begin_recognize_custom_forms | ||
is called with custom models trained with fixed vs. dynamic table tags. | ||
The models used in this sample can be created in the sample_train_model_with_labels.py using the | ||
training files in | ||
https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_forms/labeled_tables | ||
Note that Form Recognizer automatically finds and extracts all tables in your documents whether the tables | ||
are tagged/labeled or not. Tables extracted automatically by Form Recognizer will be included in the | ||
`tables` property under `RecognizedForm.pages`. | ||
A conceptual explanation of using table tags to train your custom form model can be found in the | ||
service documentation: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/supervised-table-tags | ||
USAGE: | ||
python sample_differentiate_output_labeled_tables.py | ||
Set the environment variables with your own values before running the sample: | ||
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. | ||
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key | ||
3) MODEL_ID_FIXED_ROW_TABLES - the ID of your custom model trained with labels on fixed row tables | ||
4) MODEL_ID_DYNAMIC_ROW_TABLES - the ID of your custom model trained with labels on dynamic row tables | ||
""" | ||
|
||
import os | ||
|
||
|
||
class TestDifferentiateOutputLabeledTables(object): | ||
|
||
def test_recognize_tables_fixed_rows(self): | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.formrecognizer import FormRecognizerClient | ||
|
||
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
model_id_fixed_rows_table = os.environ["MODEL_ID_FIXED_ROW_TABLES"] | ||
|
||
form_recognizer_client = FormRecognizerClient( | ||
endpoint=endpoint, credential=AzureKeyCredential(key) | ||
) | ||
|
||
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), | ||
"..", "./sample_forms/forms/label_table_fixed_rows1.pdf")) | ||
|
||
with open(path_to_sample_forms, "rb") as f: | ||
form = f.read() | ||
poller = form_recognizer_client.begin_recognize_custom_forms( | ||
model_id=model_id_fixed_rows_table, form=form | ||
) | ||
|
||
result = poller.result() | ||
|
||
print("\n--------Recognizing labeled table with fixed rows--------\n") | ||
for form in result: | ||
for name, field in form.fields.items(): | ||
# substitute "table" for the label given to the table tag during training | ||
# (if different than sample training docs) | ||
if name == "table": | ||
for row_name, column in field.value.items(): | ||
print("Row '{}' has columns:".format(row_name)) | ||
for column_name, column_value in column.value.items(): | ||
print("...Column '{}' with value '{}' and a confidence score of {}".format( | ||
column_name, column_value.value, column_value.confidence | ||
)) | ||
else: # non-table tagged FormField | ||
print("...Field '{}' has value '{}' with a confidence score of {}".format( | ||
name, | ||
field.value, | ||
field.confidence | ||
)) | ||
|
||
def test_recognize_tables_dynamic_rows(self): | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.formrecognizer import FormRecognizerClient | ||
|
||
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] | ||
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] | ||
model_id_dynamic_rows_table = os.environ["MODEL_ID_DYNAMIC_ROW_TABLES"] | ||
|
||
form_recognizer_client = FormRecognizerClient( | ||
endpoint=endpoint, credential=AzureKeyCredential(key) | ||
) | ||
|
||
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), | ||
"..", "./sample_forms/forms/label_table_dynamic_rows1.pdf")) | ||
|
||
with open(path_to_sample_forms, "rb") as f: | ||
form = f.read() | ||
poller = form_recognizer_client.begin_recognize_custom_forms( | ||
model_id=model_id_dynamic_rows_table, form=form | ||
) | ||
|
||
result = poller.result() | ||
|
||
print("\n\n--------Recognizing labeled table with dynamic rows--------\n") | ||
for form in result: | ||
for name, field in form.fields.items(): | ||
# substitute "table" for the label given to the table tag during training | ||
# (if different than sample training docs) | ||
if name == "table": | ||
for idx, row in enumerate(field.value): | ||
print("Row {}".format(idx+1)) | ||
for column_name, row_value in row.value.items(): | ||
print("...Column '{}' with value '{}' and a confidence score of {}".format( | ||
column_name, row_value.value, row_value.confidence | ||
)) | ||
else: # non-table tagged FormField | ||
print("...Field '{}' has value '{}' with a confidence score of {}".format( | ||
name, | ||
field.value, | ||
field.confidence | ||
)) | ||
|
||
|
||
if __name__ == '__main__': | ||
sample = TestDifferentiateOutputLabeledTables() | ||
sample.test_recognize_tables_fixed_rows() | ||
sample.test_recognize_tables_dynamic_rows() |
Binary file added
BIN
+69.4 KB
...cognizer/azure-ai-formrecognizer/samples/sample_forms/forms/label_table_dynamic_rows1.pdf
Binary file not shown.
Binary file added
BIN
+71.8 KB
...recognizer/azure-ai-formrecognizer/samples/sample_forms/forms/label_table_fixed_rows1.pdf
Binary file not shown.
50 changes: 50 additions & 0 deletions
50
...ecognizer/azure-ai-formrecognizer/samples/sample_forms/labeled_tables/dynamic/fields.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"$schema": "https://schema.cognitiveservices.azure.com/formrecognizer/2021-03-01/fields.json", | ||
"fields": [ | ||
{ | ||
"fieldKey": "table", | ||
"fieldType": "array", | ||
"fieldFormat": "not-specified", | ||
"itemType": "table_object", | ||
"fields": null | ||
} | ||
], | ||
"definitions": { | ||
"table_object": { | ||
"fieldKey": "table_object", | ||
"fieldType": "object", | ||
"fieldFormat": "not-specified", | ||
"itemType": null, | ||
"fields": [ | ||
{ | ||
"fieldKey": "Item", | ||
"fieldType": "string", | ||
"fieldFormat": "not-specified", | ||
"itemType": null, | ||
"fields": null | ||
}, | ||
{ | ||
"fieldKey": "Price", | ||
"fieldType": "string", | ||
"fieldFormat": "not-specified", | ||
"itemType": null, | ||
"fields": null | ||
}, | ||
{ | ||
"fieldKey": "Tax", | ||
"fieldType": "string", | ||
"fieldFormat": "not-specified", | ||
"itemType": null, | ||
"fields": null | ||
}, | ||
{ | ||
"fieldKey": "Total", | ||
"fieldType": "string", | ||
"fieldFormat": "not-specified", | ||
"itemType": null, | ||
"fields": null | ||
} | ||
] | ||
} | ||
} | ||
} |
Binary file added
BIN
+69.4 KB
...-formrecognizer/samples/sample_forms/labeled_tables/dynamic/label_table_dynamic_rows1.pdf
Binary file not shown.
Oops, something went wrong.