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.