|

|  How to Use Microsoft Azure Form Recognizer API in .NET

How to Use Microsoft Azure Form Recognizer API in .NET

October 31, 2024

Unlock insights with Microsoft Azure Form Recognizer API in .NET. Learn step-by-step integration for accurate, efficient document data extraction.

How to Use Microsoft Azure Form Recognizer API in .NET

 

Getting Started with Azure Form Recognizer API

 

  • Before diving into the code, ensure that you have set up your Azure Form Recognizer resources, and obtained the endpoint and API key from the Azure portal.
  •  

  • Make sure you have installed the Azure.AI.FormRecognizer nuget package in your .NET project to help interact with the API.
  •  

  • You'll typically work with the FormRecognizerClient class, which provides methods to analyze documents.

 

dotnet add package Azure.AI.FormRecognizer

 

Sample Configuration and Initialization

 

  • Initialize the FormRecognizerClient using the endpoint and API key obtained from your Azure account.
  •  

  • You'll usually initialize this client in a part of your application responsible for handling dependency injection or service initialization.

 

using Azure;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.DocumentAnalysis;
using System;
using System.IO;
using System.Threading.Tasks;

namespace FormRecognizerExample
{
    public class FormRecognizerService
    {
        private readonly FormRecognizerClient _formRecognizerClient;

        public FormRecognizerService(string endpoint, string apiKey)
        {
            var credential = new AzureKeyCredential(apiKey);
            _formRecognizerClient = new FormRecognizerClient(new Uri(endpoint), credential);
        }
    }
}

 

Analyzing Forms with Form Recognizer

 

  • To analyze forms, you'll often use the StartRecognizeContentAsync method for basic text extraction or the StartRecognizeCustomFormsAsync for more complex form processing.
  •  

  • For custom form models, make sure you have a form model ID from the forms you have trained previously in Azure.

 

public async Task AnalyzeFormAsync(string filePath)
{
    using var stream = new FileStream(filePath, FileMode.Open);

    var operation = await _formRecognizerClient.StartRecognizeContentAsync(stream);
    var result = await operation.WaitForCompletionAsync();

    foreach (var page in result.Value)
    {
        Console.WriteLine($"Page number: {page.PageNumber}");

        foreach (var table in page.Tables)
        {
            Console.WriteLine("Table data:");

            foreach (var cell in table.Cells)
            {
                Console.WriteLine($"Cell text: '{cell.Content}'");
            }
        }
    }
}

 

Handling Models and Training Data

 

  • Form Recognizer allows training models on your custom forms. This involves uploading training data and labeling attributes that the models will learn to recognize.
  •  

  • Make sure your training data is clean and well-organized to improve the accuracy of the form recognizer model.

 

public async Task TrainModelAsync(string trainingDataUrl)
{
    var options = new BuildModelOptions { };
    var operation = await _formRecognizerClient.StartBuildModelAsync(trainingDataUrl, options);
    var customFormModel = await operation.WaitForCompletionAsync();

    Console.WriteLine($"Model ID: {customFormModel.Value.ModelId}");
}

 

Handling Errors and Debugging

 

  • When working with live APIs, error handling is crucial. Use try-catch blocks and inspect exceptions for better reliability and debugging.
  •  

  • Azure's SDK provides extensive logging capabilities which can help debug and identify issues in the interaction with the API.

 

try
{
    await AnalyzeFormAsync("path/to/your/document.pdf");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

 

Optimizing Performance

 

  • Consider optimizing network calls by batching requests or using asynchronous operations, as shown in the examples, to maximize application responsiveness.
  •  

  • Keep models updated with new data samples to improve accuracy and account for data drift over time.

Pre-order Friend AI Necklace

Pre-Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

OMI AI PLATFORM
Remember Every Moment,
Talk to AI and Get Feedback

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI Necklace

$69.99

Make your life more fun with your AI wearable clone. It gives you thoughts, personalized feedback and becomes your second brain to discuss your thoughts and feelings. Available on iOS and Android.

Your Omi will seamlessly sync with your existing omi persona, giving you a full clone of yourself – with limitless potential for use cases:

  • Real-time conversation transcription and processing;
  • Develop your own use cases for fun and productivity;
  • Hundreds of community apps to make use of your Omi Persona and conversations.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

team@basedhardware.com

company

about

careers

invest

privacy

products

omi

omi dev kit

personas

resources

apps

affiliate

docs

github

help