python - 请求异常.HTTPError : 404 Client Error: Resource Not Found for url | Azure Cognitive Services Api

标签 python azure api http http-error

我正在尝试通过Azure 认知服务 API 发送图像。我收到此错误消息: requests.exceptions.HTTPError: 404 客户端错误: 找不到 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))

请帮助我。我是菜鸟...

最佳答案

您应该更改您的vision_service_address

如下所示,

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

测试结果:

enter image description here

修改后的代码:

# 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))

关于python - 请求异常.HTTPError : 404 Client Error: Resource Not Found for url | Azure Cognitive Services Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68058992/

相关文章:

javascript - javascript 请求时 url 无响应

php - Twitter POST direct_messages/新 api 不工作

python - 重载列表理解行为?

python - 删除具有混合数据类型的值

azure - 请求 MSTeams 机器人的访问 token

azure - 使用 ARM 模板部署 azure keyvault

python - R 翻译成 Python

python - 从本地的 pyspark 读取 azure datalake gen2 文件

api - 微软计算机视觉API手写

python - API中缺少JSON字段时的防御条件