gpt4 book ai didi

python - 使用 http.client.https.connnection 向 microsoft azure 服务发出连接请求以分析图像

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:49 26 4
gpt4 key购买 nike

我正在尝试使用azure服务-计算机视觉来使用python3.7分析图像。尝试发出连接请求时,显示错误。看起来我为 httpsConnection 提供的参数(url)是错误的,但我不知道如何纠正它。

问题出在 API 端点上。我给出的端点是正确的,但参数要求整数值(这就是我的理解)。

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

错误:

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

最佳答案

您应该使用不带 https:// 部分的主机名。

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

但是 http 模块非常笨重,有时会给您带来无用的错误消息。相反,我建议使用适用于 Python 的 Microsoft Azure 认知服务计算机视觉客户端库的官方 Python 客户端。

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

如果您不想使用该库,或者想要连接到不同的 http api,请使用 requests .

关于python - 使用 http.client.https.connnection 向 microsoft azure 服务发出连接请求以分析图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com