azure.ai.formrecognizer.aio package

class azure.ai.formrecognizer.aio.FormRecognizerClient(endpoint: str, credential: Union[AzureKeyCredential, AsyncTokenCredential], **kwargs: Any)[source]

FormRecognizerClient extracts information from forms and images into structured data. It is the interface to use for analyzing receipts, business cards, invoices, recognizing content/layout from forms, and analyzing custom forms from trained models. It provides different methods based on inputs from a URL and inputs from a stream.

Parameters
Keyword Arguments

api_version (str or FormRecognizerApiVersion) – The API version of the service to use for requests. It defaults to the latest service version. Setting to an older version may result in reduced feature compatibility.

Example:

Creating the FormRecognizerClient with an endpoint and API key.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_recognizer_client = FormRecognizerClient(endpoint, AzureKeyCredential(key))
Creating the FormRecognizerClient with a token credential.
"""DefaultAzureCredential will use the values from these environment
variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
"""
from azure.ai.formrecognizer.aio import FormRecognizerClient
from azure.identity.aio import DefaultAzureCredential

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

form_recognizer_client = FormRecognizerClient(endpoint, credential)
async begin_recognize_business_cards(business_card: Union[bytes, IO[bytes]], **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given business card. The input document must be of one of the supported content types - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, ‘image/tiff’ or ‘image/bmp’.

See fields found on a business card here: https://aka.ms/formrecognizer/businesscardfields

Parameters

business_card (bytes or IO[bytes]) – JPEG, PNG, PDF, TIFF, or BMP type file stream or bytes.

Keyword Arguments
  • locale (str) – Locale of the business card. Supported locales include: en-US, en-AU, en-CA, en-GB, and en-IN.

  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • content_type (str or FormContentType) – Content-type of the body sent to the API. Content-type is auto-detected, but can be overridden by passing this keyword argument. For options, see FormContentType.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The begin_recognize_business_cards client method

Example:

Recognize business cards from a file.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_recognizer_client = FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
)
async with form_recognizer_client:
    with open(path_to_sample_forms, "rb") as f:
        poller = await form_recognizer_client.begin_recognize_business_cards(business_card=f, locale="en-US")
    business_cards = await poller.result()

for idx, business_card in enumerate(business_cards):
    print("--------Recognizing business card #{}--------".format(idx+1))
    contact_names = business_card.fields.get("ContactNames")
    if contact_names:
        for contact_name in contact_names.value:
            print("Contact First Name: {} has confidence: {}".format(
                contact_name.value["FirstName"].value, contact_name.value["FirstName"].confidence
            ))
            print("Contact Last Name: {} has confidence: {}".format(
                contact_name.value["LastName"].value, contact_name.value["LastName"].confidence
            ))
    company_names = business_card.fields.get("CompanyNames")
    if company_names:
        for company_name in company_names.value:
            print("Company Name: {} has confidence: {}".format(company_name.value, company_name.confidence))
    departments = business_card.fields.get("Departments")
    if departments:
        for department in departments.value:
            print("Department: {} has confidence: {}".format(department.value, department.confidence))
    job_titles = business_card.fields.get("JobTitles")
    if job_titles:
        for job_title in job_titles.value:
            print("Job Title: {} has confidence: {}".format(job_title.value, job_title.confidence))
    emails = business_card.fields.get("Emails")
    if emails:
        for email in emails.value:
            print("Email: {} has confidence: {}".format(email.value, email.confidence))
    websites = business_card.fields.get("Websites")
    if websites:
        for website in websites.value:
            print("Website: {} has confidence: {}".format(website.value, website.confidence))
    addresses = business_card.fields.get("Addresses")
    if addresses:
        for address in addresses.value:
            print("Address: {} has confidence: {}".format(address.value, address.confidence))
    mobile_phones = business_card.fields.get("MobilePhones")
    if mobile_phones:
        for phone in mobile_phones.value:
            print("Mobile phone number: {} has confidence: {}".format(phone.value, phone.confidence))
    faxes = business_card.fields.get("Faxes")
    if faxes:
        for fax in faxes.value:
            print("Fax number: {} has confidence: {}".format(fax.value, fax.confidence))
    work_phones = business_card.fields.get("WorkPhones")
    if work_phones:
        for work_phone in work_phones.value:
            print("Work phone number: {} has confidence: {}".format(work_phone.value, work_phone.confidence))
    other_phones = business_card.fields.get("OtherPhones")
    if other_phones:
        for other_phone in other_phones.value:
            print("Other phone number: {} has confidence: {}".format(other_phone.value, other_phone.confidence))
async begin_recognize_business_cards_from_url(business_card_url: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given business card. The input document must be the location (URL) of the card to be analyzed.

See fields found on a business card here: https://aka.ms/formrecognizer/businesscardfields

Parameters

business_card_url (str) – The URL of the business card to analyze. The input must be a valid, encoded URL of one of the supported formats: JPEG, PNG, PDF, TIFF, or BMP.

Keyword Arguments
  • locale (str) – Locale of the business card. Supported locales include: en-US, en-AU, en-CA, en-GB, and en-IN.

  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The begin_recognize_business_cards_from_url client method

async begin_recognize_content(form: Union[bytes, IO[bytes]], **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.FormPage]][source]

Extract text and content/layout information from a given document. The input document must be of one of the supported content types - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, ‘image/tiff’ or ‘image/bmp’.

Parameters

form (bytes or IO[bytes]) – JPEG, PNG, PDF, TIFF, or BMP type file stream or bytes.

Keyword Arguments
  • pages (list[str]) – Custom page numbers for multi-page documents(PDF/TIFF), input the number of the pages you want to get OCR result. For a range of pages, use a hyphen. Separate each page or range with a comma.

  • language (str) – The BCP-47 language code of the text in the document. See supported language codes here: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/language-support. Content supports auto language identification and multilanguage documents, so only provide a language code if you would like to force the documented to be processed as that specific language.

  • content_type (str or FormContentType) – Content-type of the body sent to the API. Content-type is auto-detected, but can be overridden by passing this keyword argument. For options, see FormContentType.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[FormPage].

Return type

AsyncLROPoller[list[FormPage]]

Raises

HttpResponseError

New in version v2.1-preview: The pages and language keyword arguments and support for image/bmp content

Example:

Recognize text and content/layout information from a form.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

    with open(path_to_sample_forms, "rb") as f:
        poller = await form_recognizer_client.begin_recognize_content(form=f)

    form_pages = await poller.result()

    for idx, content in enumerate(form_pages):
        print("----Recognizing content from page #{}----".format(idx+1))
        print("Page has width: {} and height: {}, measured with unit: {}".format(
            content.width,
            content.height,
            content.unit
        ))
        for table_idx, table in enumerate(content.tables):
            print("Table # {} has {} rows and {} columns".format(table_idx, table.row_count, table.column_count))
            print("Table # {} location on page: {}".format(table_idx, format_bounding_box(table.bounding_box)))
            for cell in table.cells:
                print("...Cell[{}][{}] has text '{}' within bounding box '{}'".format(
                    cell.row_index,
                    cell.column_index,
                    cell.text,
                    format_bounding_box(cell.bounding_box)
                ))

        for line_idx, line in enumerate(content.lines):
            print("Line # {} has word count '{}' and text '{}' within bounding box '{}'".format(
                line_idx,
                len(line.words),
                line.text,
                format_bounding_box(line.bounding_box)
            ))
            for word in line.words:
                print("...Word '{}' has a confidence of {}".format(word.text, word.confidence))

        for selection_mark in content.selection_marks:
            print("Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
                selection_mark.state,
                format_bounding_box(selection_mark.bounding_box),
                selection_mark.confidence
            ))
        print("----------------------------------------")

async begin_recognize_content_from_url(form_url: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.FormPage]][source]

Extract text and layout information from a given document. The input document must be the location (URL) of the document to be analyzed.

Parameters

form_url (str) – The URL of the form to analyze. The input must be a valid, encoded URL of one of the supported formats: JPEG, PNG, PDF, TIFF, or BMP.

Keyword Arguments
  • pages (list[str]) – Custom page numbers for multi-page documents(PDF/TIFF), input the number of the pages you want to get OCR result. For a range of pages, use a hyphen. Separate each page or range with a comma.

  • language (str) – The BCP-47 language code of the text in the document. See supported language codes here: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/language-support. Content supports auto language identification and multilanguage documents, so only provide a language code if you would like to force the documented to be processed as that specific language.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[FormPage].

Return type

AsyncLROPoller[list[FormPage]]

Raises

HttpResponseError

New in version v2.1-preview: The pages and language keyword arguments and support for image/bmp content

async begin_recognize_custom_forms(model_id: str, form: Union[bytes, IO[bytes]], **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Analyze a custom form with a model trained with or without labels. The form to analyze should be of the same type as the forms that were used to train the model. The input document must be of one of the supported content types - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, or ‘image/tiff’.

Parameters
  • model_id (str) – Custom model identifier.

  • form (bytes or IO[bytes]) – JPEG, PNG, PDF, or TIFF type file stream or bytes.

Keyword Arguments
  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • content_type (str or FormContentType) – Content-type of the body sent to the API. Content-type is auto-detected, but can be overridden by passing this keyword argument. For options, see FormContentType.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]

Raises

HttpResponseError

Example:

Recognize fields and values from a custom form.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

async with FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

    # Make sure your form's type is included in the list of form types the custom model can recognize
    with open(path_to_sample_forms, "rb") as f:
        poller = await form_recognizer_client.begin_recognize_custom_forms(
            model_id=model_id, form=f
        )
    forms = await poller.result()

    for idx, form in enumerate(forms):
        print("--------Recognizing Form #{}--------".format(idx+1))
        print("Form has type {}".format(form.form_type))
        print("Form has form type confidence {}".format(form.form_type_confidence))
        print("Form was analyzed with model with ID {}".format(form.model_id))
        for name, field in form.fields.items():
            # each field is of type FormField
            # label_data is populated if you are using a model trained without labels,
            # since the service needs to make predictions for labels if not explicitly given to it.
            if field.label_data:
                print("...Field '{}' has label '{}' with a confidence score of {}".format(
                    name,
                    field.label_data.text,
                    field.confidence
                ))

            print("...Label '{}' has value '{}' with a confidence score of {}".format(
                field.label_data.text if field.label_data else name, field.value, field.confidence
            ))

        print("-----------------------------------")
async begin_recognize_custom_forms_from_url(model_id: str, form_url: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Analyze a custom form with a model trained with or without labels. The form to analyze should be of the same type as the forms that were used to train the model. The input document must be the location (URL) of the document to be analyzed.

Parameters
  • model_id (str) – Custom model identifier.

  • form_url (str) – The URL of the form to analyze. The input must be a valid, encoded URL of one of the supported formats: JPEG, PNG, PDF, or TIFF.

Keyword Arguments
  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]

Raises

HttpResponseError

async begin_recognize_invoices(invoice: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given invoice. The input document must be of one of the supported content types - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, ‘image/tiff’ or ‘image/bmp’.

See fields found on a invoice here: https://aka.ms/formrecognizer/invoicefields

Parameters

invoice (bytes or IO[bytes]) – JPEG, PNG, PDF, TIFF, or BMP type file stream or bytes.

Keyword Arguments
  • locale (str) – Locale of the invoice. Supported locales include: en-US

  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • content_type (str or FormContentType) – Content-type of the body sent to the API. Content-type is auto-detected, but can be overridden by passing this keyword argument. For options, see FormContentType.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The begin_recognize_invoices client method

Example:

Recognize invoices from a file.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:
    with open(path_to_sample_forms, "rb") as f:
        poller = await form_recognizer_client.begin_recognize_invoices(invoice=f, locale="en-US")
    invoices = await poller.result()

    for idx, invoice in enumerate(invoices):
        print("--------Recognizing invoice #{}--------".format(idx+1))
        vendor_name = invoice.fields.get("VendorName")
        if vendor_name:
            print("Vendor Name: {} has confidence: {}".format(vendor_name.value, vendor_name.confidence))
        vendor_address = invoice.fields.get("VendorAddress")
        if vendor_address:
            print("Vendor Address: {} has confidence: {}".format(vendor_address.value, vendor_address.confidence))
        customer_name = invoice.fields.get("CustomerName")
        if customer_name:
            print("Customer Name: {} has confidence: {}".format(customer_name.value, customer_name.confidence))
        customer_address = invoice.fields.get("CustomerAddress")
        if customer_address:
            print("Customer Address: {} has confidence: {}".format(customer_address.value, customer_address.confidence))
        customer_address_recipient = invoice.fields.get("CustomerAddressRecipient")
        if customer_address_recipient:
            print("Customer Address Recipient: {} has confidence: {}".format(customer_address_recipient.value, customer_address_recipient.confidence))
        invoice_id = invoice.fields.get("InvoiceId")
        if invoice_id:
            print("Invoice Id: {} has confidence: {}".format(invoice_id.value, invoice_id.confidence))
        invoice_date = invoice.fields.get("InvoiceDate")
        if invoice_date:
            print("Invoice Date: {} has confidence: {}".format(invoice_date.value, invoice_date.confidence))
        invoice_total = invoice.fields.get("InvoiceTotal")
        if invoice_total:
            print("Invoice Total: {} has confidence: {}".format(invoice_total.value, invoice_total.confidence))
        due_date = invoice.fields.get("DueDate")
        if due_date:
            print("Due Date: {} has confidence: {}".format(due_date.value, due_date.confidence))
async begin_recognize_invoices_from_url(invoice_url: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given invoice. The input document must be the location (URL) of the invoice to be analyzed.

See fields found on a invoice card here: https://aka.ms/formrecognizer/invoicefields

Parameters

invoice_url (str) – The URL of the invoice to analyze. The input must be a valid, encoded URL of one of the supported formats: JPEG, PNG, PDF, TIFF, or BMP.

Keyword Arguments
  • locale (str) – Locale of the invoice. Supported locales include: en-US

  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The begin_recognize_invoices_from_url client method

async begin_recognize_receipts(receipt: Union[bytes, IO[bytes]], **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given sales receipt. The input document must be of one of the supported content types - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, ‘image/tiff’ or ‘image/bmp’.

See fields found on a receipt here: https://aka.ms/formrecognizer/receiptfields

Parameters

receipt (bytes or IO[bytes]) – JPEG, PNG, PDF, TIFF, or BMP type file stream or bytes.

Keyword Arguments
  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • content_type (str or FormContentType) – Content-type of the body sent to the API. Content-type is auto-detected, but can be overridden by passing this keyword argument. For options, see FormContentType.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

  • locale (str) – Locale of the receipt. Supported locales include: en-US, en-AU, en-CA, en-GB, and en-IN.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The locale keyword argument and support for image/bmp content

Example:

Recognize sales receipt fields.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

    with open(path_to_sample_forms, "rb") as f:
        poller = await form_recognizer_client.begin_recognize_receipts(receipt=f, locale="en-US")

    receipts = await poller.result()

    for idx, receipt in enumerate(receipts):
        print("--------Recognizing receipt #{}--------".format(idx+1))
        receipt_type = receipt.fields.get("ReceiptType")
        if receipt_type:
            print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence))
        merchant_name = receipt.fields.get("MerchantName")
        if merchant_name:
            print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence))
        transaction_date = receipt.fields.get("TransactionDate")
        if transaction_date:
            print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
        print("Receipt items:")
        for idx, item in enumerate(receipt.fields.get("Items").value):
            print("...Item #{}".format(idx+1))
            item_name = item.value.get("Name")
            if item_name:
                print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
            item_quantity = item.value.get("Quantity")
            if item_quantity:
                print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
            item_price = item.value.get("Price")
            if item_price:
                print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
            item_total_price = item.value.get("TotalPrice")
            if item_total_price:
                print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
        subtotal = receipt.fields.get("Subtotal")
        if subtotal:
            print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))
        tax = receipt.fields.get("Tax")
        if tax:
            print("Tax: {} has confidence: {}".format(tax.value, tax.confidence))
        tip = receipt.fields.get("Tip")
        if tip:
            print("Tip: {} has confidence: {}".format(tip.value, tip.confidence))
        total = receipt.fields.get("Total")
        if total:
            print("Total: {} has confidence: {}".format(total.value, total.confidence))
        print("--------------------------------------")
async begin_recognize_receipts_from_url(receipt_url: str, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[List[azure.ai.formrecognizer._models.RecognizedForm]][source]

Extract field text and semantic values from a given sales receipt. The input document must be the location (URL) of the receipt to be analyzed.

See fields found on a receipt here: https://aka.ms/formrecognizer/receiptfields

Parameters

receipt_url (str) – The URL of the receipt to analyze. The input must be a valid, encoded URL of one of the supported formats: JPEG, PNG, PDF, TIFF, or BMP.

Keyword Arguments
  • include_field_elements (bool) – Whether or not to include all lines per page and field elements such as lines, words, and selection marks for each form field.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

  • locale (str) – Locale of the receipt. Supported locales include: en-US, en-AU, en-CA, en-GB, and en-IN.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a list[RecognizedForm].

Return type

AsyncLROPoller[list[RecognizedForm]]

Raises

HttpResponseError

New in version v2.1-preview: The locale keyword argument and support for image/bmp content

Example:

Recognize sales receipt fields from a URL.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:
    url = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"
    poller = await form_recognizer_client.begin_recognize_receipts_from_url(receipt_url=url)
    receipts = await poller.result()

    for idx, receipt in enumerate(receipts):
        print("--------Recognizing receipt #{}--------".format(idx+1))
        receipt_type = receipt.fields.get("ReceiptType")
        if receipt_type:
            print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence))
        merchant_name = receipt.fields.get("MerchantName")
        if merchant_name:
            print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence))
        transaction_date = receipt.fields.get("TransactionDate")
        if transaction_date:
            print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
        print("Receipt items:")
        for idx, item in enumerate(receipt.fields.get("Items").value):
            print("...Item #{}".format(idx+1))
            item_name = item.value.get("Name")
            if item_name:
                print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
            item_quantity = item.value.get("Quantity")
            if item_quantity:
                print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
            item_price = item.value.get("Price")
            if item_price:
                print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
            item_total_price = item.value.get("TotalPrice")
            if item_total_price:
                print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
        subtotal = receipt.fields.get("Subtotal")
        if subtotal:
            print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))
        tax = receipt.fields.get("Tax")
        if tax:
            print("Tax: {} has confidence: {}".format(tax.value, tax.confidence))
        tip = receipt.fields.get("Tip")
        if tip:
            print("Tip: {} has confidence: {}".format(tip.value, tip.confidence))
        total = receipt.fields.get("Total")
        if total:
            print("Total: {} has confidence: {}".format(total.value, total.confidence))
        print("--------------------------------------")
async close()None[source]

Close the FormRecognizerClient session.

class azure.ai.formrecognizer.aio.FormTrainingClient(endpoint: str, credential: Union[AzureKeyCredential, AsyncTokenCredential], **kwargs: Any)[source]

FormTrainingClient is the Form Recognizer interface to use for creating and managing custom models. It provides methods for training models on the forms you provide, as well as methods for viewing and deleting models, accessing account properties, copying models to another Form Recognizer resource, and composing models from a collection of existing models trained with labels.

Parameters
Keyword Arguments

api_version (str or FormRecognizerApiVersion) – The API version of the service to use for requests. It defaults to the latest service version. Setting to an older version may result in reduced feature compatibility.

Example:

Creating the FormTrainingClient with an endpoint and API key.
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormTrainingClient
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_training_client = FormTrainingClient(endpoint, AzureKeyCredential(key))
Creating the FormTrainingClient with a token credential.
"""DefaultAzureCredential will use the values from these environment
variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
"""
from azure.ai.formrecognizer.aio import FormTrainingClient
from azure.identity.aio import DefaultAzureCredential

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

form_training_client = FormTrainingClient(endpoint, credential)
async begin_copy_model(model_id: str, target: dict, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[azure.ai.formrecognizer._models.CustomFormModelInfo][source]

Copy a custom model stored in this resource (the source) to the user specified target Form Recognizer resource. This should be called with the source Form Recognizer resource (with the model that is intended to be copied). The target parameter should be supplied from the target resource’s output from calling the get_copy_authorization() method.

Parameters
  • model_id (str) – Model identifier of the model to copy to target resource.

  • target (dict) – The copy authorization generated from the target resource’s call to get_copy_authorization().

Keyword Arguments
  • polling_interval (int) – Default waiting time between two polls for LRO operations if no Retry-After header is present.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a CustomFormModelInfo.

Return type

AsyncLROPoller[CustomFormModelInfo]

Raises

HttpResponseError

Example:

Copy a model from the source resource to the target resource
source_client = FormTrainingClient(endpoint=source_endpoint, credential=AzureKeyCredential(source_key))

async with source_client:
    poller = await source_client.begin_copy_model(
        model_id=source_model_id,
        target=target  # output from target client's call to get_copy_authorization()
    )
    copied_over_model = await poller.result()

    print("Model ID: {}".format(copied_over_model.model_id))
    print("Status: {}".format(copied_over_model.status))
async begin_create_composed_model(model_ids: List[str], **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[azure.ai.formrecognizer._models.CustomFormModel][source]

Creates a composed model from a collection of existing models that were trained with labels.

A composed model allows multiple models to be called with a single model ID. When a document is submitted to be analyzed with a composed model ID, a classification step is first performed to route it to the correct custom model

Parameters

model_ids (list[str]) – List of model IDs to use in the composed model.

Keyword Arguments
  • model_name (str) – An optional, user-defined name to associate with your model.

  • polling_interval (int) – Default waiting time between two polls for LRO operations if no Retry-After header is present.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a CustomFormModel.

Return type

AsyncLROPoller[CustomFormModel]

Raises

HttpResponseError

New in version v2.1-preview: The begin_create_composed_model client method

Example:

Create a composed model
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormTrainingClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
po_supplies = os.environ['PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL']
po_equipment = os.environ['PURCHASE_ORDER_OFFICE_EQUIPMENT_SAS_URL']
po_furniture = os.environ['PURCHASE_ORDER_OFFICE_FURNITURE_SAS_URL']
po_cleaning_supplies = os.environ['PURCHASE_ORDER_OFFICE_CLEANING_SUPPLIES_SAS_URL']

form_training_client = FormTrainingClient(endpoint=endpoint, credential=AzureKeyCredential(key))
supplies_poller = await form_training_client.begin_training(
    po_supplies, use_training_labels=True, model_name="Purchase order - Office supplies"
)
equipment_poller = await form_training_client.begin_training(
    po_equipment, use_training_labels=True, model_name="Purchase order - Office Equipment"
)
furniture_poller = await form_training_client.begin_training(
    po_furniture, use_training_labels=True, model_name="Purchase order - Furniture"
)
cleaning_supplies_poller = await form_training_client.begin_training(
    po_cleaning_supplies, use_training_labels=True, model_name="Purchase order - Cleaning Supplies"
)
supplies_model = await supplies_poller.result()
equipment_model = await equipment_poller.result()
furniture_model = await furniture_poller.result()
cleaning_supplies_model = await cleaning_supplies_poller.result()

models_trained_with_labels = [
    supplies_model.model_id,
    equipment_model.model_id,
    furniture_model.model_id,
    cleaning_supplies_model.model_id
]

poller = await form_training_client.begin_create_composed_model(
    models_trained_with_labels, model_name="Office Supplies Composed Model"
)
model = await poller.result()

print("Office Supplies Composed Model Info:")
print("Model ID: {}".format(model.model_id))
print("Model name: {}".format(model.model_name))
print("Is this a composed model?: {}".format(model.properties.is_composed_model))
print("Status: {}".format(model.status))
print("Composed model creation started on: {}".format(model.training_started_on))
print("Creation completed on: {}".format(model.training_completed_on))

async begin_training(training_files_url: str, use_training_labels: bool, **kwargs: Any) → azure.core.polling._async_poller.AsyncLROPoller[azure.ai.formrecognizer._models.CustomFormModel][source]

Create and train a custom model. The request must include a training_files_url parameter that is an externally accessible Azure storage blob container URI (preferably a Shared Access Signature URI). Note that a container URI (without SAS) is accepted only when the container is public. Models are trained using documents that are of the following content type - ‘application/pdf’, ‘image/jpeg’, ‘image/png’, ‘image/tiff’. Other types of content in the container is ignored.

Parameters
Keyword Arguments
  • prefix (str) – A case-sensitive prefix string to filter documents in the source path for training. For example, when using a Azure storage blob URI, use the prefix to restrict sub folders for training.

  • include_subfolders (bool) – A flag to indicate if subfolders within the set of prefix folders will also need to be included when searching for content to be preprocessed. Not supported if training with labels.

  • model_name (str) – An optional, user-defined name to associate with your model.

  • polling_interval (int) – Waiting time between two polls for LRO operations if no Retry-After header is present. Defaults to 5 seconds.

  • continuation_token (str) – A continuation token to restart a poller from a saved state.

Returns

An instance of an AsyncLROPoller. Call result() on the poller object to return a CustomFormModel.

Return type

AsyncLROPoller[CustomFormModel]

Raises

HttpResponseError – Note that if the training fails, the exception is raised, but a model with an “invalid” status is still created. You can delete this model by calling delete_model()

New in version v2.1-preview: The model_name keyword argument

Example:

Training a model (without labels) with your custom forms.
from azure.ai.formrecognizer.aio import FormTrainingClient
from azure.core.credentials import AzureKeyCredential

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]

async with FormTrainingClient(
    endpoint, AzureKeyCredential(key)
) as form_training_client:

    poller = await form_training_client.begin_training(container_sas_url, use_training_labels=False)
    model = await poller.result()

    # Custom model information
    print("Model ID: {}".format(model.model_id))
    print("Status: {}".format(model.status))
    print("Model name: {}".format(model.model_name))
    print("Training started on: {}".format(model.training_started_on))
    print("Training completed on: {}".format(model.training_completed_on))

    print("Recognized fields:")
    # Looping through the submodels, which contains the fields they were trained on
    for submodel in model.submodels:
        print("...The submodel has form type '{}'".format(submodel.form_type))
        for name, field in submodel.fields.items():
            print("...The model found field '{}' to have label '{}'".format(
                name, field.label
            ))
async close()None[source]

Close the FormTrainingClient session.

async delete_model(model_id: str, **kwargs: Any)None[source]

Mark model for deletion. Model artifacts will be permanently removed within a predetermined period.

Parameters

model_id (str) – Model identifier.

Return type

None

Raises

HttpResponseError or ResourceNotFoundError

Example:

Delete a custom model.
await form_training_client.delete_model(model_id=custom_model.model_id)

try:
    await form_training_client.get_custom_model(model_id=custom_model.model_id)
except ResourceNotFoundError:
    print("Successfully deleted model with id {}".format(custom_model.model_id))
async get_account_properties(**kwargs: Any) → azure.ai.formrecognizer._models.AccountProperties[source]

Get information about the models on the form recognizer account.

Returns

Summary of models on account - custom model count, custom model limit.

Return type

AccountProperties

Raises

HttpResponseError

Example:

Get properties for the form recognizer account.
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import ResourceNotFoundError
from azure.ai.formrecognizer.aio import FormTrainingClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormTrainingClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_training_client:
    # First, we see how many custom models we have, and what our limit is
    account_properties = await form_training_client.get_account_properties()
    print("Our account has {} custom models, and we can have at most {} custom models\n".format(
        account_properties.custom_model_count, account_properties.custom_model_limit
    ))
async get_copy_authorization(resource_id: str, resource_region: str, **kwargs: Any) → Dict[str, Union[str, int]][source]

Generate authorization for copying a custom model into the target Form Recognizer resource. This should be called by the target resource (where the model will be copied to) and the output can be passed as the target parameter into begin_copy_model().

Parameters
Returns

A dictionary with values for the copy authorization - “modelId”, “accessToken”, “resourceId”, “resourceRegion”, and “expirationDateTimeTicks”.

Return type

Dict[str, Union[str, int]]

Raises

HttpResponseError

Example:

Authorize the target resource to receive the copied model
target_client = FormTrainingClient(endpoint=target_endpoint, credential=AzureKeyCredential(target_key))

async with target_client:
    target = await target_client.get_copy_authorization(
        resource_region=target_region,
        resource_id=target_resource_id
    )
# model ID that target client will use to access the model once copy is complete
print("Model ID: {}".format(target["modelId"]))
async get_custom_model(model_id: str, **kwargs: Any) → azure.ai.formrecognizer._models.CustomFormModel[source]

Get a description of a custom model, including the types of forms it can recognize, and the fields it will extract for each form type.

Parameters

model_id (str) – Model identifier.

Returns

CustomFormModel

Return type

CustomFormModel

Raises

HttpResponseError or ResourceNotFoundError

Example:

Get a custom model with a model ID.
custom_model = await form_training_client.get_custom_model(model_id=first_model.model_id)
print("\nModel ID: {}".format(custom_model.model_id))
print("Status: {}".format(custom_model.status))
print("Model name: {}".format(custom_model.model_name))
print("Is this a composed model?: {}".format(custom_model.properties.is_composed_model))
print("Training started on: {}".format(custom_model.training_started_on))
print("Training completed on: {}".format(custom_model.training_completed_on))
get_form_recognizer_client(**kwargs: Any) → azure.ai.formrecognizer.aio._form_recognizer_client_async.FormRecognizerClient[source]

Get an instance of a FormRecognizerClient from FormTrainingClient.

Return type

FormRecognizerClient

Returns

A FormRecognizerClient

list_custom_models(**kwargs: Any)azure.core.async_paging.AsyncItemPaged[azure.ai.formrecognizer._models.CustomFormModelInfo][source]

List information for each model, including model id, model status, and when it was created and last modified.

Returns

AsyncItemPaged[CustomFormModelInfo]

Return type

AsyncItemPaged

Raises

HttpResponseError

Example:

List model information for each model on the account.
custom_models = form_training_client.list_custom_models()

print("We have models with the following IDs:")

# Let's pull out the first model
first_model = await custom_models.__anext__()
print(first_model.model_id)
async for model in custom_models:
    print(model.model_id)