Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[formrecognizer] Add prebuilt-document samples and tests #20894

Merged
merged 5 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2598,8 +2598,12 @@ def __init__(self, **kwargs):
@classmethod
def _from_generated(cls, key_value_pair):
return cls(
key=DocumentKeyValueElement._from_generated(key_value_pair.key),
value=DocumentKeyValueElement._from_generated(key_value_pair.value),
key=DocumentKeyValueElement._from_generated(key_value_pair.key)
if key_value_pair.key
else None,
value=DocumentKeyValueElement._from_generated(key_value_pair.value)
if key_value_pair.value
else None,
confidence=key_value_pair.confidence,
)

Expand Down Expand Up @@ -2875,7 +2879,9 @@ def _from_generated(cls, mark):
return cls(
state=mark.state,
bounding_box=get_bounding_box(mark),
span=DocumentSpan._from_generated(mark.span),
span=DocumentSpan._from_generated(mark.span)
if mark.span
else None,
confidence=mark.confidence,
)

Expand Down Expand Up @@ -3433,7 +3439,9 @@ def _from_generated(cls, word):
return cls(
content=word.content,
bounding_box=get_bounding_box(word),
span=DocumentSpan._from_generated(word.span),
span=DocumentSpan._from_generated(word.span)
if word.span
else None,
confidence=word.confidence,
)

Expand Down Expand Up @@ -3525,7 +3533,9 @@ def _from_generated(cls, response):
api_version=response.api_version,
model_id=response.model_id,
content=response.content,
pages=[DocumentPage._from_generated(page) for page in response.pages],
pages=[DocumentPage._from_generated(page) for page in response.pages]
if response.pages
else [],
tables=[DocumentTable._from_generated(table) for table in response.tables]
if response.tables
else [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# 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_analyze_document_async.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should maybe add prebuilt into the sample file name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about general document? I didnt want to add prebuilt only to this one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prebuilt makes the most sense to me since we pass "prebuilt-document". do you mean you don't want to add it because the other samples don't have prebuilt? I agree that it's not ideal, but if the service team markets it as the "prebuilt document" model I think we should probably follow suit so people can connect the sample with the feature. Have you seen them use any other names for it?

DESCRIPTION:
This sample demonstrates how to extract general document information from a document
given through a file.
Note that selection marks returned from begin_analyze_document() do not return the text associated with
the checkbox. For the API to return this information, build a custom model to analyze the checkbox and its text.
See sample_build_model_async.py for more information.
Comment on lines +16 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's nix this comment... more of a v2 limitation. You might be able to infer the text associated with the checkbox now that everything is included in reading order in the content string.

USAGE:
python sample_analyze_document_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
"""

import os
import asyncio

def format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
return ", ".join("Page #{}: {}".format(region.page_number, format_bounding_box(region.bounding_box)) for region in bounding_regions)

def format_bounding_box(bounding_box):
if not bounding_box:
return "N/A"
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])


async def analyze_document():
path_to_sample_documents = os.path.abspath(
os.path.join(
os.path.abspath(__file__),
"..",
"..",
"..",
"./sample_forms/forms/form_selection_mark.png",
)
)
# [START analyze_document]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import DocumentAnalysisClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)

async with document_analysis_client:
with open(path_to_sample_documents, "rb") as f:
poller = await document_analysis_client.begin_analyze_document(
"prebuilt-document", document=f
)
result = await poller.result()

for idx, style in enumerate(result.styles):
print(
"Document contains {} content".format(
"handwritten" if style.is_handwritten else "no handwritten"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"handwritten" if style.is_handwritten else "not handwritten"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would end up saying "Document contains not handwritten content" :S

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. I read that like 5x and it still read wrong to me. losing it 😆

)
)

for idx, page in enumerate(result.pages):
print("----Analyzing document from page #{}----".format(idx + 1))
print(
"Page has width: {} and height: {}, measured with unit: {}".format(
page.width, page.height, page.unit
)
)

for line_idx, line in enumerate(page.lines):
print(
"Line # {} has text content '{}' within bounding box '{}'".format(
line_idx,
line.content,
format_bounding_box(line.bounding_box),
)
)

for word in page.words:
print(
"...Word '{}' has a confidence of {}".format(
word.content, word.confidence
)
)

for selection_mark in page.selection_marks:
print(
"Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
selection_mark.state,
format_bounding_box(selection_mark.bounding_box),
selection_mark.confidence,
)
)

for table_idx, table in enumerate(result.tables):
print(
"Table # {} has {} rows and {} columns".format(
table_idx, table.row_count, table.column_count
)
)
for region in table.bounding_regions:
print(
"Table # {} location on page: {} is {}".format(
table_idx,
region.page_number,
format_bounding_box(region.bounding_box),
)
)
for cell in table.cells:
print(
"...Cell[{}][{}] has text '{}'".format(
cell.row_index,
cell.column_index,
cell.content,
)
)
for region in cell.bounding_regions:
print(
"...content on page {} is within bounding box '{}'".format(
region.page_number,
format_bounding_box(region.bounding_box),
)
)

print("----Entities found in document----")
for idx, entity in enumerate(result.entities):
print("Entity of category '{}' with sub-category '{}'".format(entity.category, entity.sub_category))
print("...has content '{}'".format(entity.content))
print("...within '{}' bounding regions".format(format_bounding_region(entity.bounding_regions)))
print("...with confidence {}".format(entity.confidence))

print("----Key-value pairs found in document----")
for idx, kv_pair in enumerate(result.key_value_pairs):
if kv_pair.key:
print(
"Key '{}' found within '{}' bounding regions".format(
kv_pair.key.content,
format_bounding_region(kv_pair.key.bounding_regions),
)
)
if kv_pair.value:
print(
"Value '{}' found within '{}' bounding regions".format(
catalinaperalta marked this conversation as resolved.
Show resolved Hide resolved
kv_pair.value.content,
format_bounding_region(kv_pair.value.bounding_regions),
)
)
print("----------------------------------------")

# [END analyze_document]


async def main():
await analyze_document()

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def analyze_layout_async():
for idx, style in enumerate(result.styles):
print(
"Document contains {} content".format(
"handwritten" if style.is_handwritte else "no handwritten"
"handwritten" if style.is_handwritten else "no handwritten"
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# 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_analyze_document.py
DESCRIPTION:
This sample demonstrates how to extract general document information from a document
given through a file.
Note that selection marks returned from begin_analyze_document() do not return the text associated with
the checkbox. For the API to return this information, build a custom model to analyze the checkbox and its text.
See sample_build_model.py for more information.
USAGE:
python sample_analyze_document.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
"""

import os

def format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
return ", ".join("Page #{}: {}".format(region.page_number, format_bounding_box(region.bounding_box)) for region in bounding_regions)

def format_bounding_box(bounding_box):
if not bounding_box:
return "N/A"
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])


def analyze_document():
path_to_sample_documents = os.path.abspath(
os.path.join(
os.path.abspath(__file__),
"..",
"..",
"./sample_forms/forms/form_selection_mark.png",
)
)
# [START analyze_document]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open(path_to_sample_documents, "rb") as f:
poller = document_analysis_client.begin_analyze_document(
"prebuilt-document", document=f
)
result = poller.result()

for idx, style in enumerate(result.styles):
print(
"Document contains {} content".format(
"handwritten" if style.is_handwritten else "no handwritten"
)
)

for idx, page in enumerate(result.pages):
print("----Analyzing document from page #{}----".format(idx + 1))
print(
"Page has width: {} and height: {}, measured with unit: {}".format(
page.width, page.height, page.unit
)
)

for line_idx, line in enumerate(page.lines):
print(
"Line # {} has text content '{}' within bounding box '{}'".format(
line_idx,
line.content,
format_bounding_box(line.bounding_box),
)
)

for word in page.words:
print(
"...Word '{}' has a confidence of {}".format(
word.content, word.confidence
)
)

for selection_mark in page.selection_marks:
print(
"Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
selection_mark.state,
format_bounding_box(selection_mark.bounding_box),
selection_mark.confidence,
)
)

for table_idx, table in enumerate(result.tables):
print(
"Table # {} has {} rows and {} columns".format(
table_idx, table.row_count, table.column_count
)
)
for region in table.bounding_regions:
print(
"Table # {} location on page: {} is {}".format(
table_idx,
region.page_number,
format_bounding_box(region.bounding_box),
)
)
for cell in table.cells:
print(
"...Cell[{}][{}] has text '{}'".format(
cell.row_index,
cell.column_index,
cell.content,
)
)
for region in cell.bounding_regions:
print(
"...content on page {} is within bounding box '{}'".format(
region.page_number,
format_bounding_box(region.bounding_box),
)
)

print("----Entities found in document----")
for idx, entity in enumerate(result.entities):
print("Entity of category '{}' with sub-category '{}'".format(entity.category, entity.sub_category))
print("...has content '{}'".format(entity.content))
print("...within '{}' bounding regions".format(format_bounding_region(entity.bounding_regions)))
print("...with confidence {}".format(entity.confidence))

print("----Key-value pairs found in document----")
for idx, kv_pair in enumerate(result.key_value_pairs):
if kv_pair.key:
print(
"Key '{}' found within '{}' bounding regions".format(
kv_pair.key.content,
format_bounding_region(kv_pair.key.bounding_regions),
)
)
if kv_pair.value:
print(
"Value '{}' found within '{}' bounding regions".format(
kv_pair.value.content,
format_bounding_region(kv_pair.value.bounding_regions),
)
)
print("----------------------------------------")

# [END analyze_document]


if __name__ == "__main__":
analyze_document()
Loading