0
votes

I am attempting to use a local image in my text recognition script, the documentation has the following example (https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/python-hand-text):

enter image description here

But when I change the image_url to a local file path, it sends a HTTPError: 400 Client Error: Bad Request for url. I have tried following other tutorials but nothing seems to work.

Any help would be greatly appreciated :)

1

1 Answers

3
votes

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')