-
Notifications
You must be signed in to change notification settings - Fork 49
/
sample_analyze_layout.py
197 lines (164 loc) · 9.68 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
194
195
196
197
# 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, tables, figures, selection marks and document structure (e.g., sections) information from a document
given through a file.
PREREQUISITES:
The following prerequisites are necessary to run the code. For more details, please visit the "How-to guides" link: https://aka.ms/how-to-guide
-------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 DOCUMENTINTELLIGENCE_API_KEY <yourKey>
setx DOCUMENTINTELLIGENCE_ENDPOINT <yourEndpoint>
• You need to 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/set-environment-variables-for-macOS
3) For Linux:
export DOCUMENTINTELLIGENCE_API_KEY=<yourKey>
export DOCUMENTINTELLIGENCE_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/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-documentintelligence --pre
------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
def get_words(page, line):
result = []
for word in page.words:
if _in_span(word, line.spans):
result.append(word)
return result
# To learn the detailed concept of "span" in the following codes, visit: https://aka.ms/spans
def _in_span(word, spans):
for span in spans:
if word.span.offset >= span.offset and (word.span.offset + word.span.length) <= (span.offset + span.length):
return True
return False
def analyze_layout():
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult, AnalyzeDocumentRequest
# For how to obtain the endpoint and key, please see PREREQUISITES above.
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
# Analyze a document at a URL:
formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
# Replace with your actual formUrl:
# If you use the URL of a public website, to find more URLs, please visit: https://aka.ms/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_intelligence_client.begin_analyze_document(
"prebuilt-layout",
AnalyzeDocumentRequest(url_source=formUrl)
)
# # 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_intelligence_client.begin_analyze_document(
# "prebuilt-layout", analyze_request=f, content_type="application/octet-stream"
# )
result: AnalyzeResult = poller.result()
# [START extract_layout]
# Analyze whether the document contains handwritten content.
if result.styles and any([style.is_handwritten for style in result.styles]):
print("Document contains handwritten content")
else:
print("Document does not contain handwritten content")
# Analyze pages.
# To learn the detailed concept of "bounding polygon" in the following content, visit: https://aka.ms/bounding-region
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.
if page.lines:
for line_idx, line in enumerate(page.lines):
words = get_words(page, line)
print(
f"...Line # {line_idx} has word count {len(words)} and text '{line.content}' "
f"within bounding polygon '{line.polygon}'"
)
# Analyze words.
for word in words:
print(f"......Word '{word.content}' has a confidence of {word.confidence}")
# Analyze selection marks.
if page.selection_marks:
for selection_mark in page.selection_marks:
print(
f"Selection mark is '{selection_mark.state}' within bounding polygon "
f"'{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/train-your-custom-model
# Analyze tables.
if result.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")
if table.bounding_regions:
for region in table.bounding_regions:
print(f"Table # {table_idx} location on page: {region.page_number} is {region.polygon}")
# Analyze cells.
for cell in table.cells:
print(f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'")
if cell.bounding_regions:
for region in cell.bounding_regions:
print(f"...content on page {region.page_number} is within bounding polygon '{region.polygon}'")
# Analyze figures.
# To learn the detailed concept of "figures" in the following content, visit: https://aka.ms/figures
if result.figures:
for figures_idx,figures in enumerate(result.figures):
print(f"Figure # {figures_idx} has the following spans:{figures.spans}")
for region in figures.bounding_regions:
print(f"Figure # {figures_idx} location on page:{region.page_number} is within bounding polygon '{region.polygon}'")
print("----------------------------------------")
# [END extract_layout]
if __name__ == "__main__":
from azure.core.exceptions import HttpResponseError
from dotenv import find_dotenv, load_dotenv
try:
load_dotenv(find_dotenv())
analyze_layout()
except HttpResponseError as error:
# 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/di-layout
# Find more sample code: https://aka.ms/doc-intelligence-samples