Skip to content

Commit 85a96aa

Browse files
authored
Merge pull request #202634 from laujan/update-python-quickstart
update python code
2 parents 62ad0d5 + 00b6590 commit 85a96aa

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

articles/applied-ai-services/form-recognizer/quickstarts/try-v3-python-sdk.md

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ manager: nitinme
77
ms.service: applied-ai-services
88
ms.subservice: forms-recognizer
99
ms.topic: quickstart
10-
ms.date: 03/31/2022
10+
ms.date: 06/23/2022
1111
ms.author: lajanuar
1212
recommendations: false
1313
---
@@ -107,12 +107,12 @@ key = "<your-key>"
107107
def format_bounding_region(bounding_regions):
108108
if not bounding_regions:
109109
return "N/A"
110-
return ", ".join("Page #{}: {}".format(region.page_number, format_bounding_box(region.bounding_box)) for region in bounding_regions)
110+
return ", ".join("Page #{}: {}".format(region.page_number, format_polygon(region.polygon)) for region in bounding_regions)
111111

112-
def format_bounding_box(bounding_box):
113-
if not bounding_box:
112+
def format_polygon(polygon):
113+
if not polygon:
114114
return "N/A"
115-
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])
115+
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
116116

117117

118118
def analyze_general_documents():
@@ -148,13 +148,6 @@ def analyze_general_documents():
148148
)
149149
)
150150

151-
print("----Entities found in document----")
152-
for entity in result.entities:
153-
print("Entity of category '{}' with sub-category '{}'".format(entity.category, entity.sub_category))
154-
print("...has content '{}'".format(entity.content))
155-
print("...within '{}' bounding regions".format(format_bounding_region(entity.bounding_regions)))
156-
print("...with confidence {}\n".format(entity.confidence))
157-
158151
for page in result.pages:
159152
print("----Analyzing document from page #{}----".format(page.page_number))
160153
print(
@@ -168,7 +161,7 @@ def analyze_general_documents():
168161
"...Line # {} has text content '{}' within bounding box '{}'".format(
169162
line_idx,
170163
line.content,
171-
format_bounding_box(line.bounding_box),
164+
format_polygon(line.polygon),
172165
)
173166
)
174167

@@ -183,7 +176,7 @@ def analyze_general_documents():
183176
print(
184177
"...Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
185178
selection_mark.state,
186-
format_bounding_box(selection_mark.bounding_box),
179+
format_polygon(selection_mark.polygon),
187180
selection_mark.confidence,
188181
)
189182
)
@@ -199,7 +192,7 @@ def analyze_general_documents():
199192
"Table # {} location on page: {} is {}".format(
200193
table_idx,
201194
region.page_number,
202-
format_bounding_box(region.bounding_box),
195+
format_polygon(region.polygon),
203196
)
204197
)
205198
for cell in table.cells:
@@ -214,7 +207,7 @@ def analyze_general_documents():
214207
print(
215208
"...content on page {} is within bounding box '{}'\n".format(
216209
region.page_number,
217-
format_bounding_box(region.bounding_box),
210+
format_polygon(region.polygon),
218211
)
219212
)
220213
print("----------------------------------------")
@@ -274,17 +267,18 @@ from azure.core.credentials import AzureKeyCredential
274267
endpoint = "<your-endpoint>"
275268
key = "<your-key>"
276269

277-
def format_bounding_box(bounding_box):
278-
if not bounding_box:
270+
def format_polygon(polygon):
271+
if not polygon:
279272
return "N/A"
280-
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])
273+
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
281274

282275
def analyze_layout():
283276
# sample form document
284277
formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
285278

286-
# create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
287-
document_analysis_client = DocumentAnalysisClient(endpoint=endpoint, credential=AzureKeyCredential(key))
279+
document_analysis_client = DocumentAnalysisClient(
280+
endpoint=endpoint, credential=AzureKeyCredential(key)
281+
)
288282

289283
poller = document_analysis_client.begin_analyze_document_from_url(
290284
"prebuilt-layout", formUrl)
@@ -312,7 +306,7 @@ def analyze_layout():
312306
line_idx,
313307
len(words),
314308
line.content,
315-
format_bounding_box(line.bounding_box),
309+
format_polygon(line.polygon),
316310
)
317311
)
318312

@@ -327,7 +321,7 @@ def analyze_layout():
327321
print(
328322
"...Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
329323
selection_mark.state,
330-
format_bounding_box(selection_mark.bounding_box),
324+
format_polygon(selection_mark.polygon),
331325
selection_mark.confidence,
332326
)
333327
)
@@ -343,7 +337,7 @@ def analyze_layout():
343337
"Table # {} location on page: {} is {}".format(
344338
table_idx,
345339
region.page_number,
346-
format_bounding_box(region.bounding_box),
340+
format_polygon(region.polygon),
347341
)
348342
)
349343
for cell in table.cells:
@@ -358,7 +352,7 @@ def analyze_layout():
358352
print(
359353
"...content on page {} is within bounding box '{}'".format(
360354
region.page_number,
361-
format_bounding_box(region.bounding_box),
355+
format_polygon(region.polygon),
362356
)
363357
)
364358

@@ -434,20 +428,21 @@ key = "<your-key>"
434428
def format_bounding_region(bounding_regions):
435429
if not bounding_regions:
436430
return "N/A"
437-
return ", ".join("Page #{}: {}".format(region.page_number, format_bounding_box(region.bounding_box)) for region in bounding_regions)
431+
return ", ".join("Page #{}: {}".format(region.page_number, format_polygon(region.polygon)) for region in bounding_regions)
438432

439-
def format_bounding_box(bounding_box):
440-
if not bounding_box:
433+
def format_polygon(polygon):
434+
if not polygon:
441435
return "N/A"
442-
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])
436+
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
443437

444438

445439
def analyze_invoice():
446440

447441
invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
448442

449-
# create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
450-
document_analysis_client = DocumentAnalysisClient(endpoint=endpoint, credential=AzureKeyCredential(key))
443+
document_analysis_client = DocumentAnalysisClient(
444+
endpoint=endpoint, credential=AzureKeyCredential(key)
445+
)
451446

452447
poller = document_analysis_client.begin_analyze_document_from_url(
453448
"prebuilt-invoice", invoiceUrl)
@@ -702,6 +697,8 @@ def analyze_invoice():
702697

703698
if __name__ == "__main__":
704699
analyze_invoice()
700+
701+
print("----------------------------------------")
705702
```
706703

707704
**Run the application**

0 commit comments

Comments
 (0)