Skip to content

Commit 9e1d561

Browse files
[formrecognizer] Add samples for converting to and from dictionary (Azure#21770)
Fixes in part Azure#21592 Pending: update samples readme and mentioning these samples in the changelog.
1 parent 0db03b1 commit 9e1d561

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_and_from_dict_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert models returned from a recognize operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
16+
17+
USAGE:
18+
python sample_convert_to_and_from_dict_async.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource.
22+
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
23+
"""
24+
25+
import os
26+
import json
27+
import asyncio
28+
29+
async def convert_to_and_from_dict_async():
30+
path_to_sample_forms = os.path.abspath(
31+
os.path.join(
32+
os.path.abspath(__file__),
33+
"..",
34+
"..",
35+
"..",
36+
"./sample_forms/id_documents/license.jpg",
37+
)
38+
)
39+
40+
from azure.core.serialization import AzureJSONEncoder
41+
from azure.core.credentials import AzureKeyCredential
42+
from azure.ai.formrecognizer.aio import FormRecognizerClient
43+
from azure.ai.formrecognizer import RecognizedForm
44+
45+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
46+
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
47+
48+
form_recognizer_client = FormRecognizerClient(
49+
endpoint=endpoint, credential=AzureKeyCredential(key)
50+
)
51+
async with form_recognizer_client:
52+
with open(path_to_sample_forms, "rb") as f:
53+
poller = await form_recognizer_client.begin_recognize_identity_documents(identity_document=f)
54+
55+
id_documents = await poller.result()
56+
57+
# convert the received model to a dictionary
58+
recognized_form_dict = [doc.to_dict() for doc in id_documents]
59+
60+
# save the dictionary as JSON content in a JSON file, use the AzureJSONEncoder
61+
# to help make types, such as dates, JSON serializable
62+
# NOTE: AzureJSONEncoder is only available with azure.core>=1.18.0.
63+
with open('data.json', 'w') as f:
64+
json.dump(recognized_form_dict, f, cls=AzureJSONEncoder)
65+
66+
# convert the dictionary back to the original model
67+
model = [RecognizedForm.from_dict(doc) for doc in recognized_form_dict]
68+
69+
# use the model as normal
70+
for idx, id_document in enumerate(model):
71+
print("--------Recognizing converted ID document #{}--------".format(idx+1))
72+
first_name = id_document.fields.get("FirstName")
73+
if first_name:
74+
print("First Name: {} has confidence: {}".format(first_name.value, first_name.confidence))
75+
last_name = id_document.fields.get("LastName")
76+
if last_name:
77+
print("Last Name: {} has confidence: {}".format(last_name.value, last_name.confidence))
78+
document_number = id_document.fields.get("DocumentNumber")
79+
if document_number:
80+
print("Document Number: {} has confidence: {}".format(document_number.value, document_number.confidence))
81+
82+
print("----------------------------------------")
83+
84+
85+
async def main():
86+
await convert_to_and_from_dict_async()
87+
88+
89+
if __name__ == '__main__':
90+
loop = asyncio.get_event_loop()
91+
loop.run_until_complete(main())
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_and_from_dict.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert models returned from an analyze operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
16+
17+
USAGE:
18+
python sample_convert_to_and_from_dict.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource.
22+
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
23+
"""
24+
25+
import os
26+
import json
27+
28+
def convert_to_and_from_dict():
29+
path_to_sample_forms = os.path.abspath(
30+
os.path.join(
31+
os.path.abspath(__file__),
32+
"..",
33+
"..",
34+
"./sample_forms/id_documents/license.jpg",
35+
)
36+
)
37+
38+
from azure.core.serialization import AzureJSONEncoder
39+
from azure.core.credentials import AzureKeyCredential
40+
from azure.ai.formrecognizer import FormRecognizerClient, RecognizedForm
41+
42+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
43+
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
44+
45+
form_recognizer_client = FormRecognizerClient(
46+
endpoint=endpoint, credential=AzureKeyCredential(key)
47+
)
48+
with open(path_to_sample_forms, "rb") as f:
49+
poller = form_recognizer_client.begin_recognize_identity_documents(identity_document=f)
50+
51+
id_documents = poller.result()
52+
53+
# convert the received model to a dictionary
54+
recognized_form_dict = [doc.to_dict() for doc in id_documents]
55+
56+
# save the dictionary as JSON content in a JSON file, use the AzureJSONEncoder
57+
# to help make types, such as dates, JSON serializable
58+
# NOTE: AzureJSONEncoder is only available with azure.core>=1.18.0.
59+
with open('data.json', 'w') as f:
60+
json.dump(recognized_form_dict, f, cls=AzureJSONEncoder)
61+
62+
# convert the dictionary back to the original model
63+
model = [RecognizedForm.from_dict(doc) for doc in recognized_form_dict]
64+
65+
# use the model as normal
66+
for idx, id_document in enumerate(model):
67+
print("--------Recognizing converted ID document #{}--------".format(idx+1))
68+
first_name = id_document.fields.get("FirstName")
69+
if first_name:
70+
print("First Name: {} has confidence: {}".format(first_name.value, first_name.confidence))
71+
last_name = id_document.fields.get("LastName")
72+
if last_name:
73+
print("Last Name: {} has confidence: {}".format(last_name.value, last_name.confidence))
74+
document_number = id_document.fields.get("DocumentNumber")
75+
if document_number:
76+
print("Document Number: {} has confidence: {}".format(document_number.value, document_number.confidence))
77+
78+
print("----------------------------------------")
79+
80+
81+
if __name__ == "__main__":
82+
convert_to_and_from_dict()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_and_from_dict_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert models returned from an analyze operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
16+
17+
USAGE:
18+
python sample_convert_to_and_from_dict_async.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource.
22+
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
23+
"""
24+
25+
import os
26+
import json
27+
import asyncio
28+
29+
async def convert_to_and_from_dict_async():
30+
path_to_sample_documents = os.path.abspath(
31+
os.path.join(
32+
os.path.abspath(__file__),
33+
"..",
34+
"..",
35+
"..",
36+
"./sample_forms/forms/Form_1.jpg",
37+
)
38+
)
39+
40+
from azure.core.serialization import AzureJSONEncoder
41+
from azure.core.credentials import AzureKeyCredential
42+
from azure.ai.formrecognizer.aio import DocumentAnalysisClient
43+
from azure.ai.formrecognizer import AnalyzeResult
44+
45+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
46+
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
47+
48+
document_analysis_client = DocumentAnalysisClient(
49+
endpoint=endpoint, credential=AzureKeyCredential(key)
50+
)
51+
async with document_analysis_client:
52+
with open(path_to_sample_documents, "rb") as f:
53+
poller = await document_analysis_client.begin_analyze_document(
54+
"prebuilt-document", document=f
55+
)
56+
result = await poller.result()
57+
58+
# convert the received model to a dictionary
59+
analyze_result_dict = result.to_dict()
60+
61+
# save the dictionary as JSON content in a JSON file, use the AzureJSONEncoder
62+
# to help make types, such as dates, JSON serializable
63+
# NOTE: AzureJSONEncoder is only available with azure.core>=1.18.0.
64+
with open('data.json', 'w') as f:
65+
json.dump(analyze_result_dict, f, cls=AzureJSONEncoder)
66+
67+
# convert the dictionary back to the original model
68+
model = AnalyzeResult.from_dict(analyze_result_dict)
69+
70+
# use the model as normal
71+
print("----Converted from dictionary AnalyzeResult----")
72+
print("Model ID: '{}'".format(model.model_id))
73+
print("Number of pages analyzed {}".format(len(model.pages)))
74+
print("API version used: {}".format(model.api_version))
75+
76+
print("----------------------------------------")
77+
78+
79+
async def main():
80+
await convert_to_and_from_dict_async()
81+
82+
83+
if __name__ == '__main__':
84+
loop = asyncio.get_event_loop()
85+
loop.run_until_complete(main())
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_convert_to_and_from_dict.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to convert models returned from an analyze operation
14+
to and from a dictionary. The dictionary in this sample is then converted to a
15+
JSON file, then the same dictionary is converted back to its original model.
16+
17+
USAGE:
18+
python sample_convert_to_and_from_dict.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource.
22+
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
23+
"""
24+
25+
import os
26+
import json
27+
28+
def convert_to_and_from_dict():
29+
path_to_sample_documents = os.path.abspath(
30+
os.path.join(
31+
os.path.abspath(__file__),
32+
"..",
33+
"..",
34+
"./sample_forms/forms/Form_1.jpg",
35+
)
36+
)
37+
38+
from azure.core.serialization import AzureJSONEncoder
39+
from azure.core.credentials import AzureKeyCredential
40+
from azure.ai.formrecognizer import DocumentAnalysisClient, AnalyzeResult
41+
42+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
43+
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
44+
45+
document_analysis_client = DocumentAnalysisClient(
46+
endpoint=endpoint, credential=AzureKeyCredential(key)
47+
)
48+
with open(path_to_sample_documents, "rb") as f:
49+
poller = document_analysis_client.begin_analyze_document(
50+
"prebuilt-document", document=f
51+
)
52+
result = poller.result()
53+
54+
# convert the received model to a dictionary
55+
analyze_result_dict = result.to_dict()
56+
57+
# save the dictionary as JSON content in a JSON file, use the AzureJSONEncoder
58+
# to help make types, such as dates, JSON serializable
59+
# NOTE: AzureJSONEncoder is only available with azure.core>=1.18.0.
60+
with open('data.json', 'w') as f:
61+
json.dump(analyze_result_dict, f, cls=AzureJSONEncoder)
62+
63+
# convert the dictionary back to the original model
64+
model = AnalyzeResult.from_dict(analyze_result_dict)
65+
66+
# use the model as normal
67+
print("----Converted from dictionary AnalyzeResult----")
68+
print("Model ID: '{}'".format(model.model_id))
69+
print("Number of pages analyzed {}".format(len(model.pages)))
70+
print("API version used: {}".format(model.api_version))
71+
72+
print("----------------------------------------")
73+
74+
75+
if __name__ == "__main__":
76+
convert_to_and_from_dict()

0 commit comments

Comments
 (0)