0
votes

I have a problem with Computer Vision resource on Azure. This code is based on documentation example and it already worked.(https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/python-disk)

Suddenly i started getting 400 error:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://nameofmyresource.cognitiveservices.azure.com/vision/v2.0/analyze?visualFeatures=Objects%2CTags

My piece of code:

for img_path in img_path_list:
    image_data = open(img_path, "rb").read()
    print(image_data)
    headers = {'Ocp-Apim-Subscription-Key': subscription_key,
               'Content-Type': 'application/octet-stream'}
    params = {'visualFeatures': 'Objects,Tags'}
    response = requests.post(
        analyze_url, headers=headers, params=params, data=image_data)
    response.raise_for_status()
    analysis = response.json()

I've printed image_data (seems okay) and created new resource - nothing. Any thoughts?

2

2 Answers

0
votes

Seems the url you are generating is wrong, Can you try the following code,

apikey = "e720e03190c41148ec555889daf2f64"
assert apikey
api_url = "https://southeastasia.api.cognitive.microsoft.com/vision/v2.0/"
analyse_api = api_url + "analyze"
image_data = img
headers = {"Ocp-Apim-Subscription-Key": apikey,
'Content-Type': 'application/octet-stream'}
params = {'visualFeatures':'Categories,Description,Color,Objects,Faces'}
response = requests.post(
    analyse_api, headers=headers, params=params, data=image_data)
response.raise_for_status()
analysis = response.json()
#image_caption = analysis["description"]["captions"][0]["text"].capitalize()
people = 0
for i in analysis['objects']:
    if i['object'] == 'person':
        people += 1
describepeople = []
for i in analysis['faces']:
    describepeople.append(i['gender'] + ' ' + str(i['age']))
tags = analysis['description']['tags']
return[people, describepeople, tags]
0
votes

Something was bad with particular photo - next photo was okay