Building a Simple Text Recognizer in Python | by Behic Guven | Towards Data Science

In this post, I will show you how to extract text from an image using Python. This process is simply called “Text Recognition” or “Text Detection”. So basically as you can understand from the title, we will build a simple python program that will extract text for us. After the extraction, the program will also export the result into a text document. This way, we can keep a record of our results.
If you haven’t heard of Computer Vision before, this is the best time to learn about it. Most of the machine learning and artificial intelligence fields are strongly related to Computer Vision. As we are growing and exploring, seeing the outside world has a big impact on our development. This goes the same for machines, they see the outside world using images, and those images are basically turned into data that computers can understand. Computer Vision is a very deep ocean, I just tried to give some idea of it. If you want to learn more about Computer Vision, here is a nice video by Google Cloud Platform:
There are many nice projects that I want to show you in the following weeks to get started with computer vision. You will be amazed at how powerful things you can do using computer vision. Text Recognition is just one drop from the big ocean. If you are ready, let get started!
First, let’s install the module packages that we will be using in this program. We will install PIL, pytesseract packages. PIL stands for Python Imaging Library, it adds image processing capabilities to your program. The module supports many image formats. And PyTesseract is another module we will be using, which basically does the text recognition part. It will help us to recognize the text and read it.
Here is the code to pip install multiple modules at the same time:
pip install Pillow pytesseract
If you want to learn more about these packages:
PIL
PyTesseract
Now let’s import the libraries into our program:
# adds image processing capabilitiesfrom PIL import Image# will convert the image to text stringimport pytesseract
In this step, we will choose an image that we want to work on. You can download an image from Google or take a picture using your phone. Both ways should work fine. After choosing your image, create a new folder in the same directory, you can call it “image_test” and copy the image file into that folder.
I’ve downloaded an image that has a quote by Virginia Woolf. Fun Fact: I really enjoy reading her books :)
Anyway, after pasting the image in our image_test folder, let’s get back to coding. In the following code, we are creating an image variable to store the image file and then recognizing text using image_to_string method.
# assigning an image from the source pathimg = Image.open(‘image_test/virginia-quote.jpg’)# converts the image to result and saves it into result variableresult = pytesseract.image_to_string(img)
In this step, we will export our results from the previous code into a text document. This way we will have both the original image file and the text we recognized from that image.
with open(‘text_result.txt’, mode =’w’) as file: file.write(result) print(“ready!”)
Well done! You just created your own Text Recognizer using Python. If you want to make it better, check the next extra step. Also here is another project you may be interested.
towardsdatascience.com
PIL is a great and huge package that has many capabilities. In this extra step, we would do some little touches to the image so that our text recognizer can recognize or in other words “read” the text easier. Making little touches on images is taking a big role in Computer Vision. The computer needs our help to process the image in a better way.
Without image processing, it is like putting the recipe ingredients in front of a child and ask him/her to eat. With the right image processing, it is like putting the cooked food in front of the child. The difference is huge, I highly recommend going through image processing documentation to learn more.
Here is the whole code with some little touches added, in other words, a freshly cooked code :)
# adds more image processing capabilitiesfrom PIL import Image, ImageEnhance# assigning an image from the source pathimg = Image.open(‘image_test/virginia-quote.jpg’)# adding some sharpness and contrast to the image enhancer1 = ImageEnhance.Sharpness(img)enhancer2 = ImageEnhance.Contrast(img)img_edit = enhancer1.enhance(20.0)img_edit = enhancer2.enhance(1.5)# save the new imageimg_edit.save("edited_image.png")# converts the image to result and saves it into result variableresult = pytesseract.image_to_string(img_edit)with open(‘text_result2.txt’, mode =’w’) as file: file.write(result) print(“ready!”)
Personally, I am using the one we just created to convert my book pages into text documents, it makes note-taking process much easier and faster. Instead of writing down my favorite lines each time, now I am just taking a picture of that page and converting it with the Text Recognizer. Isn’t that the cool part about coding? Create something that will solve a problem in your life.
I am Behic Guven, and I love sharing stories on creativity, programming, motivation, and life. Follow me Medium to stay inspired.