For a Batch Run with Figures Output how to download the Figures

Martin Møldrup 0 Reputation points
2025-02-18T15:32:01.91+00:00

For azure AI document intelligence, when I batch run with begin_analyze_batch_documents() using the python SDK, I would like to download the figures for each of the results in a batch, is there a way to do that?

I see then method get_analyze_batch_result() has been overwritten in a patch to take in a continuation token instead of model_id and result_id. If I call the get_analyze_result_figure() API directly, the problem is that a figure id is not unique for a result ID anymore?

Versions:
Document Intelligence v4 (api: 2024-11-30)

Python package: azure-ai-documentintelligence==1.0.0

Model ID: prebuilt-layout

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,933 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pavankumar Purilla 3,410 Reputation points Microsoft Vendor
    2025-02-18T21:17:35.3466667+00:00

    Hi Martin Møldrup,
    Greetings & Welcome to the Microsoft Q&A forum! Thank you for sharing your query.

    Based on your request, here is the updated code to analyze documents using Azure Document Intelligence and extract figures.

    
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documentintelligence import DocumentIntelligenceClient
    from azure.ai.documentintelligence.models import AnalyzeOutputOption, AnalyzeResult
    
    endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
    key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
    
    document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
    
    with open("path_to_sample_documents", "rb") as f:
        poller = document_intelligence_client.begin_analyze_batch_documents(
            model_id="prebuilt-layout",
            body=f,
            output=[AnalyzeOutputOption.FIGURES],
        )
    
    # Retrieve the continuation token
    continuation_token = poller.continuation_token()
    
    # Get the batch result using the continuation token
    batch_result = document_intelligence_client.get_analyze_batch_result(continuation_token)
    
    # Iterate through the results and download figures
    for result in batch_result.results:
        if result.figures:
            for figure in result.figures:
                if figure.id:
                    response = document_intelligence_client.get_analyze_result_figure(
                        model_id=result.model_id, result_id=result.id, figure_id=figure.id
                    )
                    with open(f"{figure.id}.png", "wb") as writer:
                        writer.write(response.read())
        else:
            print("No figures found.")
    
    print("Figures downloaded successfully.")
    

    Please try the above code and let us know if you are facing any issues.
    I hope this information helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.