Data Science

Checkmate the OCR Challenge: Image to Text Extraction

Extract text from images in 3 simple steps

Unleash the power of Microsoft Azure Computer vision using Python

Checkmate the OCR challenge : Image to Text Extraction using Python & Azure, (Photo by GR Stocks on Unsplash)

OCR optical character recognition or to say in layman terms extracting text from images has been one of the most remarkable features available today. Gone are the days when organizations employed thousands of people just to type the text from images! Today, it is a job of a few people who can process thousands of images in just few minutes. Extracting text from images has helped organizations to massively improve customer service, increase storage spaces and secure the sensitive data in a compliant manner.


What will we Discuss?

We would learn how to extract text from images using the power of Microsoft Azure computer vision cognitive services. 

  1. We would use a sample image stored in Azure container Blob storage (https://jayantml1356189034.blob.core.windows.net/jayantcontainer/0001.jpg)
  2. Process the given image to extract 100+ words.
  3. Store the extracted result in a local MS Excel file.

Resources Required

  • Python instance (i.e. Spyder IDE)
  • Microsoft Azure Subscription (to run computer vision cognitive services and storage service to store images in Blob)

Step 1: Create Azure Computer Vision

1.1 Login to the Azure Portal: https://portal.azure.com/#home, search for “ Computer Vision

1.2 Create computer vision service by selecting subscription, creating a resource group (just a container to bind the resources), location and pricing tier. A free web container allows 5,000 transactions free per month. After clicking “Review + Create” , Azure may take a couple of minutes to create the resource.

Create Computer Vision service

1.3 Once the computer vision service has been created, navigate to “Keys and Endpoint” and copy the keys and endpoint details probably in a notepad.

Keys and Endpoint for Computer vision service
⚠️ Please keep a note that keys and endpoint should not be disclosed to unauthorized people as they may impact your azure consumption cost. Regenerate keys if you have accidently disclosed the same.

Now, you are done with the Azure Portal portion now and can navigate to Python (Spyder IDE).

Step 2: Install AZURE packages

Installation: Open Python instance (i.e. Spyder 🐍 ) and issue below commands to install the required azure packages.

pip install — upgrade azure-cognitiveservices-vision-computervision
pip install pillow
pip install azure-storage-blob

Step 3: Python code magic

Use the below script and replace (a) Subscription key (b) Endpoint and Execute! You can try executing the script with other images by replacing the field ‘remote_image_handw_text_url’. The script has been updated with self-explanatory comments. Feel free to ask any further questions in the comments section.

 

This is how your output looks like. You can download the Excel here.

                                                                                           Exported MS Excel Workbook

 

Conclusion

We learned 📘 how to extract text from image and get the output in MS Excel for further analysis.. You could use other images and customize the code to see what suits your use case best! 👍

Have questions?? Please drop it in the comments !

References

[1] https://docs.microsoft.com/en-in/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-python

Categories: Data Science

Tagged as: , , , ,

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.