Ms Computer Vision API 图片 url 使用 Python 替换为本地 .jpg
Ms Computer Vision API image url replace as local .jpg using Python
我正在尝试在 Pi 上编写一个 python 程序来捕获图像并从 Ms Computer Vision API 获取描述。它正在使用 image_url 作为 "http://website.com/abc.jpg",但是当我更改为我的本地图像时 "abc.jpg", 有错误.
File "ms.py", line 71, in
response.raise_for_status()
File "C:\Python27\lib\site-packages\requests-2.19.1-py2.7.egg\requests\models.py", line 939, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?visualFeatures=Categories%2CDescription%2CColor
原始工作代码如下:
import requests
import matplotlib.pyplot as plt
import simplejson as json
from PIL import Image
from io import BytesIO
subscription_key = "XXXX"
assert subscription_key
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/"
analyze_url = vision_base_url + "analyze"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/" + \
"Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key }
params = {'visualFeatures': 'Categories,Description,Color'}
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()
所以当将 image_url 替换为 "abc.jpg" 或 "C:/abc.jpg" 时,它不起作用。
是的,您应该像这样在发送前阅读图像:
# Set image_path to the local path of an image that you want to analyze.
image_path = "C:/Documents/ImageToAnalyze.jpg"
# Read the image into a byte array
image_data = open(image_path, "rb").read()
response = requests.post(
analyze_url, headers=headers, params=params, data=image_data)
将image_path设置为您要分析的图像的本地路径。
image_path = "图片位置(本地路径)"
将图像读入字节数组
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream'}
参数={'visualFeatures':'Categories,Description,Color'}
回应=requests.post(
ocr_url, headers=headers, 参数=参数, 数据=image_data)
response.raise_for_status()
'analysis' object 包含描述图像的各种字段。图像最相关的标题是从 'description' 属性.
中获得的
分析=response.json()
打印(分析)
我正在尝试在 Pi 上编写一个 python 程序来捕获图像并从 Ms Computer Vision API 获取描述。它正在使用 image_url 作为 "http://website.com/abc.jpg",但是当我更改为我的本地图像时 "abc.jpg", 有错误.
File "ms.py", line 71, in response.raise_for_status()
File "C:\Python27\lib\site-packages\requests-2.19.1-py2.7.egg\requests\models.py", line 939, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?visualFeatures=Categories%2CDescription%2CColor
原始工作代码如下:
import requests
import matplotlib.pyplot as plt
import simplejson as json
from PIL import Image
from io import BytesIO
subscription_key = "XXXX"
assert subscription_key
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/"
analyze_url = vision_base_url + "analyze"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/" + \
"Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key }
params = {'visualFeatures': 'Categories,Description,Color'}
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()
所以当将 image_url 替换为 "abc.jpg" 或 "C:/abc.jpg" 时,它不起作用。
是的,您应该像这样在发送前阅读图像:
# Set image_path to the local path of an image that you want to analyze.
image_path = "C:/Documents/ImageToAnalyze.jpg"
# Read the image into a byte array
image_data = open(image_path, "rb").read()
response = requests.post(
analyze_url, headers=headers, params=params, data=image_data)
将image_path设置为您要分析的图像的本地路径。
image_path = "图片位置(本地路径)"
将图像读入字节数组
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key, 'Content-Type': 'application/octet-stream'}
参数={'visualFeatures':'Categories,Description,Color'}
回应=requests.post( ocr_url, headers=headers, 参数=参数, 数据=image_data)
response.raise_for_status()
'analysis' object 包含描述图像的各种字段。图像最相关的标题是从 'description' 属性.
中获得的分析=response.json()
打印(分析)