pythonazuremicrosoft-custom-vision

Getting KeyError: 'Endpoint' error in Python when calling Custom Vision API


from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.customvision import prediction
from PIL import Image

endpoint = "https://southcentralus.api.cognitive.microsoft.com/"
project_id = "projectidhere"
prediction_key = "predictionkeyhere"

predict = CustomVisionPredictionClient(prediction_key, endpoint)

with open("c:/users/paul.barbin/pycharmprojects/hw3/TallowTest1.jpg", mode="rb") as image_data:
    tallowresult = predict.detect_image(project_id, "test1", image_data)

Python 3.7, and I'm using Azure Custom Vision 3.1? (>azure.cognitiveservices.vision.customvision) (3.1.0)

Note that I've seen the same question on SO but no real solution. The posted answer on the other question says to use the REST API instead.

I believe the error is in the endpoint (as stated in the error), and I've tried a few variants - with the slash, without, using an environment variable, without, I've tried appending various strings to my endpoint but I keep getting the same message. Any help is appreciated.

Full error here:

Traceback (most recent call last):
  File "GetError.py", line 15, in <module>
    tallowresult = predict.detect_image(project_id, "test1", image_data)
  File "C:\Users\paul.barbin\PycharmProjects\hw3\.venv\lib\site-packages\azure\cognitiveservices\vision\customvision\prediction\operations\_custom_vision_
prediction_client_operations.py", line 354, in detect_image
    request = self._client.post(url, query_parameters, header_parameters, form_content=form_data_content)
  File "C:\Users\paul.barbin\PycharmProjects\hw3\.venv\lib\site-packages\msrest\service_client.py", line 193, in post
    request = self._request('POST', url, params, headers, content, form_content)
  File "C:\Users\paul.barbin\PycharmProjects\hw3\.venv\lib\site-packages\msrest\service_client.py", line 108, in _request
    request = ClientRequest(method, self.format_url(url))
  File "C:\Users\paul.barbin\PycharmProjects\hw3\.venv\lib\site-packages\msrest\service_client.py", line 155, in format_url
    base = self.config.base_url.format(**kwargs).rstrip('/')
KeyError: 'Endpoint'

Solution

  • CustomVisionPredictionClient takes two required, positional parameters: endpoint and credentials. Endpoint needs to be passed in before credentials, try swapping the order:

    predict = CustomVisionPredictionClient(endpoint, prediction_key)