How to apply Azure OCR API with Request library on local images?

Viewed 264

Actually, I'm using ocr.space as an OCR API for the OCR task in my project (It's a python project).

I would like to use Azure OCR API and check which API is better than the other.

I followed this documentation https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-python.

As you can see the computervision_client.read(...) function needs an image URL to work correctly. However, I want to apply this API on local image in my computer.

What do you suggest mates ?

Thank you

2 Answers

I had the same issue, they discussed it on github here. And somebody put up a good list of examples for using all the Azure OCR functions with local images. Try using the read_in_stream() function, something like

with open("path_to_image.png", "rb") as image_stream:
        job = client.read_in_stream(
            image=image_stream,
            mode="Printed",
            raw=True
        )
operation_id = job.headers['Operation-Location'].split('/')[-1]
image_analysis = computervision_client.get_read_result(operation_id)
while image_analysis.status.lower() in ['notstarted', 'running']:
    time.sleep(1)
    image_analysis = computervision_client.get_read_result(
      operation_id=operation_id)

print(image_analysis)
lines = [res.lines for res in image_analysis.analyze_result.read_results]
print(lines)
Related