OCR Engine: What Happens Between the Image and the Text
← All posts
GuideNov 22, 2026· 9 min read

OCR Engine: What Happens Between the Image and the Text

An OCR engine is the software component that takes an image of text and returns machine-readable text, usually with the position of every word and a confidence score. It is not the scanner, the PDF tool, or the app you click in. Those all call an engine underneath. Inside, nearly every modern engine runs the same four steps: clean up the image, find where the text is, recognize each line, and put the pieces back in reading order.

Knowing those steps is useful for a practical reason. When OCR output is wrong, the mistake almost always comes from one of them, and the fix depends on which. A missing paragraph is a detection problem. A word with two swapped letters is a recognition problem. Correct words in a scrambled order are a layout problem. Retraining or switching engines rarely helps if the real issue is a 150 dpi phone photo.

This guide walks through each stage, shows how three well-documented engine families implement them, and ends with a short Python script that prints what an engine actually saw on your page. If you already understand the internals and want to pick a model, read the OCR model guide instead.

The four stages inside an OCR engine

1. Preprocessing

Before recognition, the engine normalizes the image: converts it to grayscale or black and white, removes noise, corrects rotation and skew, and scales it so characters are a predictable size. Tesseract does much of this internally with the Leptonica library, but its documentation is frank that the built-in steps are not always enough. It recommends images of at least 300 dpi, dark text on a light background, horizontal text lines, and a small white border around tightly cropped text (Tesseract: Improving the quality of the output).

The same page also notes that retraining Tesseract is unlikely to help unless you use a very unusual font or a new language. That matches what most teams discover: bad input beats a better model every time.

2. Text detection

Detection answers “where is the text?” The output is a set of boxes or polygons, one per word or line. Older engines found text by analyzing connected blobs of dark pixels. Neural detectors treat it as a segmentation problem: the network predicts, for every pixel, how likely it is to belong to text, then turns that probability map into regions.

DBNet is a widely used example. Its authors moved the binarization step, the threshold that turns probabilities into text regions, inside the network so it can be learned rather than hand-tuned, and report 62 frames per second with a ResNet-18 backbone on the MSRA-TD500 benchmark (Liao et al., arXiv 1911.08947). Detection-based engines handle curved, rotated, and scattered text far better than blob analysis, which is why they dominate for photos, labels, and screenshots.

If detection misses a region, no later stage can recover it. Faint stamps, light gray table text, and text on colored backgrounds are the usual casualties.

3. Line recognition

Recognition takes each detected line crop and returns a string. This is where the machine learning most people associate with OCR lives, and where the field changed most in the last decade.

Classic engines segmented each character and classified it on its own. Modern engines read the whole line as a sequence. The CRNN architecture combines a convolutional network that extracts features, a recurrent network that models the sequence, and a transcription layer in a single trainable model. Its authors highlight that it handles text of any length without segmenting characters and is not tied to a fixed dictionary (Shi et al., arXiv 1507.05717). Tesseract made the same shift: since version 4 its default recognizer is an LSTM network, and in Tesseract 5 you choose it with --oem 1 or the legacy character classifier with --oem 0 (Tesseract command-line usage).

The newest recognizers drop the recurrent part for transformers. TrOCR pairs a pre-trained image transformer with a text transformer and generates wordpieces instead of characters. Its authors note that earlier systems often needed a separate language model as a post-processing step, which TrOCR folds into the model itself (Li et al., arXiv 2109.10282).

That built-in language knowledge cuts both ways. It fixes smudged letters in ordinary words, but it can also “correct” an invoice number or part code into something that looks more plausible and is wrong.

4. Layout and reading order

The last stage groups lines into blocks, paragraphs, columns, and tables, then decides the order a person would read them in. Tesseract exposes this as page segmentation modes: --psm 3 is fully automatic page layout, while modes such as --psm 6 (a single uniform block) or --psm 7 (a single line) tell the engine to skip layout analysis when you already know what the crop contains.

Layout is the weakest stage in most traditional engines. Two-column articles get read straight across, table cells lose their rows, and sidebars end up in the middle of body text. Toolkits built for documents add dedicated layout and table models on top of detection and recognition, and end-to-end document models handle layout implicitly by writing Markdown or HTML for the whole page.

Three engine designs, one pipeline

The four stages are a useful map even when an engine hides them.

Engine design Example How the stages are built Typical strength
Classic pipeline Tesseract 5 Leptonica preprocessing, layout analysis, LSTM line recognizer Runs offline on a CPU, clean printed pages, many languages
Neural pipeline PaddleOCR (PP-OCR) Separate detector, text direction classifier, and recognizer models Photos, rotated and mixed-language text, small models
End-to-end model TrOCR for lines, document VLMs for pages One network replaces some or all of the stages Handwriting, complex layouts, Markdown output

PP-OCR shows how small a neural pipeline can be. The original paper describes a 3.5 MB system for 6,622 Chinese characters and a 2.8 MB version for 63 alphanumeric symbols, with released models for the detector, the direction classifier, and the recognizer (Du et al., arXiv 2009.09941). Splitting the job into separate models is also what makes these pipelines easy to debug: you can look at the boxes the detector produced before you blame the recognizer.

End-to-end models trade that transparency for context. Because they see the whole page at once, they cope better with tables and multi-column layouts, but when they fail, the failure can be a fluent paragraph that never appeared on the page. The OCR model guide compares the current document models on public benchmarks.

What an OCR engine returns

Plain text is only the simplest output. Most engines can also give you structure that you will want in production:

  • Word boxes and confidences. Tesseract’s TSV output lists every word with its page, block, paragraph, line, pixel coordinates, and a confidence value.
  • hOCR or ALTO. XML-style formats that keep the full hierarchy of page, block, line, and word with bounding boxes. Archives and digitization projects use them to highlight search hits on the original image.
  • Searchable PDF. The original image with an invisible text layer on top, which is what document management systems such as Paperless-ngx store.
  • Markdown or JSON. Document-focused engines and APIs return headings, lists, and tables as Markdown, or named fields as typed JSON. See how OCR can output structured JSON or XML for the trade-offs.

Confidence scores are the most underused part of that list. They are not probabilities you can compare across engines, but within one engine they are a good way to route the weakest words to a person instead of checking every page.

Look inside an engine on your own page

The script below runs Tesseract on an image and prints what each stage produced: how many blocks and lines layout found, how many words recognition returned, and the words with the lowest confidence. It needs the tesseract binary plus pip install pytesseract pillow.

import sys
from collections import Counter

import pytesseract
from PIL import Image

path = sys.argv[1]
psm = sys.argv[2] if len(sys.argv) > 2 else "3"

img = Image.open(path)
print(f"image: {img.width}x{img.height} px, dpi={img.info.get('dpi', 'unknown')}")

data = pytesseract.image_to_data(
    img, config=f"--oem 1 --psm {psm}", output_type=pytesseract.Output.DICT
)

words = []
lines = set()
blocks = set()
for i, text in enumerate(data["text"]):
    conf = float(data["conf"][i])
    if conf < 0 or not text.strip():
        continue  # structural rows (page, block, line) have conf -1
    words.append((conf, text, data["left"][i], data["top"][i], data["height"][i]))
    lines.add((data["block_num"][i], data["par_num"][i], data["line_num"][i]))
    blocks.add(data["block_num"][i])

print(f"layout: {len(blocks)} blocks, {len(lines)} lines")
print(f"recognition: {len(words)} words")
if not words:
    sys.exit("no text found: check detection (contrast, rotation, psm)")

heights = Counter(w[4] // 5 * 5 for w in words)
print(f"most common word height: about {heights.most_common(1)[0][0]} px")

print("lowest-confidence words:")
for conf, text, x, y, _ in sorted(words)[:10]:
    print(f"  {conf:5.1f}  {text!r} at x={x}, y={y}")

Run it as python inspect_ocr.py scan.png, then again with 6 or 11 as the second argument to compare page segmentation modes. Read the result stage by stage:

  • Few or no words on a page that clearly has text means detection or preprocessing failed. Check contrast, rotation, and resolution before anything else.
  • A small typical word height, below roughly 20 px, usually means the image is too low resolution for the recognizer. Rescan or upscale rather than tuning settings.
  • Low-confidence words clustered in one area often point to a stamp, a table, or a background color in that region.
  • Good words in the wrong order is a layout issue. Try another --psm, crop the regions you need, or move to an engine that understands document layout.

For a symptom-first checklist that covers PDFs and photographed pages as well, see why OCR output goes wrong.

Run an engine yourself or call one

Running your own engine makes sense when pages cannot leave your network, volume is high and predictable, and your documents suit one engine well. You then own the upgrades, the GPU or CPU capacity, and the work of wiring detection, recognition, and layout output into something your systems can use.

OCRskill is the other route: a hosted OCR API where the engine choice, hardware, and updates are not your problem. You send an image or PDF to POST /ocr and get Markdown back, or call POST /ocr.json with the fields you want and get typed JSON:

curl -s https://api.ocrskill.com/ocr \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/pdf" \
  --data-binary "@invoice.pdf"

Uploads are limited to 20 MB per request, and the structured JSON API reference lists the supported fields. It does not give you per-word boxes the way Tesseract’s TSV does, so if your application highlights words on the original image, a local engine is still the better fit for that part.

Limitations of this picture

  • The stages blur in newer models. End-to-end document models do not expose detection boxes or confidences in the same way, so the debugging approach above works best on pipeline engines.
  • Confidence is engine specific. A Tesseract confidence of 80 and a score of 0.8 from another engine do not mean the same thing. Calibrate thresholds on your own pages.
  • Benchmarks lag reality. Published accuracy figures come from standard datasets. Your forms, stamps, and phone photos are the only test that counts.

Conclusion

An OCR engine is four jobs chained together, and each one fails in its own recognizable way. When output looks bad, find the stage first: count the words detection found, check the character height recognition received, and compare layout modes before you switch tools or retrain anything. Pipeline engines such as Tesseract and PaddleOCR make that inspection easy because every stage leaves evidence. End-to-end models read complex pages better but hide their reasoning, so pair them with validation of the values that matter. Whichever engine you choose, keep its raw output and confidences next to the final text. They are what let you fix the next bad page in minutes instead of guessing.