Give AlbumentationsX a star on GitHub — it powers this leaderboard

Star on GitHub

azure-ai-formrecognizer

Microsoft Azure Form Recognizer Client Library for Python

Rank: #3113Downloads: 1,589,287 (30 days)Stars: 5,497Forks: 3,248

Description

Azure Form Recognizer client library for Python

Azure Document Intelligence ([previously known as Form Recognizer][service-rename]) is a cloud service that uses machine learning to analyze text and structured data from your documents. It includes the following main features:

  • Layout - Extract content and structure (ex. words, selection marks, tables) from documents.
  • Document - Analyze key-value pairs in addition to general layout from documents.
  • Read - Read page information from documents.
  • Prebuilt - Extract common field values from select document types (ex. receipts, invoices, business cards, ID documents, U.S. W-2 tax documents, among others) using prebuilt models.
  • Custom - Build custom models from your own data to extract tailored field values in addition to general layout from documents.
  • Classifiers - Build custom classification models that combine layout and language features to accurately detect and identify documents you process within your application.
  • Add-on capabilities - Extract barcodes/QR codes, formulas, font/style, etc. or enable high resolution mode for large documents with optional parameters.

[Source code][python-fr-src] | [Package (PyPI)][python-fr-pypi] | Package (Conda) | [API reference documentation][python-fr-ref-docs] | [Product documentation][python-fr-product-docs] | [Samples][python-fr-samples]

Disclaimer

This package supports the following service API versions: 2.0, 2.1, 2022-08-31 and 2023-07-31. Service API version 2023-10-31-preview and later are supported in package azure-ai-documentintelligence. Please refer this [doc][fr_to_di_migration_guideline] for migration details.

Getting started

Prerequisites

  • Python 3.8 or later is required to use this package.
  • You must have an [Azure subscription][azure_subscription] and a [Cognitive Services or Form Recognizer resource][FR_or_CS_resource] to use this package.

Install the package

Install the Azure Form Recognizer client library for Python with [pip][pip]:

pip install azure-ai-formrecognizer

Note: This version of the client library defaults to the 2023-07-31 version of the service.

This table shows the relationship between SDK versions and supported API versions of the service:

SDK versionSupported API version of service
3.3.X - Latest GA release2.0, 2.1, 2022-08-31, 2023-07-31 (default)
3.2.X2.0, 2.1, 2022-08-31 (default)
3.1.X2.0, 2.1 (default)
3.0.02.0

Note: Starting with version 3.2.X, a new set of clients were introduced to leverage the newest features of the Document Intelligence service. Please see the [Migration Guide][migration-guide] for detailed instructions on how to update application code from client library version 3.1.X or lower to the latest version. Additionally, see the [Changelog][changelog] for more detailed information. The below table describes the relationship of each client and its supported API version(s):

API versionSupported clients
2023-07-31DocumentAnalysisClient and DocumentModelAdministrationClient
2022-08-31DocumentAnalysisClient and DocumentModelAdministrationClient
2.1FormRecognizerClient and FormTrainingClient
2.0FormRecognizerClient and FormTrainingClient

Create a Cognitive Services or Form Recognizer resource

Document Intelligence supports both [multi-service and single-service access][cognitive_resource_portal]. Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Document Intelligence access only, create a Form Recognizer resource. Please note that you will need a single-service resource if you intend to use Azure Active Directory authentication.

You can create either resource using:

  • Option 1: [Azure Portal][cognitive_resource_portal].
  • Option 2: [Azure CLI][cognitive_resource_cli].

Below is an example of how you can create a Form Recognizer resource using the CLI:

# Create a new resource group to hold the Form Recognizer resource
# if using an existing resource group, skip this step
az group create --name <your-resource-name> --location <location>
# Create form recognizer
az cognitiveservices account create \
    --name <your-resource-name> \
    --resource-group <your-resource-group-name> \
    --kind FormRecognizer \
    --sku <sku> \
    --location <location> \
    --yes

For more information about creating the resource or how to get the location and sku information see [here][cognitive_resource_cli].

Authenticate the client

In order to interact with the Document Intelligence service, you will need to create an instance of a client. An endpoint and credential are necessary to instantiate the client object.

Get the endpoint

You can find the endpoint for your Form Recognizer resource using the [Azure Portal][azure_portal_get_endpoint] or [Azure CLI][azure_cli_endpoint_lookup]:

# Get the endpoint for the Form Recognizer resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "properties.endpoint"

Either a regional endpoint or a custom subdomain can be used for authentication. They are formatted as follows:

Regional endpoint: https://<region>.api.cognitive.microsoft.com/
Custom subdomain: https://<resource-name>.cognitiveservices.azure.com/

A regional endpoint is the same for every resource in a region. A complete list of supported regional endpoints can be consulted [here][regional_endpoints]. Please note that regional endpoints do not support AAD authentication.

A custom subdomain, on the other hand, is a name that is unique to the Form Recognizer resource. They can only be used by [single-service resources][cognitive_resource_portal].

Get the API key

The API key can be found in the [Azure Portal][azure_portal] or by running the following Azure CLI command:

az cognitiveservices account keys list --name "<resource-name>" --resource-group "<resource-group-name>"

Create the client with AzureKeyCredential

To use an [API key][cognitive_authentication_api_key] as the credential parameter, pass the key as a string into an instance of [AzureKeyCredential][azure-key-credential].

from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient

endpoint = "https://<my-custom-subdomain>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
document_analysis_client = DocumentAnalysisClient(endpoint, credential)

Create the client with an Azure Active Directory credential

AzureKeyCredential authentication is used in the examples in this getting started guide, but you can also authenticate with Azure Active Directory using the [azure-identity][azure_identity] library. Note that regional endpoints do not support AAD authentication. Create a [custom subdomain][custom_subdomain] name for your resource in order to use this type of authentication.

To use the [DefaultAzureCredential][default_azure_credential] type shown below, or other credential types provided with the Azure SDK, please install the azure-identity package:

pip install azure-identity

You will also need to [register a new AAD application and grant access][register_aad_app] to Document Intelligence by assigning the "Cognitive Services User" role to your service principal.

Once completed, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

<!-- SNIPPET:sample_authentication.create_da_client_with_aad -->
"""DefaultAzureCredential will use the values from these environment
variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
"""
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
credential = DefaultAzureCredential()

document_analysis_client = DocumentAnalysisClient(endpoint, credential)
<!-- END SNIPPET -->

Key concepts

DocumentAnalysisClient

DocumentAnalysisClient provides operations for analyzing input documents using prebuilt and custom models through the begin_analyze_document and begin_analyze_document_from_url APIs. Use the model_id parameter to select the type of model for analysis. See a full list of supported models [here][fr-models]. The DocumentAnalysisClient also provides operations for classifying documents through the begin_classify_document and begin_classify_document_from_url APIs. Custom classification models can classify each page in an input file to identify the document(s) within and can also identify multiple documents or multiple instances of a single document within an input file.

Sample code snippets are provided to illustrate using a DocumentAnalysisClient here. More information about analyzing documents, including supported features, locales, and document types can be found in the [service documentation][fr-models].

DocumentModelAdministrationClient

DocumentModelAdministrationClient provides operations for:

  • Building custom models to analyze specific fields you specify by labeling your custom documents. A DocumentModelDetails is returned indicating the document type(s) the model can analyze, as well as the estimated confidence for each field. See the [service documentation][fr-build-model] for a more detailed explanation.
  • Creating a composed model from a collection of existing models.
  • Managing models created in your account.
  • Listing operations or getting a specific model operation created within the last 24 hours.
  • Copying a custom model from one Form Recognizer resource to another.
  • Build and manage