将本地图像用于 Read 3.0、Azure 认知服务、计算机视觉

Using local image for Read 3.0, Azure Cognitive Service, Computer Vision

我正在尝试在我的文本识别脚本中使用本地图像,文档中有以下示例 (https://docs.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')