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

Viewed 2437
1 Answers

The Cognitive services API will not be able to locate an image via the URL of a file on your local machine. Instead you can call the same endpoint with the binary data of your image in the body of the request.

Replace the following lines in the sample Python code

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)

with

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)

And replace the following line:

image = Image.open(BytesIO(requests.get(image_url).content))

with

image = Image.open('./YOUR_LOCAL_IMAGE_FILE.png')
Related