From aee5062e9d63048550ca408ac296931a44ed35b4 Mon Sep 17 00:00:00 2001 From: kyichii Date: Tue, 15 Dec 2020 14:12:50 +0900 Subject: [PATCH] Changed the initial URL to a valid link in the Azure Computer Vision demo. The previous URL does not appear to be avaialble This fixes an error that was caused by the breaking API change from the python SDK commit: https://github.com/Azure/azure-sdk-for-python/commit/99668db644fe606c24cc7cb553135b6614c10ffd --- .../vision/computer_vision_extract_text.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/samples/vision/computer_vision_extract_text.py b/samples/vision/computer_vision_extract_text.py index b3ad429..865b2b9 100644 --- a/samples/vision/computer_vision_extract_text.py +++ b/samples/vision/computer_vision_extract_text.py @@ -1,7 +1,6 @@ from azure.cognitiveservices.vision.computervision import ComputerVisionClient from msrest.authentication import CognitiveServicesCredentials -from azure.cognitiveservices.vision.computervision.models import TextRecognitionMode -from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes +from azure.cognitiveservices.vision.computervision.models import ReadOperationResult, OperationStatusCodes import time ''' @@ -27,30 +26,39 @@ # Create client client = ComputerVisionClient(endpoint, credentials) -url = "https://azurecomcdn.azureedge.net/cvt-1979217d3d0d31c5c87cbd991bccfee2d184b55eeb4081200012bdaf6a65601a/images/shared/cognitive-services-demos/read-text/read-1-thumbnail.png" -mode = TextRecognitionMode.handwritten +# change this URL to reflect the image that you would like to test. +url = "https://azurecomcdn.azureedge.net/cvt-181c82bceabc9fab9ec6f3dca486738800e04b45a0b3c1268609c94f4d67173a/images/shared/cognitive-services-demos/analyze-image/analyze-6-thumbnail.jpg" +# image_path = "images/computer_vision_ocr.png" +lang = 'en' raw = True custom_headers = None -numberOfCharsInOperationId = 36 -# Async SDK call -rawHttpResponse = client.batch_read_file(url, mode, custom_headers, raw) +# Read an image from a url +rawHttpResponse = client.read(url, language=lang, custom_headers=custom_headers, raw=raw) + +# Uncomment the following code and comment out line 37 to read from image stream +# with open(image_path, "rb") as image_stream: +# rawHttpResponse = client.read_in_stream( +# image=image_stream, language=lang, +# # Raw will return the raw response which can be used to find the operation_id +# raw=True +# ) # Get ID from returned headers operationLocation = rawHttpResponse.headers["Operation-Location"] -idLocation = len(operationLocation) - numberOfCharsInOperationId -operationId = operationLocation[idLocation:] +operationId = operationLocation.split('/')[-1] # SDK call while True: - result = client.get_read_operation_result(operationId) - if result.status not in ['NotStarted', 'Running']: + result = client.get_read_result(operationId) + if result.status not in [OperationStatusCodes.not_started, OperationStatusCodes.running]: break time.sleep(1) # Get data: displays text captured and its bounding box (position in the image) -if result.status == TextOperationStatusCodes.succeeded: - for textResult in result.recognition_results: - for line in textResult.lines: + +if result.status == OperationStatusCodes.succeeded: + for read_result in result.analyze_result.read_results: + for line in read_result.lines: print(line.text) print(line.bounding_box) \ No newline at end of file