微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

requests.exceptions.HTTPError:404 客户端错误:找不到 url 的资源 | Azure 认知服务 API

如何解决requests.exceptions.HTTPError:404 客户端错误:找不到 url 的资源 | Azure 认知服务 API

我正在尝试通过 Azure Cognitive Service 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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。