401 error while using the Computer Vision API

hamza13444 0 Reputation points
2023-02-11T23:51:53.2266667+00:00

Hello

I want to label all my images according to their file names. Therefore I used a python code but it gave an error like this:

Error labeling image: YC 3817 - 3.246 kgIm.jpg. Error message: {"error":{"code":"401","message": "The Analyze Image Operation under Computer Vision API (v3.0) is not supported with the current subscription key and pricing tier Custom_Vision.Training.S0."}}

Here was my code:

import requests
import os

# Define the endpoint, key, and project ID
endpoint = "https://[HIDDEN].cognitiveservices.azure.com/"
key = "[HIDDEN]"
project_id = "[HIDDEN]"

# Define the resource ID
resource_id = "/subscriptions/[HIDDEN]/resourceGroups/Ya/providers/Microsoft.CognitiveServices/accounts/[HIDDEN]"

# Define the path to the image directory
image_dir = "C:\Users\[HIDDEN]"

# Create a list to store the results
results = []

# Loop through all the images in the directory
for filename in os.listdir(image_dir):
    if filename.endswith(".jpg"):
        with open(os.path.join(image_dir, filename), "rb") as image_file:
            image_data = image_file.read()
        
        headers = {
            "Ocp-Apim-Subscription-Key": key,
            "Content-Type": "application/octet-stream",
            "Prediction-Key": key,
            "Project-Id": project_id,
            "Resource-Id": resource_id
        }
        
        response = requests.post(endpoint + "vision/v3.0/analyze?visualFeatures=Categories,Description,Tags", headers=headers, data=image_data)
        
        # Check if the request was successful
        if response.status_code == 200:
            results.append(response.json())
        else:
            print("Error labeling image: " + filename + ". Error message: " + response.text)

# Print the results
print(results)

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
305 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,611 Reputation points
    2023-02-27T06:51:50.51+00:00

    @hamza13444 Thanks for the question. You can use the Computer Vision API v4.0, which supports the Analyze Image operation with the Custom_Vision.Training.S0 pricing tier. To do this, you can update the endpoint and version in your code as follows:

    endpoint = "https://[HIDDEN].[cognitiveservices.azure.com/computervision/v4.0]"

    Here is the Portal for Computer Vision Scenarios.

    https://portal.vision.cognitive.azure.com/gallery/featured

    1 person found this answer helpful.
    0 comments No comments