Azure-Samples / cognitive-services-quickstart-code

Code Examples used by the Quickstarts in the Cognitive Services Documentation
MIT License
346 stars 513 forks source link

CustomVisionQuickstart.py uses wrong endpoint for predictor client config #295

Open ghost opened 2 years ago

ghost commented 2 years ago

Please provide us with the following information:

This issue is for a: (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce

Run "cognitive-services-quickstart-code/python/CustomVision/ObjectDetection/CustomVisionQuickstart.py" with following info:

ENDPOINT = "TRAINING_RESOURCE_ENDPOINT"
training_key = "TRAINING_RESOURCE_API_KEY"
prediction_key = "PREDICTION_API_KEY"
prediction_resource_id = "PREDICTION_RESOURCE_ID"

Any log messages given by the failure

CustomVisionErrorException                Traceback (most recent call last)
Input In [29], in <module>
      1 with open(os.path.join (base_image_location, "test", "test_image.jpg"), mode="rb") as test_data:
----> 2     results = predictor.detect_image(project.id, publish_iteration_name, test_data)

File c:\...\venv\lib\site-packages\azure\cognitiveservices\vision\customvision\prediction\operations\_custom_vision_prediction_client_operations.py:358, in CustomVisionPredictionClientOperationsMixin.detect_image(self, project_id, published_name, image_data, application, custom_headers, raw, **operation_config)
    355 response = self._client.send(request, stream=False, **operation_config)
    357 if response.status_code not in [200]:
--> 358     raise models.CustomVisionErrorException(self._deserialize, response)
    360 deserialized = None
    361 if response.status_code == 200:

CustomVisionErrorException: Operation returned an invalid status code 'Unauthorized'

Expected/desired behavior

Returned results

OS and Version?

Windows 11 Enterprise

Versions

21H2

Mention any other details that might be useful

Fixed by changing the following:

TRAINING_ENDPOINT = "TRAINING_RESOURCE_ENDPOINT"
training_key = "TRAINING_RESOURCE_API_KEY"
PREDICTION_ENDPOINT = "PREDICTION_RESOURCE_ENDPOINT"
prediction_key = "PREDICTION_API_KEY"
prediction_resource_id = "PREDICTION_RESOURCE_ID"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(TRAINING_ENDPOINT, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(PREDICTION_ENDPOINT, prediction_credentials)

Be sure to update docs if this is indeed an issue:

jolguk commented 2 years ago

Just spotted the same issue and fixed it the same way.