Textman; Text Extraction with FastAPI and Pytesseract

In Africa and some parts of the world, some businesses still have a large volume of their data in physical files and not digitized. This significantly hinders progress in these companies because valuable insight that could help them make progressive decisions are locked away in these files.

Just imagine what insights will be covered if these data are digitized and analyzed. Fortunately, that is what we will be doing in this article. We will create a web app that will take in a picture containing text and then extract the text from the image using a cool library called Pytessaract.

Setup

Let’s get started by creating a virtual environment and installing the packages we will need for our app.

NB: for windows user, refer to this article to install tesseract https://medium.com/quantrium-tech/installing-and-using-tesseract-4-on-windows-10-4f7930313f82

Now let’s create a directory called textman and add the following files and folders

Build the app

Lets now add content to the app.py file

First, we imported our libraries, instantiated an app, and specified our template directory so FastAPI can read our index.hml. We then created a home route that displays our index.html file. We also created a function(read_img()) that takes in an image and extract text from it in just one line of code. Cool right? I know! We finally created a post routes that get our image file from the form we will create in the index.html file and then use OpenCV to convert the image into the right format for Pytesseract.

Our markup will now look like the following;

We are using Tailwindcss; a cool utility-first CSS library and we are also using Heropatterns for the cool background.

Our logic in the template is simple. We first check to see if there is a label and then we display the label using Jinja2 template engine.

The only thing we are left with now is to run our app. To do so, add the following to app.py

Finally, run the app with;

If all goes well, you should see the following;

In just a few lines of code, we’ve created an awesome text extractor. How cool is that? The code for this app is available here.

Conclusion

Machine Learning has numerous applications for industry. Text Extraction is just one these benefits that can transform a business and simplify lives. I hope this article will motivate you to try out these cool technologies and perhaps change a live. Thank for Reading!

AI Engineer, Data Scientist, Full-stack Developer. Co-founder (Trestle Academy Ghana(TAG))

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store