When I am running following code from azure.ai.formrecognizer import DocumentAnalysisClient
getting error as Import error : cannot import name 'DocumentAnalysisClient' from 'azure.ai.formrecognizer'
I am using azure form recongizer service and trying to import DocumentAnalyseClient.I need to resolve the error. Thank you in advance.
Make sure you're using the latest stable pip version of azure-ai-formrecognizer which is version 3.2.1 as per this Link1. And DocumentAnalysisClient was introduced in pip version 3.2.0b1 as mentioned in this Link2. So make sure your azure-ai-formrecognizer version is 3.2.0b1 or above.
I installed the latest version of azure-ai-formrecognizer and did not receive any import error with DocumentAnalysisClient, Refer below:-
pip install azure-ai-formrecognizer==3.2.0 OR pip install azure-ai-formrecognizer
Code:-
I have referred the below code from this MS Document and followed the same document.
Copied my API Key and endpoint from here:-
I have set the key and endpoint as Environment variable in my terminal like below:-
setx FR_KEY <Key> setx FR_ENDPOINT <endpoint>
Close the Terminal and VS Code and open again for above variables to populate. Now, Run the code below:-
While running the code below which I referred from this MS Document, I did not receive any Import error:-
import os from azure.ai.formrecognizer import DocumentAnalysisClient from azure.core.credentials import AzureKeyCredential # use your `key` and `endpoint` environment variables key = os.environ.get('FR_KEY') endpoint = os.environ.get('FR_ENDPOINT') # formatting function def format_polygon(polygon): if not polygon: return "N/A" return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon]) def analyze_read(): # sample form document formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/read.png" document_analysis_client = DocumentAnalysisClient( endpoint=endpoint, credential=AzureKeyCredential(key) ) poller = document_analysis_client.begin_analyze_document_from_url( "prebuilt-read", formUrl ) result = poller.result() print("Document contains content: ", result.content) for idx, style in enumerate(result.styles): print( "Document contains {} content".format( "handwritten" if style.is_handwritten else "no handwritten" ) ) for page in result.pages: print("----Analyzing Read from page #{}----".format(page.page_number)) print( "Page has width: {} and height: {}, measured with unit: {}".format( page.width, page.height, page.unit ) ) for line_idx, line in enumerate(page.lines): print( "...Line # {} has text content '{}' within bounding box '{}'".format( line_idx, line.content, format_polygon(line.polygon), ) ) for word in page.words: print( "...Word '{}' has a confidence of {}".format( word.content, word.confidence ) ) print("----------------------------------------") if __name__ == "__main__": analyze_read()
Output:-
Document Analyzed successfully:-