Search code examples
pythonazureapiazure-cognitive-services

How to call Azure Cognitive Services API?


I've created and trained an image classification model using Azure Custom Vision (Cognitive Services) and published the model with API. Now, I've written a simple code in Python which takes an image from given URL and calls the API. However, I'm still getting this error even though the image surely exists:

with open(URL, "rb") as image_contents: FileNotFoundError: [Errno 2] No such file or directory: 'https://upload.wikimedia.org/wikipedia/commons/5/55/Dalailama1_20121014_4639.jpg'

The code is as below:

from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient

ENDPOINT = "https://westeurope.api.cognitive.microsoft.com/"
PROJECT_ID = "bbed3f99-4199-4a17-81f2-df83f0659be3"

# Replace with a valid key
prediction_key = "<my prediction key>"
prediction_resource_id = "/subscriptions/97c4e143-9c0c-4f1e-b880-15492e327dd1/resourceGroups/WestEurope/providers/Microsoft.CognitiveServices/accounts/HappyAI"
publish_iteration_name = "Iteration5"

# Classify image
URL = "https://upload.wikimedia.org/wikipedia/commons/5/55/Dalailama1_20121014_4639.jpg"

# Now there is a trained endpoint that can be used to make a prediction
predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT)

with open(URL, "rb") as image_contents:
    results = predictor.classify_image(
        PROJECT_ID, publish_iteration_name, image_contents.read())

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

Help would be appreciated!

Thanks in advance!


Solution

  • There are two ways to give an image to the Cognitive Service. You are mixing both ;)

    1) Provide a URL to an image that is accessible over the internet. You do this by sending a JSON to the service: {"url":"https://sample.com/myimage.png"}

    2) Upload the image as binary in the POST request.

    enter image description here

    Source: https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api#get-the-url-and-prediction-key

    Your issue is that you are trying to use open() for method 2. However, this does not work with remote files in Python. If you want to do this (instead of method 1), use for example urllib2.urlopen like this.