Image to Text | OCR Extraction

Images are everywhere, evreryone loves to takes a snap to different views like scenery, selfie, panorma or even if you don’t feel like writing then you prefer taking a snap of the documents that you have with you. And it really helps us alot and saves lot of our time. But do you know if you want to edit or copy the text that you have with you in the form of an image then we have to do the typing type the complete text from an image into the text format which will consume lot of our time which we saved before. Now, you will really feel like boring if you don’t like typing.

So, What’s the solution for it, because I will not prefer wasting lot of my time typing the text from an image.

So, once again I’m back with an advanced technique to convert the image to the text which you can also call as the OCR Extraction. So, what is OCR extraction and how does it work. Let’s understand it.

What is OCR Extraction?

OCR or Optical Character Recognition is an electronics or mechanical conversion of the image (with typed, printed, or handwritten text) into the machine-encoded text, whether from the scanned documents, snap of a document, scene photo or from subtitle text superimposed on an image.

How does it work?

In the initial stage, the document that you have is in the form of an image consisting of dots and lines – or unstructured data that cannot be read with the OCR extraction.

1. With OCR Extraction the unstructured data is converted to the structured data.

2. The data that you have now is the in the form of the n-dimensional array which can be used further for character detection as well as the character recognition.

3. Then the recognised text converted to the text format and stored in different text format for reuse.

Implementation

Now, let’s understand the practical implementation of the conversion that we discussed above. So, to implement this we have to follow the given steps,

– Read the image document on which we want to apply OCR extraction. For reading the image we are going to use OpenCV.

– Image to text conversion using tesseract

Before starting, let’s install OpenCV and tesseract. Use the below command to install OpenCV and pytesseract,

pip install opencv-python pytesseract

and

sudo apt update && sudo apt install tesseract-ocr

You can also refer to this session for proper understanding,

Here is the image that I’ll use in this blog for image to text conversation,

Fig: Image document to read

After the successful installation, write the below code to read the text from the above image document,

Full code:

import cv2
import pytesseract

## Read the image using cv2.imread
img=cv2.imread("Innovate_yourself.png")

## convert the read image document to text with the given configuration
text=pytesseract.image_to_string(img,config=r"--oem 3 --psm 6")

## store the read text to file named image2text.txt
with open(“image2text.txt”,”w”) as file:
file.write(text)
print(text)

In this way, you will be able to extract the text from the image document and this process will really help you a lot if you have image documents in bulk because it will save a lot of your efforts and time.

This is all about this blog, I hope you have enjoyed this blog. But still, if you are having any queries regarding the topic or you want to share your views then feel free to leave a comment below in the comment section.
Stay tuned and Happy Learning. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *