-
Notifications
You must be signed in to change notification settings - Fork 16
/
sample_analyze_layout.py
193 lines (162 loc) · 8.89 KB
/
sample_analyze_layout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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_layout.py
DESCRIPTION:
This sample demonstrates how to extract text, selection marks, and layout information from a document
given through a file.
Note that selection marks returned from begin_analyze_document(model_id="prebuilt-layout") 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.
PREREQUISITES:
The following prerequisites are necessary to run the code. For more details, please visit the "How-to guides" link: https://aka.ms/How-toguides
-------Python and IDE------
1) Install Python 3.7 or later (https://www.python.org/), which should include pip (https://pip.pypa.io/en/stable/).
2) Install the latest version of Visual Studio Code (https://code.visualstudio.com/) or your preferred IDE.
------Azure AI services or Document Intelligence resource------
Create a single-service (https://aka.ms/single-service) or multi-service (https://aka.ms/multi-service) resource.
You can use the free pricing tier (F0) to try the service and upgrade to a paid tier for production later.
------Get the key and endpoint------
1) After your resource is deployed, select "Go to resource".
2) In the left navigation menu, select "Keys and Endpoint".
3) Copy one of the keys and the Endpoint for use in this sample.
------Set your environment variables------
At a command prompt, run the following commands, replacing <yourKey> and <yourEndpoint> with the values from your resource in the Azure portal.
1) For Windows:
setx DI_KEY <yourKey>
setx DI_ENDPOINT <yourEndpoint>
• Close the Command Prompt window after you set your environment variables. Restart any running programs that read the environment variable.
2) For macOS:
export key=<yourKey>
export endpoint=<yourEndpoint>
• This is a temporary environment variable setting method that only lasts until you close the terminal session.
• To set an environment variable permanently, visit: https://aka.ms/V3.1-set-environment-variables-for-macOS
3) For Linux:
export DI_KEY=<yourKey>
export DI_ENDPOINT=<yourEndpoint>
• This is a temporary environment variable setting method that only lasts until you close the terminal session.
• To set an environment variable permanently, visit: https://aka.ms/V3.1-set-environment-variables-for-Linux
------Set up your programming environment------
At a command prompt,run the following code to install the Azure AI Document Intelligence client library for Python with pip:
pip install azure-ai-formrecognizer==3.3.0
------Create your Python application------
1) Create a new Python file called "sample_analyze_layout.py" in an editor or IDE.
2) Open the "sample_analyze_layout.py" file and insert the provided code sample into your application.
3) At a command prompt, use the following code to run the Python code:
python sample_analyze_layout.py
"""
import os
# To learn the detailed concept of "polygon" in the following content, visit: https://aka.ms/V3.1-bounding-region
def format_polygon(polygon):
if not polygon:
return "N/A"
return ", ".join([f"[{p.x}, {p.y}]" for p in polygon])
def analyze_layout():
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
# For how to obtain the endpoint and key, please see PREREQUISITES above.
endpoint = os.environ["DI_ENDPOINT"]
key = os.environ["DI_KEY"]
document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
# Analyze a document at a URL:
url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
# Replace with your actual url:
# If you use the URL of a public website, to find more URLs, please visit: https://aka.ms/V3.1-more-URLs
# If you analyze a document in Blob Storage, you need to generate Public SAS URL, please visit: https://aka.ms/create-sas-tokens
poller = document_analysis_client.begin_analyze_document_from_url(
"prebuilt-layout", document_url=url
)
# # If analyzing a local document, remove the comment markers (#) at the beginning of these 8 lines.
# # Delete or comment out the part of "Analyze a document at a URL" above.
# # Replace <path to your sample file> with your actual file path.
# path_to_sample_document = "<path to your sample file>"
# with open(path_to_sample_document, "rb") as f:
# poller = document_analysis_client.begin_analyze_document(
# "prebuilt-layout", document=f
# )
result = poller.result()
# [START extract_layout]
# Analyze whether the document contains handwritten content.
if any([style.is_handwritten for style in result.styles]):
print("Document contains handwritten content")
else:
print("Document does not contain handwritten content")
# Analyze pages.
for page in result.pages:
print(f"----Analyzing layout from page #{page.page_number}----")
print(
f"Page has width: {page.width} and height: {page.height}, measured with unit: {page.unit}"
)
# Analyze lines.
for line_idx, line in enumerate(page.lines):
words = line.get_words()
print(
f"...Line # {line_idx} has word count {len(words)} and text '{line.content}' "
f"within bounding polygon '{format_polygon(line.polygon)}'"
)
# Analyze words.
for word in words:
print(
f"......Word '{word.content}' has a confidence of {word.confidence}"
)
# Analyze selection marks.
for selection_mark in page.selection_marks:
print(
f"Selection mark is '{selection_mark.state}' within bounding polygon "
f"'{format_polygon(selection_mark.polygon)}' and has a confidence of {selection_mark.confidence}"
)
# Note that selection marks returned from begin_analyze_document(model_id="prebuilt-layout") 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. For detailed steps, visit: https://aka.ms/V3.1-train-your-custom-model
# Analyze tables.
for table_idx, table in enumerate(result.tables):
print(
f"Table # {table_idx} has {table.row_count} rows and "
f"{table.column_count} columns"
)
for region in table.bounding_regions:
print(
f"Table # {table_idx} location on page: {region.page_number} is {format_polygon(region.polygon)}"
)
for cell in table.cells:
print(
f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'"
)
for region in cell.bounding_regions:
print(
f"...content on page {region.page_number} is within bounding polygon '{format_polygon(region.polygon)}'"
)
print("----------------------------------------")
# [END extract_layout]
if __name__ == "__main__":
import sys
from azure.core.exceptions import HttpResponseError
try:
analyze_layout()
except HttpResponseError as error:
print(
"For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
)
# Examples of how to check an HttpResponseError
# Check by error code:
if error.error is not None:
if error.error.code == "InvalidImage":
print(f"Received an invalid image error: {error.error}")
if error.error.code == "InvalidRequest":
print(f"Received an invalid request error: {error.error}")
# Raise the error again after printing it
raise
# If the inner error is None and then it is possible to check the message to get more information:
if "Invalid request".casefold() in error.message.casefold():
print(f"Uh-oh! Seems there was an invalid request: {error}")
# Raise the error again
raise
# Next steps:
# Learn more about Layout model: https://aka.ms/V3.1-layout
# Find more sample code: https://github.com/Azure-Samples/document-intelligence-code-samples