11from azure .cognitiveservices .vision .computervision import ComputerVisionClient
22from msrest .authentication import CognitiveServicesCredentials
3- from azure .cognitiveservices .vision .computervision .models import TextRecognitionMode
4- from azure .cognitiveservices .vision .computervision .models import TextOperationStatusCodes
3+ from azure .cognitiveservices .vision .computervision .models import ReadOperationResult , OperationStatusCodes
54import time
65
76'''
2726# Create client
2827client = ComputerVisionClient (endpoint , credentials )
2928
30- url = "https://azurecomcdn.azureedge.net/cvt-1979217d3d0d31c5c87cbd991bccfee2d184b55eeb4081200012bdaf6a65601a/images/shared/cognitive-services-demos/read-text/read-1-thumbnail.png"
31- mode = TextRecognitionMode .handwritten
29+ # change this URL to reflect the image that you would like to test.
30+ url = "https://azurecomcdn.azureedge.net/cvt-181c82bceabc9fab9ec6f3dca486738800e04b45a0b3c1268609c94f4d67173a/images/shared/cognitive-services-demos/analyze-image/analyze-6-thumbnail.jpg"
31+ # image_path = "images/computer_vision_ocr.png"
32+ lang = 'en'
3233raw = True
3334custom_headers = None
34- numberOfCharsInOperationId = 36
3535
36- # Async SDK call
37- rawHttpResponse = client .batch_read_file (url , mode , custom_headers , raw )
36+ # Read an image from a url
37+ rawHttpResponse = client .read (url , language = lang , custom_headers = custom_headers , raw = raw )
38+
39+ # Uncomment the following code and comment out line 37 to read from image stream
40+ # with open(image_path, "rb") as image_stream:
41+ # rawHttpResponse = client.read_in_stream(
42+ # image=image_stream, language=lang,
43+ # # Raw will return the raw response which can be used to find the operation_id
44+ # raw=True
45+ # )
3846
3947# Get ID from returned headers
4048operationLocation = rawHttpResponse .headers ["Operation-Location" ]
41- idLocation = len (operationLocation ) - numberOfCharsInOperationId
42- operationId = operationLocation [idLocation :]
49+ operationId = operationLocation .split ('/' )[- 1 ]
4350
4451# SDK call
4552while True :
46- result = client .get_read_operation_result (operationId )
47- if result .status not in ['NotStarted' , 'Running' ]:
53+ result = client .get_read_result (operationId )
54+ if result .status not in [OperationStatusCodes . not_started , OperationStatusCodes . running ]:
4855 break
4956 time .sleep (1 )
5057
5158# Get data: displays text captured and its bounding box (position in the image)
52- if result .status == TextOperationStatusCodes .succeeded :
53- for textResult in result .recognition_results :
54- for line in textResult .lines :
59+
60+ if result .status == OperationStatusCodes .succeeded :
61+ for read_result in result .analyze_result .read_results :
62+ for line in read_result .lines :
5563 print (line .text )
5664 print (line .bounding_box )
0 commit comments