Skip to content Skip to sidebar Skip to footer

Python Custom Vision Predictor Fails

I'm using Python to retrieve a Blob image from Azure storage and then send it to Custom Vision for a prediction. This is the code: import io from azure.storage.blob import BlockBlo

Solution 1:

Here in below i am providing similar example using custom vision prediction using image URL, you can change it to image file :

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 11:04:54 2019

@author: moverm
"""
#from azure.storage.blobimportBlockBlobServicefrom azure.cognitiveservices.vision.customvision.predictionimportCustomVisionPredictionClient


#block_blob_service = BlockBlobService(
#    account_name=account_name,
#    account_key=account_key
#)
#
#fp = io.BytesIO()
#block_blob_service.get_blob_to_stream(
#    container_name, 
#    blob_name, 
#    fp, 
#    max_connections=2
#)

predictor = CustomVisionPredictionClient(
    "prediction-key", 
    endpoint="https://southcentralus.api.cognitive.microsoft.com"
)
# This call breaks with the below error message
#results = predictor.predict_image(
#    'prediction-key',
#    image_data.getvalue(),
#    iteration_id=cv_iteration_id
#)
test_img_url = "https://pointsprizes-blog.s3-accelerate.amazonaws.com/316.jpg"
results = predictor.predict_image_url("project-Id", "Iteration-Id", url=test_img_url)

# Display the results.
for prediction in results.predictions:
    print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))

Basically issue is related to endpoint.Use https://southcentralus.api.cognitive.microsoft.com for an endpoint.

It should work, and you should be able to see the prediction probability.

Hope it helps.


Solution 2:

I tried to reproduce your issue and got a similar issue, which was caused by using the incorrect endpoint from Azure portal when I created a Cognitive Service on the region of Janpa East, as the figure below.

enter image description here

As the figure above shown, the endpoint is https://japaneast.api.cognitive.microsoft.com/customvision/training/v1.0 for version 1, but the azure-cognitiveservices-vision-customvision PyPI page points out the corrent endpoint which should be https://{AzureRegion}.api.cognitive.microsoft.com as the figure below.

enter image description here

So I got the similar issue with yours if using the incorrent endpoint, as below. My code used is the same as yours, the only difference is the running environment which yours is on Azure Functions, but mine is a console script.

enter image description here

Meanwhile, according to the source code custom_vision_prediction_client.py of Azure Cognitive Service SDK for Custom Vision, you can see the code base_url = '{Endpoint}/customvision/v2.0/Prediction' to concat your passed endpoint with /customvision/v2.0/Prediction to generate the real endpoint for calling prediction api.

Therefore, as @MohitVerma-MSFT said, using https://<your cognitive service region>.api.cognitive.microsoft.com for the current version of Python package.

Additional notes as below, there is an announce of important update for customvision.ai you need to know, it may make effect for your current code working soon after.

enter image description here

Post a Comment for "Python Custom Vision Predictor Fails"