Monday, February 17, 2025

Azure AI services | Document intelligence | Use prebuilt Document Intelligence models

Azure AI services | Document intelligence | Use prebuilt Document Intelligence models

Source: https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence

https://documentintelligence.ai.azure.com/studio

C# Code:

using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;

// dotnet add package Azure.AI.FormRecognizer --version 4.1.0

// Store connection information
string endpoint = "https://sreedocumentintelligence.cognitiveservices.azure.com/";
string apiKey = "BxcKE20FOGiN8b";

Uri fileUri = new Uri("https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence/blob
/main/Labfiles/01-prebuild-models/sample-invoice/sample-invoice.pdf?raw=true");

Console.WriteLine("\nConnecting to Forms Recognizer at: {0}", endpoint);
Console.WriteLine("Analyzing invoice at: {0}\n", fileUri.ToString());

// Create the client
var cred = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), cred);

// Analyze the invoice
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed,
"prebuilt-invoice", fileUri);


// Display invoice information to the user
AnalyzeResult result = operation.Value;

foreach (AnalyzedDocument invoice in result.Documents)
{
    if (invoice.Fields.TryGetValue("VendorName", out DocumentField? vendorNameField))
    {
        if (vendorNameField.FieldType == DocumentFieldType.String)
        {
            string vendorName = vendorNameField.Value.AsString();
            Console.WriteLine($"Vendor Name: '{vendorName}', with confidence
            {vendorNameField.Confidence}.");
        }
    }

    if (invoice.Fields.TryGetValue("CustomerName", out DocumentField? customerNameField))
    {
        if (customerNameField.FieldType == DocumentFieldType.String)
        {
            string customerName = customerNameField.Value.AsString();
            Console.WriteLine($"Customer Name: '{customerName}', with confidence
            {customerNameField.Confidence}.");
        }
    }

    if (invoice.Fields.TryGetValue("InvoiceTotal", out DocumentField? invoiceTotalField))
    {
        if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
            Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}',
            with confidence {invoiceTotalField.Confidence}.");
        }
    }
}

Console.WriteLine("\nAnalysis complete.\n");


OutPut:


Python Code:
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient

# pip install azure-ai-formrecognizer==3.3.3

# Store connection information
endpoint = "https://sreedocumentintelligence.cognitiveservices.azure.com/"
key = "BxcKE20FOGiN8b"

fileUri = "https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence/blob/main/Labfiles
/01-prebuild-models/sample-invoice/sample-invoice.pdf?raw=true"
fileLocale = "en-US"
fileModelId = "prebuilt-invoice"

print(f"\nConnecting to Forms Recognizer at: {endpoint}")
print(f"Analyzing invoice at: {fileUri}")

# Create the client
document_analysis_client = DocumentAnalysisClient(
     endpoint=endpoint, credential=AzureKeyCredential(key)
)

# Analyse the invoice
poller = document_analysis_client.begin_analyze_document_from_url(
     fileModelId, fileUri, locale=fileLocale
)

# Display invoice information to the user
receipts = poller.result()
   
for idx, receipt in enumerate(receipts.documents):
    vendor_name = receipt.fields.get("VendorName")
    if vendor_name:
        print(f"\nVendor Name: {vendor_name.value}, with confidence {vendor_name.confidence}.")

    customer_name = receipt.fields.get("CustomerName")
    if customer_name:
        print(f"Customer Name: '{customer_name.value}, with confidence {customer_name.confidence}.")


    invoice_total = receipt.fields.get("InvoiceTotal")
    if invoice_total:
        print(f"Invoice Total: '{invoice_total.value.symbol}{invoice_total.value.amount},
        with confidence {invoice_total.confidence}.")

print("\nAnalysis complete.\n")

OutPut:




3 comments:

  1. Orbit I truly appreciate how minidlna helps users create efficient home media environments by offering reliable streaming performance and lightweight server functionality that simplify multimedia access while maintaining compatibility and ease of use throughout regular entertainment and content sharing experiences across devices

    ReplyDelete
  2. Ember I genuinely like how dxwebsetup provides efficient support for updating DirectX components helping users resolve compatibility issues quickly while benefiting from a simple installation process and dependable functionality throughout gaming and multimedia setup experiences across different computer systems.

    ReplyDelete

Featured Post

Create a Dataverse Table With Every Common Column Type Using Power Automate

Create a Dataverse Table With Every Common Column Type Using Power Automate Create a Dataverse Table With Every Common Column Typ...

Popular posts