-
Notifications
You must be signed in to change notification settings - Fork 49
/
sample_analyze_addon_fonts.py
221 lines (179 loc) · 10.2 KB
/
sample_analyze_addon_fonts.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# 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_addon_fonts.py
DESCRIPTION:
This sample demonstrates how to extract font information using the add-on
'STYLE_FONT' capability.
Add-on capabilities are available within all models except for the Business card
model. This sample uses Layout model to demonstrate.
Add-on capabilities accept a list of strings containing values from the `AnalysisFeature`
enum class. For more information, see:
https://learn.microsoft.com/en-us/python/api/azure-ai-formrecognizer/azure.ai.formrecognizer.analysisfeature?view=azure-python.
The following capabilities are free:
- BARCODES
- LANGUAGES
The following capabilities will incur additional charges:
- FORMULAS
- OCR_HIGH_RESOLUTION
- STYLE_FONT
See pricing: https://azure.microsoft.com/pricing/details/ai-document-intelligence/.
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_addon_fonts.py" in an editor or IDE.
2) Open the "sample_analyze_addon_fonts.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_addon_fonts.py
"""
import os
from collections import defaultdict
def format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
return ", ".join(
f"Page #{region.page_number}: {format_polygon(region.polygon)}"
for region in bounding_regions
)
# 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])
# To learn the detailed concept of "span" in the following content, visit: https://aka.ms/v3.1-spans
def get_styled_text(styles, content):
# Iterate over the styles and merge the spans from each style.
spans = [span for style in styles for span in style.spans]
spans.sort(key=lambda span: span.offset)
return ','.join([content[span.offset : span.offset + span.length] for span in spans])
def analyze_fonts():
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient, AnalysisFeature
# 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://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/curl/form-recognizer/contoso-allinone.jpg?raw=true"
# 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, features=[AnalysisFeature.STYLE_FONT] # Specify which add-on capabilities to enable.
)
# # 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, features=[AnalysisFeature.STYLE_FONT] # Specify which add-on capabilities to enable.
# )
result = poller.result()
# [START analyze_fonts]
# DocumentStyle has the following font related attributes:
similar_font_families = defaultdict(list) # e.g., 'Arial, sans-serif
font_styles = defaultdict(list) # e.g, 'italic'
font_weights = defaultdict(list) # e.g., 'bold'
font_colors = defaultdict(list) # in '#rrggbb' hexadecimal format
font_background_colors = defaultdict(list) # in '#rrggbb' hexadecimal format
if any([style.is_handwritten for style in result.styles]):
print("Document contains handwritten content")
else:
print("Document does not contain handwritten content")
print("\n----Fonts styles detected in the document----")
# Iterate over the styles and group them by their font attributes.
for style in result.styles:
if style.similar_font_family:
similar_font_families[style.similar_font_family].append(style)
if style.font_style:
font_styles[style.font_style].append(style)
if style.font_weight:
font_weights[style.font_weight].append(style)
if style.color:
font_colors[style.color].append(style)
if style.background_color:
font_background_colors[style.background_color].append(style)
print(f"Detected {len(similar_font_families)} font families:")
for font_family, styles in similar_font_families.items():
print(f"- Font family: '{font_family}'")
print(f" Text: '{get_styled_text(styles, result.content)}'")
print(f"\nDetected {len(font_styles)} font styles:")
for font_style, styles in font_styles.items():
print(f"- Font style: '{font_style}'")
print(f" Text: '{get_styled_text(styles, result.content)}'")
print(f"\nDetected {len(font_weights)} font weights:")
for font_weight, styles in font_weights.items():
print(f"- Font weight: '{font_weight}'")
print(f" Text: '{get_styled_text(styles, result.content)}'")
print(f"\nDetected {len(font_colors)} font colors:")
for font_color, styles in font_colors.items():
print(f"- Font color: '{font_color}'")
print(f" Text: '{get_styled_text(styles, result.content)}'")
print(f"\nDetected {len(font_background_colors)} font background colors:")
for font_background_color, styles in font_background_colors.items():
print(f"- Font background color: '{font_background_color}'")
print(f" Text: '{get_styled_text(styles, result.content)}'")
print("----------------------------------------")
# [END analyze_fonts]
if __name__ == "__main__":
from azure.core.exceptions import HttpResponseError
try:
analyze_fonts()
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 Add-on capabilities (Font property extraction): https://aka.ms/V3.1-font-property-extraction
# Find more sample code: https://github.com/Azure-Samples/document-intelligence-code-samples