Skip to content

Commit 2234074

Browse files
author
Winona Azure
committed
Initial commit, new sample
1 parent 2771a16 commit 2234074

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)