Search code examples
c#azureazure-form-recognizer

Azure Form Recognizer Set API Version


I am trying to use the Az Form Recognizer SDK version 4.1.0-beta.1 and the API version "2023_02_28_preview".

The documentation reads that I can use the DocumentAnalysisClient, but it doesnt show how to set the API version to the desired version. Any examples on how to do that?


Solution

  • You cannot specify the Form recognizer API version in your C# code, Because while using C# SDK for form recognizer, By default it will use the Form Recognizer REST API version 3.0 or use 4.1.0-beta.1 by default depending on the Nuget package version you have imported.

    Refer the Note which states Important in this MS Document
    Important
    This project targets Form Recognizer REST API version
    3.0
    and Refer the Note below from this link.
    Note: This version of the client library defaults to the 2022-08-31 version of the service.

    You can select your required API version while importing the Form Recognizer Nuget package in Visual Studio like below:-

    enter image description here

    After importing the above Azure.AI.FormRecognizer Nuget package with desired API version you do not need to specifically mention it again in your code.

    Now, you can refer the code from this MS Document and run the sample code to manage form recognizer resource.

    Note:- above document uses API version 4.0.0 as mentioned - Select version 4.0.0 from the dropdown menu and install the package in your project.

    Code:-

    
    using Azure; using Azure.AI.FormRecognizer.DocumentAnalysis;
    
    //use your `key` and `endpoint` environment variables to create your
    `AzureKeyCredential` and `DocumentAnalysisClient` instances string key
    = "<api-key>"; string endpoint = "https://siliconformrecgnizer123.cognitiveservices.azure.com/";
    AzureKeyCredential credential = new AzureKeyCredential(key);
    DocumentAnalysisClient client = new DocumentAnalysisClient(new
    Uri(endpoint), credential);
    
    //sample document Uri fileUri = new
    Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/read.png");
    
    AnalyzeDocumentOperation operation = await
    client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed,
    "prebuilt-read", fileUri); AnalyzeResult result = operation.Value;
    
    foreach (DocumentPage page in result.Pages) {
        Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
        Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");
    
        for (int i = 0; i < page.Lines.Count; i++)
        {
            DocumentLine line = page.Lines[i];
            Console.WriteLine($"  Line {i} has content: '{line.Content}'.");
    
            Console.WriteLine($"    Its bounding polygon (points ordered clockwise):");
    
            for (int j = 0; j < line.BoundingPolygon.Count; j++)
            {
                Console.WriteLine($"      Point {j} => X: {line.BoundingPolygon[j].X}, Y: {line.BoundingPolygon[j].Y}");
            }
        } }
    
    foreach (DocumentStyle style in result.Styles) {
        // Check the style and style confidence to see if text is handwritten.
        // Note that value '0.8' is used as an example.
    
        bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;
    
        if (isHandwritten && style.Confidence > 0.8)
        {
            Console.WriteLine($"Handwritten content found:");
    
            foreach (DocumentSpan span in style.Spans)
            {
                Console.WriteLine($"  Content: {result.Content.Substring(span.Index, span.Length)}");
            }
        } }
    
    Console.WriteLine("Detected languages:");
    
    foreach (DocumentLanguage language in result.Languages) {
        Console.WriteLine($"  Found language with locale'{language.Locale}' with confidence {language.Confidence}."); }
    
    
    

    Output:-

    enter image description here

    Alternatively, You can directly call the API by going to this link or this link and passing the require parameters like below:-

    enter image description here

    enter image description here

    Note- Form Recognizer 2023-02-28-preview is still in preview and not Generally available so some features might not yet work completely.

    You can also Specify API version in Form recognizer studio while creating your model like below:-

    enter image description here