1+ '''
2+ References:
3+ Quickstart: https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/python-sdk
4+ SDK: https://docs.microsoft.com/en-us/python/api/overview/azure/cognitiveservices/computervision?view=azure-python
5+ '''
6+
7+ from azure .cognitiveservices .vision .computervision import ComputerVisionClient
8+ from msrest .authentication import CognitiveServicesCredentials
9+ from azure .cognitiveservices .vision .computervision .models import TextRecognitionMode
10+ from azure .cognitiveservices .vision .computervision .models import TextOperationStatusCodes
11+ import time
12+
13+ # Replace with your endpoint and key from the Azure portal
14+ endpoint = '<ADD ENDPOINT HERE>'
15+ key = '<ADD COMPUTER VISION SUBSCRIPTION KEY HERE>'
16+
17+ # Alternatively, get endpoint and key from environment variables
18+ '''
19+ import os
20+ endpoint = os.environ['ACCOUNT_ENDPOINT']
21+ key = os.environ['ACCOUNT_KEY']
22+ '''
23+
24+ # Set credentials
25+ credentials = CognitiveServicesCredentials (key )
26+
27+ # Create client
28+ client = ComputerVisionClient (endpoint , credentials )
29+
30+ url = "https://azurecomcdn.azureedge.net/cvt-1979217d3d0d31c5c87cbd991bccfee2d184b55eeb4081200012bdaf6a65601a/images/shared/cognitive-services-demos/read-text/read-1-thumbnail.png"
31+ mode = TextRecognitionMode .handwritten
32+ raw = True
33+ custom_headers = None
34+ numberOfCharsInOperationId = 36
35+
36+ # Async SDK call
37+ rawHttpResponse = client .batch_read_file (url , mode , custom_headers , raw )
38+
39+ # Get ID from returned headers
40+ operationLocation = rawHttpResponse .headers ["Operation-Location" ]
41+ idLocation = len (operationLocation ) - numberOfCharsInOperationId
42+ operationId = operationLocation [idLocation :]
43+
44+ # SDK call
45+ while True :
46+ result = client .get_read_operation_result (operationId )
47+ if result .status not in ['NotStarted' , 'Running' ]:
48+ break
49+ time .sleep (1 )
50+
51+ # Get data: text captured and its bounding box
52+ if result .status == TextOperationStatusCodes .succeeded :
53+ for textResult in result .recognition_results :
54+ for line in textResult .lines :
55+ print (line .text )
56+ print (line .bounding_box )
0 commit comments