Trying to use Microsoft Computer Vision Image Analysis with URL and keep getting 'TypeError: not all arguments converted during string formatting'

Viewed 31

I'm quite desperate for some help as I've been trying and failing for days :( I am using the PYTHON code sample provided in the MS CV documentation. Here is my code:

import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': '****************************',
    }
params = urllib.parse.urlencode({
    # Request parameters
    'visualFeatures': 'Categories',
    #'details': 'Celebrities',
    'language': 'en',
    })
try:
    body = {"url":"https://www.doghealth.com/images/can_dogs_eat_strawberries.jpg"}
    conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
    conn.request("POST", "/vision/v3.2-preview.3/analyze?%s" % params, "{body}" , headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

The error is:

TypeError                                 Traceback (most recent call last)
<ipython-input-60-f0f928148ac1> in <module>
     21     conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
---> 22     conn.request("POST", "/vision/v2.1/analyze?visualFeatures=Description,Tags&details=Celebrities&language=en" % params, "{body}" , headers)
     23     response = conn.getresponse()

**TypeError: not all arguments converted during string formatting**

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-60-f0f928148ac1> in <module>
     26     conn.close()
     27 except Exception as e:
---> 28     print("[Errno {0}] {1}".format(e.errno, e.strerror))
     29 
     30 ####################################

AttributeError: 'TypeError' object has no attribute 'errno'

Please help if you can, I don't know where I'm going wrong. Thank you

1 Answers

You are almost there!

Your very same code has worked with the following modifications:

  1. the variable body must be a string containing a key/value pair, not a dict:

    body = '{"url":"https://www.doghealth.com/images/can_dogs_eat_strawberries.jpg"}'

  2. when you use the variable body, you should refer to its content, not the string between curly braces: conn.request("POST", "/vision/v3.2-preview.3/analyze?%s" % params, body, headers)

The full code is:

import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': 'your_subscription_key',
    }
params = urllib.parse.urlencode({
    # Request parameters
    'visualFeatures': 'Categories',
    #'details': 'Celebrities',
    'language': 'en',
    })
try:
    body = '{"url":"https://www.doghealth.com/images/can_dogs_eat_strawberries.jpg"}'
    conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
    conn.request("POST", "/vision/v3.2-preview.3/analyze?%s" % params, body, headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

This code has generated the following output, what seems to be correct:

b'{"categories":[{"name":"animal_dog","score":0.98046875}],"requestId":"881b18ee-2623-4d82-9b9d-215f8a0666d9","metadata":{"height":940,"width":940,"format":"Jpeg"}}'

Related