Search code examples
azureazure-identitymicrosoft-custom-vision

Azure Custom Vision API PermissionDenied Error when Using detect_image in Python


I am encountering a PermissionDenied error when trying to use the detect_image function of the Azure Custom Vision API in Python, following the documentation: https://learn.microsoft.com/en-us/azure/ai-services/custom-vision-service/quickstarts/object-detection?tabs=windows%2Cvisual-studio&pivots=programming-language-python

The code I have:


with open(os.path.join(base_image_location, "test", "test_image.jpg"), mode="rb") as test_data:
    results = predictor.detect_image(project.id, publish_iteration_name, test_data)

for prediction in results.predictions:
    print("\t" + prediction.tag_name + ": {0:.2f}% bbox.left = {1:.2f}, bbox.top = {2:.2f}, bbox.width = {3:.2f}, bbox.height = {4:.2f}".format(prediction.probability * 100, prediction.bounding_box.left, prediction.bounding_box.top, prediction.bounding_box.width, prediction.bounding_box.height))

And the error I receive is:

CustomVisionErrorException: Operation returned an invalid status code 'PermissionDenied'
I feel I have used my correct apis/endpoints, wanted to confirm I am getting the right ones though.

I am passing in a key and endpoint from my training model here:


credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})


trainer = CustomVisionTrainingClient(endpoint=endpoint1, credentials=credentials)
Here I am passing in the published models iteration name (from "Published as" in performance from my model).

And the resource id is from my predictive model (from the overarching custom vision settings page)

trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id)

Any advice on how I can resolve this? Can also provide more of the code if need be.


Solution

  • Initially, I too got similar error when I followed this MS Documentation and ran code by passing same ENDPOINT for both trainer and predictor like this:

    ENDPOINT = "https://democustvis.cognitiveservices.azure.com/" #Trainer Endpoint
    training_key = "training_key"
    prediction_key = "prediction_key"
    prediction_resource_id = "/subscriptions/xxxxxxxxxxx/resourceGroups/Sri/providers/Microsoft.CognitiveServices/accounts/democustvis-Prediction"
    
    credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
    trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
    prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
    predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
    

    Response:

    enter image description here

    You need to use VISION_PREDICTION_ENDPOINT as value of predictor endpoint to resolve the error.

    In my case, I modified this part of code and got the response successfully as below:

    TRAINER_ENDPOINT = "https://democustvis.cognitiveservices.azure.com/" #Trainer Endpoint
    training_key = "training_key"
    prediction_key = "prediction_key"
    prediction_resource_id = "/subscriptions/xxxxxxxxxxx/resourceGroups/Sri/providers/Microsoft.CognitiveServices/accounts/democustvis-Prediction"
    PREDICTOR_ENDPOINT = "https://democustvis-prediction.cognitiveservices.azure.com/" #Predictor Endpoint
    
    credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
    trainer = CustomVisionTrainingClient(TRAINER_ENDPOINT, credentials) #modified
    prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
    predictor = CustomVisionPredictionClient(PREDICTOR_ENDPOINT, prediction_credentials) #modified
    

    Response:

    enter image description here

    Reference:

    Azure Custom Vision Error: Invalid status code 'Unauthorized' - Microsoft Q&A by Khalid Obaid