-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[formrecognizer] Use get_words and get_lines in samples #21533
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
Merged
catalinaperalta
merged 10 commits into
Azure:main
from
catalinaperalta:getChildrenSamples
Nov 3, 2021
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ed5f4d
use get_words and get_lines in samples
catalinaperalta d4385ec
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
catalinaperalta 0b7def2
fix comment and add info on README
catalinaperalta 9c7b17e
fix wording in sample
catalinaperalta 5e408af
update readme
catalinaperalta 5c0b8ce
revert additions to README
catalinaperalta ae00f6f
add get_words() example to README
catalinaperalta 29858b8
add get children samples
catalinaperalta b7368c9
remove get_lines from entities
catalinaperalta f5adfe9
update comment
catalinaperalta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
136 changes: 136 additions & 0 deletions
136
...e-ai-formrecognizer/samples/v3.2-beta/async_samples/sample_get_document_elements_async.py
This file contains hidden or 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,136 @@ | ||
| # 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_get_document_elements_async.py | ||
|
|
||
| DESCRIPTION: | ||
| This sample demonstrates how to get related document elements from the result of calling | ||
| `begin_analyze_document()`. | ||
|
|
||
| USAGE: | ||
| python sample_get_document_elements_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 get_document_elements_async(): | ||
| path_to_sample_documents = os.path.abspath( | ||
| os.path.join( | ||
| os.path.abspath(__file__), | ||
| "..", | ||
| "..", | ||
| "..", | ||
| "./sample_forms/forms/Form_1.jpg", | ||
| ) | ||
| ) | ||
|
|
||
| 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() | ||
|
|
||
| print("----Getting words in key-value pairs found in document----") | ||
| for kv_pair in 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), | ||
| ) | ||
| ) | ||
| words = kv_pair.key.get_words() | ||
| print( | ||
| "Key has {} word(s):".format( | ||
| len(words), | ||
| ) | ||
| ) | ||
| for word in words: | ||
| print( | ||
| "...found '{}' word with confidence {}".format( | ||
| word.content, | ||
| word.confidence, | ||
| ) | ||
| ) | ||
|
|
||
| print("----Getting words in entities found in document----") | ||
| for entity in result.entities: | ||
| print("Entity of category '{}' with sub-category '{}'".format(entity.category, entity.sub_category)) | ||
| # NOTE: Calling get_lines() here will return a list of the DocumentLines that make up the entity. | ||
| # These lines can be processed just like any other DocumentLine instance. | ||
| words = entity.get_words() | ||
| for word in words: | ||
| print( | ||
| "...contains '{}' with confidence {}".format( | ||
| word.content, | ||
| word.confidence, | ||
| ) | ||
| ) | ||
|
|
||
| print("----Getting lines in tables found in document----") | ||
| for table_idx, table in enumerate(result.tables): | ||
| print( | ||
| "Table # {} has {} rows and {} columns".format( | ||
| table_idx, table.row_count, table.column_count | ||
| ) | ||
| ) | ||
| print( | ||
| "Table # {} has {} lines and {} words".format( | ||
| table_idx, len(table.get_lines()), len(table.get_words()) | ||
| ) | ||
| ) | ||
| for line in table.get_lines(): | ||
| print( | ||
| "...found '{}' line".format( | ||
| line.content, | ||
| ) | ||
| ) | ||
| for word in line.get_words(): | ||
| print( | ||
| "......contains '{}' with confidence {}".format( | ||
| word.content, | ||
| word.confidence, | ||
| ) | ||
| ) | ||
| print("----------------------------------------") | ||
|
|
||
|
|
||
| async def main(): | ||
| await get_document_elements_async() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| loop = asyncio.get_event_loop() | ||
| loop.run_until_complete(main()) | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.