Originally published in Towards AI.
Due to the importance of textual information, an OCR pipeline is critical for many machine-learning systems. The text extracted using an OCR engine can be used for various purposes, like document classification or in an RAG system. The output quality from the OCR is critical for the performance of the machine learning models on downstream tasks, which is why this article will discuss how to create a high-quality OCR pipeline.
Learn how you can develop an OCR pipeline. Image by ChatGPT. “can you make a similar image but avoid having text in the image” prompt. ChatGPT, 4, OpenAI, 14 Mar. 2024.
https://chat.openai.com.
Motivation
My motivation for this article is that I am developing an app that can take images of receipts, extract useful information from them, and deliver value to the consumer. Implementing OCR for this purpose poses a variety of problems, however, which I have spent countless hours researching and solving. I am therefore writing this article to explain how I approached the problem and to provide essential notes to keep in mind when developing the OCR pipeline.

This article will follow a series of steps in developing the OCR pipeline. First, we will look into the data you are working on. The data type will significantly impact how you approach the problem. You can, for example, imagine this by thinking of how different the problem of reading a camera picture of a receipt is versus reading a scanned document. The demands for the OCR engine will be different; thus, you must consider this when developing your OCR pipeline. Following the first step, we will look into different OCR models you can choose from, of which there are plenty. I will discuss various free models for commercial use, meaning you can utilize them in commercial settings if you wish (with some restrictions for a few of the models). After implementing the OCR models, you have to test their performance, and I will discuss how to interpret these tests and what you need to keep in mind.
Step 1: Data
Inspecting your data
The first step in developing most machine-learning systems is inspecting your dataset. This will give you an understanding of the data you are working with, which allows you to make better decisions when developing your machine-learning system. I therefore recommend inspecting the images to which you want to apply OCR. Some questions you can think about when examining the data for OCR are:
What is the aspect ratio of the data, and is it always the same
What type of data is it? Is the data scanned, or are they pictures?
Are the images always clear, or do some appear to be blurry
Is the font size always the same, or does it vary
And so on. Simply looking through several samples from your dataset will naturally answer these questions and give you a better understanding of the data.

Standardizing data format
After inspecting your data, you should pre-process it and standardize the format of the data. This will make step 2 of applying different OCR models to the data more accessible. I recommend developing a standard format you will always use. An example is that all the images you want to apply OCR to are converted to (1024, 1024), with RGB. They are then stored in an images folder, and all image paths are stored in a dataframe. You then always keep this format for your data, making the following steps in the OCR pipeline easier.
I strongly recommend having a standardized format for your data and how you read it. The reason is that this makes the following steps in the pipeline less complicated. If the input format of the data is always the same, you do not have to account for all the problems that can occur later. An example is that if all images are not guaranteed to be the same shape or file type, this must be converted and checked later in the pipeline. This opens up errors that can be difficult later in the project.

Pre-processing
Furthermore, there is a variety of preprocessing techniques that you can apply to your data. I recommend reading this Towards Data Science article on pre-processing in OCR, which gives an excellent introduction to different methods you can apply to your data before using the OCR engine. Applying pre-processing techniques like deblurring, skew correction, thresholding, and so on can significantly boost your OCR engine’s performance, which I recommend looking into.
Furthermore, pre-preprocessing is often cheap, which makes it simple to test out many different combinations of pre-processing techniques to see what performs the best on your data. Which pre-processing techniques will give you the optimal OCR performance for your data will naturally depend on your data and the different attributes you inspected earlier in step 1.
A receipt before (left image) and after (right image) applying thresholding. In this case, the thresholding was converting the image to greyscale, which gives all pixels a value in [0, 255]. Pixels with a value lower than 128 were then made black, and a value equal to or larger than 128 was made white. You can see that the thresholding can make the text in the image clearer, which can improve OCR performance. Image by the author.
Step 2: Implementing OCR models
Choosing a model
After looking into your data and standardizing its format, it is time to implement some OCR models. As of this article’s writing, many high-performance OCR engines often work right out of the box. You only need to download and apply the model to your data to see decent results. I have tested out several different OCR engines on various tasks, and I recommend testing out the following engines:
As of this article’s writing, these models have a license permitting commercial use, though some restrictions may apply. For example, SuryaOCR is free for commercial use as long as your company has an income of less than 5 million dollars.
Installing and applying these models locally is quite simple to do. The one exception that is slightly more difficult is Tesseract since you need to download the engine separately and then link to the engine from the programming language of your choice, though there are plenty of articles about how to do this, which you can follow. The other models I mentioned require one line of code to download the models, and you can apply the models to your data.
To run PaddleOCR, for example, you can use the following code to download and use the model:
#first install packages
!pip install pillow
!pip install paddleocr
!pip install paddlepaddle
#then run the ocr on the image (code from https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/doc/doc_en/quickstart_en.md)
from PIL import Image as PILImage
from paddleocr import PaddleOCR,draw_ocr
image_path = “image1.png” #TODO make sure to change this line to your own image path
ocr = PaddleOCR(use_angle_cls=True, lang=’en’) # need to run only once to download and load model into memory
result = ocr.ocr(image_path, cls=True)
for idx in range(len(result)):
res = result[idx]
for line in res:
print(line)
# you can also draw the bounding boxes and text the OCR found in the image
# if you want to do this, download the font from https://huggingface.co/spaces/PaddlePaddle/PaddleOCR/blob/main/simfang.ttf
image = PILImage.open(image_path).convert(’RGB’)
boxes = [line[0] for line in result[0]]
txts = [line[1][0] for line in result[0]]
scores = [line[1][1] for line in result[0]]
im_show = draw_ocr(image, boxes, txts, scores, font_path=’simfang.ttf’)
im_show = PILImage.fromarray(im_show)
im_show.save(’result.jpg’)Which can give an output like this:

PaddleOCR was applied to the image shown earlier. You can see that It clearly picks up each line of text and correctly predicts all the characters. Image by the author.
Or, if you apply PaddleOCR to a more difficult-to-read receipt:

Example of Paddle OCR inference output. You
I recommend trying out all models on your data. This is because installing each engine takes a short time, and you can discover that one engine works very well on your dataset compared to the other models. Testing all the models can be a valuable use of your time. Furthermore, it is essential to consider what the models are made for. An example is that SuryaOCR is only made for documents, while TrOCR also has models for handwritten texts. If your dataset, therefore, consists of a large amount of handwritten text, you should naturally avoid using SuryaOCR. Information on each model is typically found on the GitHub pages of each model, where they describe what the model is trained on and what it can be used for.
Fine-tuning a model
If the off-the-shelf performance of the models needs to be better for your use case, fine-tuning is the way to go. You should be wary, however, that fine-tuning an OCR model can be a time-intensive process, considering you need a labeled dataset and also have to run the fine-tuning process, which can be computationally intensive. I have written an article on fine-tuning the bounding box detection model in EasyOCR below, which can show you how to run a fine-tuning process on an OCR engine.
How to fine-tune the CRAFT model in EasyOCR
Fine-tuning the text detection model (CRAFT) in EasyOCR is essential to achieve high performance for the OCR engine.
When fine-tuning an OCR engine, there are typically two layers you can fine-tune. The first layer is the text detection model, which is the model that finds the position of the text in an image. If you see that your OCR engine has difficulty finding all the text in an image, this is the layer you should fine-tune. The second layer is the text recognition model, which, given a bounding box containing text, retrieves the characters within that bounding box. If you notice that your OCR is finding the text but have difficulty reading the characters, this is the layer you should fine-tune. In many cases, you must fine-tune both the first text detection layer and the second text recognition layer to achieve high OCR performance.
Often, the GitHub repositories for the OCR engines will also include pages on how to run fine-tuning for the models. If this information is not present in the article, I have found that looking into the GitHub issues of the GitHub repository contains a lot of useful links and answers for how to approach fine-tuning.
However, it would be best to have a dataset before you can perform fine-tuning. There are a few ways to obtain a set to fine-tune your OCR engine.
Label your dataset
Download a labeled dataset online
Labeling your dataset is a labor-intensive process, as you have to mark the bounding boxes of the text and write out the text within the bounding box. To achieve decent performance with fine-tuning, you likely need 100’s, if not 1000’s of documents, which makes labeling a time-consuming process. Another option is downloading a labeled dataset online, which can be a quick process but has some downsides. First, you must convert the labeled dataset format to the format your fine-tuning script expects. Secondly, finding an excellent online dataset that matches your needs can be complex. The third option is to generate a synthetic dataset, which I think is an interesting topic I have also written an article about. Generating a synthetic dataset allows you to create a large amount of labeled data quickly, and developing a high-quality synthetic dataset can be challenging.
Examples of synthetic receipts I have made to fine-tune an OCR engine. You can learn more about how to make synthetic data like this in my synthetic data generation article.
It is important to know that fine-tuning an OCR engine for your specific use case can be time-consuming, though it can also significantly boost your OCR performance, making it an attractive option for improving your OCR. To potentially save time, I would also like to add a note that you can sometimes find fine-tuned OCR models for specific use cases on HuggingFace.
Step 3: Testing
Tests to consider
After implementing several different OCR engines and potentially fine-tuning them, you must test your models. At this step, you will typically generate OCR output from various models you are considering and compare the outputs. However, it is also important to remember that there are other factors that OCR quality should consider. Another critical metric is the inference time of the OCR engine. Some OCR engines will be a lot slower than others. This can be because time is traded for higher accuracy, though it can be a reason in other cases. For example, Tesseract only runs on a CPU, while other OCR engines allow for optimized processing using a GPU, which will be faster.
You can also consider other factors for each model, such as its popularity (a more popular model will have more answers to potential problems online, making it more attractive) or which languages it supports.
Testing for accuracy
Testing an OCR engine for accuracy can be challenging, as it requires both a ground truth text and a way to compare the ground truth text with the OCR-predicted text. For example, getting a hold of ground truth text can be done by labeling the text yourself or downloading a labeled dataset. One of the best options for getting a hold of ground truth text is if you have a digitally born PDF document. If you have this available, the text in the document will be stored in the metadata of the PDF document, which you can extract with the code below.
You can use various text comparison methods after getting a hold of the ground truth text. Popular methods for comparing the similarity or distance between two strings are:
Levenshtein distance
Jaro similarity
Damerau-Levenshtein Distance
which you can, for example, find in the Jellyfish Python package. The methods will give you a metric for the similarity or distance between two strings, allowing you to compare different OCR methods. You can then see which OCR method performs best on the various metrics and consider this result when deciding which OCR engine to use for your machine-learning system. I also wrote an article earlier on Understanding OCR Quality, which you can read to learn more about testing an OCR system for the quality of its output.
You can easily use the Jellyfish package by first installing it with pip:
pip install jellyfishAnd then using a function like Levenshtein distance:
import jellyfish
jellyfish.levenshtein_distance(’this is a string’, ‘slightly different string’) Which outputs a Levenshtein distance of 15.
Conclusion
In this article, I have discussed how you can develop your OCR pipeline in three main steps:
Data
Implementing OCR models
Testing
Each of these steps is critical for the quality of your OCR system, and I have therefore discussed important considerations to keep in mind at each of these steps and how you can implement each.
Related reading
Get practical AI-engineering workflows in your inbox.
If you also use coding agents to build your AI projects, explore my configuration library. The paid Vault includes global and project templates, commands, hook configuration, prompt patterns and the parallel-agent setup. Access and updates are included while subscribed.
Try the free Claude Code Setup Pack · My website · LinkedIn · X




