Search code examples
pythonazureapihttphttp-error

requests.exceptions.HTTPError: 404 Client Error: Resource Not Found for url | Azure Cognitive Services Api


I'm trying to send an image through Azure Cognitive Service API. I got this Error Message: requests.exceptions.HTTPError: 404 Client Error: Resource Not Found for url : https://myfirstpythonapi.cognitiveservices.azure.com/analyze? ...

# Use the requests library to simplify making a REST API call from Python 
import requests

# We will need the json library to read the data passed back 
# by the web service
import json

# You need to update the SUBSCRIPTION_KEY to 
# they key for your Computer Vision Service
SUBSCRIPTION_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# You need to update the vision_service_address to the address of
# your Computer Vision Service
vision_service_address = "https://myfirstpythonapi.cognitiveservices.azure.com/"

# Add the name of the function you want to call to the address
address = vision_service_address + "analyze"

# According to the documentation for the analyze image function 
# There are three optional parameters: language, details & visualFeatures
parameters  = {'visualFeatures':'Description',
               'language':'en'}

# Open the image file to get a file object containing the image to analyze
image_path = "c:/Users/PC[enter image description here][1]T/Desktop/CallApi/Dodge.jpg"
image_data = open(image_path, "rb").read()

# According to the documentation for the analyze image function
# we need to specify the subscription key and the content type
# in the HTTP header. Content-Type is application/octet-stream when you pass in a image directly
headers    = {'Content-Type': 'application/octet-stream',
              'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY}

# According to the documentation for the analyze image function
# we use HTTP POST to call this function
response = requests.post(address, headers=headers, params=parameters, data=image_data)

# Raise an exception if the call returns an error code
response.raise_for_status()

# Display the JSON results returned
results = response.json()
print(json.dumps(results))

Please help me. I'm a noobie...


Solution

  • You should change your vision_service_address.

    Like below,

    vision_service_address = "https://<your_region>.api.cognitive.microsoft.com/vision/v3.2/"
    

    Test Tesult:

    enter image description here

    Modified Code:

    # Use the requests library to simplify making a REST API call from Python 
    
    import requests
    
    # We will need the json library to read the data passed back 
    # by the web service
    import json
    
    # You need to update the SUBSCRIPTION_KEY to 
    # they key for your Computer Vision Service
    SUBSCRIPTION_KEY = "03****e"
    
    # You need to update the vision_service_address to the address of
    # your Computer Vision Service
    vision_service_address = "https://centralus.api.cognitive.microsoft.com/vision/v3.2/"
    
    # Add the name of the function you want to call to the address
    address = vision_service_address + "analyze"
    
    # According to the documentation for the analyze image function 
    # There are three optional parameters: language, details & visualFeatures
    parameters  = {'visualFeatures':'Description',
                   'language':'en'}
    
    # Open the image file to get a file object containing the image to analyze
    image_path = "E:\\MyCase\Dev\\Python\\faceapi2\\jason.jpg"
    image_data = open(image_path, "rb").read()
    
    # According to the documentation for the analyze image function
    # we need to specify the subscription key and the content type
    # in the HTTP header. Content-Type is application/octet-stream when you pass in a image directly
    headers    = {'Content-Type': 'application/octet-stream',
                  'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY}
    
    # According to the documentation for the analyze image function
    # we use HTTP POST to call this function
    response = requests.post(address, headers=headers, params=parameters, data=image_data)
    
    # Raise an exception if the call returns an error code
    response.raise_for_status()
    
    # Display the JSON results returned
    results = response.json()
    print(json.dumps(results))