Error 404 for Azure AI Vision API endpoint

Nishigandha Lad 25 Reputation points
2023-12-26T07:24:01.6233333+00:00

I am using Azure AI computer vision for Search photos with image retrieval service. I have created the service in Central India and East US region with Standard pricing tier. I am using the API key and endpoint for image embedding in jupyter notebook.

Here is my code:

def image_embedding(image, endpoint, key):
    """
    Image embedding using Azure Computer Vision 4.0 (Vectorize Image API)
    """
    with open(image, 'rb') as img:
        data = img.read()

    # Image retrieval API
    version = "?api-version=2023-02-01-preview&modelVersion=latest"
    vectorize_img_url = endpoint + "retrieval:vectorizeImage" + version

    headers = {
        'Content-type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key': key
    }

    try:
        r = requests.post(vectorize_img_url, data=data, headers=headers)

        if r.status_code == 200:
            image_vector = r.json()['vector']
            return image_vector
        else:
            print(f"An error occurred while processing {image}. Error code: {r.status_code}.")
        
    except Exception as e:
        print(f"An error occurred while processing {image}: {e}")

    return None

# Load environment variables
load_dotenv()
endpoint ='https://Service_Name.cognitiveservices.azure.com/'
key = 'API_Key'

# image embedding
image = r'C:\Users\HP\Downloads\[Hidden].jpg'
image_emb = image_embedding(image, endpoint, key)
# display the image
plt.imshow(Image.open(image))
plt.axis('off')
plt.show()

The error seems to be associated with API endpoint.

Key points to consider:

  1. The code is error free and is tested earlier with correct outputs.
  2. API endpoint and key are copied correctly.
  3. Image path is accurate.
  4. All libraries are loaded.
Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
371 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,833 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 45,961 Reputation points Microsoft Employee
    2023-12-26T09:13:01.1133333+00:00

    @Nishigandha Lad Are you trying to use the sample mentioned on this page? I see the URL in the POST request is of the following format.

    https://<endpoint>/computervision/retrieval:vectorizeImage?api-version=2023-02-01-preview&modelVersion=latest

    From your code above, I think you are missing the computervision path in your URL. Could you cross check and try again?

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.