How to Add Barcode Capabilities to Azure.AI.DocumentIntelligence.AnalyzeDocumentAsync() method for scanning all Barcode

Alizain Hirani 20 Reputation points
2025-03-05T03:28:53.1266667+00:00

Hi,

I'm trying to add Barcode Feature to the AnalyzeDocumentAsync() method and its only scanning 3 barcodes on the page instead of scanning all the barcodes present. I'm using the "prebuilt-layout" as a model.

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

Accepted answer
  1. Prashanth Veeragoni 1,185 Reputation points Microsoft External Staff
    2025-03-05T07:14:16.5066667+00:00

    Hi Alizain Hirani,

    Welcome to Microsoft Q&A forum. Thank you for posting your query.

    Issue: Limited Barcode Detection in AnalyzeDocumentAsync() with "prebuilt-layout" Model

    From your description, you are using Azure AI Document Intelligence's AnalyzeDocumentAsync() method with the "prebuilt-layout" model to scan barcodes. However, the method is detecting only three barcodes per page, rather than detecting all barcodes present.

    Possible Causes of the Issue

    Model Limitation – The "prebuilt-layout" model primarily focuses on extracting text, tables, and structures but is not optimized for barcode detection. It has limited barcode scanning capabilities.

    Default Barcode Limit – Some models may have a limit on the number of barcodes they scan per page.

    Barcode Visibility Issues – Poor image quality, orientation, or contrast can affect barcode detection.

    Incorrect Model Selection – The prebuilt-layout model may not be the best for barcode scanning. Instead, Azure provides a dedicated "prebuilt-barcode" model optimized for scanning all barcodes on a document.

    Solution: Use the "prebuilt-barcode" Model Instead

    Since "prebuilt-layout" does not specialize in barcode detection, the recommended solution is to use the "prebuilt-barcode" model, which is specifically designed to extract all barcodes.

    Steps to Implement the Solution

    Modify your code to use "prebuilt-barcode" instead of __"__prebuilt-layout":

    from azure.ai.documentintelligence import DocumentIntelligenceClient
    from azure.core.credentials import AzureKeyCredential
    
    # Azure AI Document Intelligence Credentials
    endpoint = "https://your-resource-name.cognitiveservices.azure.com/"
    key = "your-api-key"
    
    # Initialize the Document Intelligence Client
    client = DocumentIntelligenceClient(endpoint, AzureKeyCredential(key))
    
    # Specify the document file path
    file_path = "path_to_your_document.pdf"
    
    # Read the document content
    with open(file_path, "rb") as f:
        document_content = f.read()
    
    # Analyze the document using the "prebuilt-barcode" model
    poller = client.begin_analyze_document("prebuilt-barcode", document_content)
    result = poller.result()
    
    # Print the detected barcodes
    for barcode in result.documents[0].fields.get("barcodes", []):
        print(f"Barcode Value: {barcode.value}")
        print(f"Barcode Type: {barcode.value_type}")
    
    

    If you must use "prebuilt-layout", try the following optimizations:

    Increase Image Quality: Ensure the document is scanned with higher resolution (300 DPI or higher) for better barcode recognition.

    Preprocess Image with OpenCV: Adjust contrast and sharpness before sending the image to Azure.

    Manually Extract More Barcodes: The response might contain a bounding box for undetected elements. Try iterating through them manually to extract more barcodes.

    Hope this helps. Do let us know if you any further queries.   

    ------------- 

    If this answers your query, do click Accept Answer and Yes for was this answer helpful.

    Thank you.


0 additional answers

Sort by: Most helpful

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.