Azure / MachineLearningNotebooks

Python notebooks with ML and deep learning examples with Azure Machine Learning Python SDK | Microsoft
https://docs.microsoft.com/azure/machine-learning/service/
MIT License
4.07k stars 2.52k forks source link

'ClientRequestError' when trying to use Azure Computer Vision API from Azure Machine Learning Notebook #1501

Closed imbachb closed 3 years ago

imbachb commented 3 years ago

I'm trying to use Azure Computer Vision's OCR API in an Azure Machine Learning Notebook. However there seems to be an error when trying to call the Computer Vision API from an Azure Machine Learning Notebook. The same code works when I'm running it on a local machine. I'm following Azure Computer Vision's OCR Quickstart: https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-python

When running the following code, computervision_client.read(read_image_url, raw=True) does not return but throws an exception. Exception: ClientRequestError: Error occurred in request., ConnectionError: HTTPSConnectionPool(host='some-host.cognitiveservices.azure.com', port=443): Max retries exceeded with url: /vision/v3.2/read/analyze?model-version=latest&readingOrder=basic (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f59e0102820>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

Code:

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

from array import array
import os
from PIL import Image
import sys
import time

'''
Authenticate
Authenticates your credentials and creates a client.
'''
subscription_key = os.environ["COMPUTERVISION_KEY"]
endpoint = os.environ["COMPUTERVISION_URL"]

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

'''
OCR: Read File using the Read API, extract text - remote
This example will extract text in an image, then print results, line by line.
This API call can also extract handwriting style text (not shown).
'''
print("===== Read File - remote =====")
# Get an image with text
read_image_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"

# Call API with URL and raw response (allows you to get the operation location)
read_response = computervision_client.read(read_image_url,  raw=True) # <- THROWS EXCEPTION

Used azure packages:

azure-ai-textanalytics                        5.1.0b7
azure-cognitiveservices-vision-computervision 0.9.0
azure-common                                  1.1.27
azure-core                                    1.14.0
azure-cosmos                                  4.2.0
azure-graphrbac                               0.61.1
azure-identity                                1.4.1
azure-mgmt-authorization                      0.61.0
azure-mgmt-containerregistry                  8.0.0
azure-mgmt-core                               1.2.2
azure-mgmt-keyvault                           2.2.0
azure-mgmt-resource                           13.0.0
azure-mgmt-storage                            11.2.0
azure-storage-blob                            12.8.0
azureml-automl-core                           1.29.0
azureml-contrib-dataset                       1.29.0
azureml-core                                  1.29.0.post1
azureml-dataprep                              2.15.1
azureml-dataprep-native                       33.0.0
azureml-dataprep-rslex                        1.13.0
azureml-dataset-runtime                       1.29.0
azureml-pipeline-core                         1.29.0
azureml-pipeline-steps                        1.29.0
azureml-telemetry                             1.29.0
azureml-train-automl-client                   1.29.0
azureml-train-core                            1.29.0
azureml-train-restclients-hyperdrive          1.29.0
azureml-widgets                               1.29.0.post1

Maybe related to #1107

imbachb commented 3 years ago

After a long while I've tried the exact same script in the same compute instance again. I don't experience the connection error anymore so I will close this ticket. I don't know what changed.