我尝试在文本识别脚本中使用本地镜像,文档中有以下示例 ( https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/python-hand-text ):
但是当我将 image_url
更改为本地文件路径时,它会发送 HTTPError: 400 Client Error: Bad Request for url。我尝试过遵循其他教程,但似乎没有任何效果。
任何帮助将不胜感激:)
最佳答案
认知服务 API 将无法通过本地计算机上文件的 URL 定位图像。相反,您可以使用请求正文中图像的二进制数据来调用相同的端点。
替换示例 Python 代码中的以下行
image_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
data = {'url': image_url}
response = requests.post(
text_recognition_url, headers=headers, json=data)
与
headers = {'Ocp-Apim-Subscription-Key': subscription_key,'Content-Type': 'application/octet-stream'}
with open('YOUR_LOCAL_IMAGE_FILE', 'rb') as f:
data = f.read()
response = requests.post(
text_recognition_url, headers=headers, data=data)
并替换以下行:
image = Image.open(BytesIO(requests.get(image_url).content))
与
image = Image.open('./YOUR_LOCAL_IMAGE_FILE.png')
关于azure - 使用本地镜像进行 Read 3.0、Azure 认知服务、计算机视觉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63907566/