1

I am trying to use the azure service-Computer vision for analyzing an Image with python3.7. While trying to make a connection request, an error is shown. It looks like the parameter(url) I am giving for httpsConnection is wrong, But I dont know how to rectify it.

The problem is with the API endpoint. The endpoint I have given is correct but the parameter is asking for integer value(that's what I understood).

def analyze_image(data):
    try:
        conn = http.client.HTTPSConnection('https://xxx.cognitiveservices.azure.com/')
        conn.request("POST", "/vision/v1.0/analyze?%s" % params, str(data), headers)
        response = conn.getresponse()
        data = response.read()
        print(str(data))
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

    return data

Error:

Traceback (most recent call last):
  File "C:\Users\Rupali Singh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 887, in _get_hostport
    port = int(host[i+1:])
ValueError: invalid literal for int() with base 10: '//rupali.cognitiveservices.azure.com/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Rupali Singh/PycharmProjects/Drishti/ms_visionapi.py", line 44, in analyze_image
    conn = http.client.HTTPSConnection('https://rupali.cognitiveservices.azure.com/')
  File "C:\Users\Rupali Singh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1373, in __init__
    blocksize=blocksize)
  File "C:\Users\Rupali Singh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 851, in __init__
    (self.host, self.port) = self._get_hostport(host, port)
  File "C:\Users\Rupali Singh\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 892, in _get_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
http.client.InvalidURL: nonnumeric port: '//rupali.cognitiveservices.azure.com/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Rupali Singh/PycharmProjects/Drishti/ms_visionapi.py", line 85, in <module>
    data = analyze_image(img)
  File "C:/Users/Rupali Singh/PycharmProjects/Drishti/ms_visionapi.py", line 51, in analyze_image
    print("[Errno {0}] {1}".format(e.errno, e.strerror))
AttributeError: 'InvalidURL' object has no attribute 'errno'

Process finished with exit code 1
Rupali Singh
  • 55
  • 1
  • 6
  • What is the value of your `params` var that you are trying to add to your url? – Nicolas R Aug 12 '19 at 07:44
  • Please refer the following link for Computer vision analyze Image REST API and Python Code in code samples. https://westcentralus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa – Ram Aug 12 '19 at 10:36
  • Check this thread where you should find answer to your 'errno' issue: https://stackoverflow.com/questions/48541077/exceptions-runtimeerror-object-has-no-attribute-errno – IoTKid Mar 05 '21 at 23:27

1 Answers1

0

You should use the host name without the https:// part.

conn = http.client.HTTPSConnection('xxx.cognitiveservices.azure.com')

But the http module is quite clunky and sometimes give you unhelpful error messages. Instead, I recommend using the official python client for Microsoft Azure Cognitive Services Computer Vision Client Library for Python.

https://pypi.org/project/azure-cognitiveservices-vision-computervision/

If you don't want to use that library, or want to connect to a different http api, use requests.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67