'Ms Computer Vision API image url replace as local .jpg using Python
I am trying to write a python program on Pi to capture an image and get the description from Ms Computer Vision API. It is working with using image_url as "http://website.com/abc.jpg", but when I change to my local image "abc.jpg", it is have an error.
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
The original working code is below:
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()
so when replacing image_url as "abc.jpg" or "C:/abc.jpg", it didn't work.
Solution 1:[1]
yes, you should read image before send like this :
# 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)
Solution 2:[2]
Set image_path to the local path of an image that you want to analyze.
image_path = "Image location (local path)"
Read the image into a byte array
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key, 'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Categories,Description,Color'}
response = requests.post( ocr_url, headers=headers, params=params, data=image_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(analysis)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Baptistin |
Solution 2 | Nicolas Gervais |