Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ocr-pipeline-optimizer

A small, reproducible study in making an OCR pipeline faster and more accurate. It runs a plain baseline pipeline over sample document images, profiles where the time actually goes, applies targeted optimizations, and reports the before-and-after on the same inputs. The point is the method that a real "optimize our OCR program" job needs: measure first, change the part that matters, prove the gain.

Everything runs on public and synthetic sample images. No client code and no client data.

The idea

Most OCR programs are not slow or inaccurate because of the OCR engine itself. They are slow because of how images are pushed through it (one at a time, no reuse, heavy preprocessing on every page) and inaccurate because the preprocessing does not suit the input. So this repo does two honest things: it finds where the time goes with a profiler, and it fixes accuracy at the preprocessing stage, then shows the numbers.

What it contains

  • Baseline pipeline. A straightforward Tesseract + OpenCV pipeline: load, preprocess, OCR, collect text. Deliberately unoptimized, the kind of code that grows organically.
  • Profiler pass. Times each stage (load, preprocess, OCR, post-process) per page and in aggregate, so the bottleneck is a number, not a guess.
  • Optimizations.
    • Accuracy: deskew, denoise, adaptive thresholding, and DPI normalization tuned to the input, plus the right Tesseract page-segmentation and OEM modes.
    • Speed: batch and parallelize across pages, reuse the OCR engine/handle instead of re-init per page, skip redundant work, and cache intermediate results.
  • Benchmark harness. Runs baseline and optimized over the same image set and prints a table: latency, throughput, and accuracy, before vs after.

Metrics

  • Latency per page (ms), and throughput (pages per second).
  • Accuracy as character error rate (CER) and word error rate (WER) against ground-truth text for the sample set.
  • Peak memory, since an OCR batch job often falls over on memory, not CPU.

Run

pip install -r requirements.txt   # pytesseract, opencv-python, and the Tesseract binary
python -m ocr_opt.profile  samples/            # where does the time go on the baseline
python -m ocr_opt.bench    samples/            # baseline vs optimized, prints the table
python -m ocr_opt.run --optimized samples/     # run the optimized pipeline on a folder

On the baseline being fair

A comparison is worthless if the baseline is a strawman. The baseline here is plain, not sabotaged: it re-initializes the engine per page, preprocesses every image the same way, and works through pages one at a time. That is what an OCR script that grew organically actually looks like, and each of those three is a real thing worth fixing rather than an invented handicap.

The benchmark also toggles one optimization at a time, so the table can name which change produced the win instead of reporting a single lump improvement.

Results (to be filled with real captured numbers)

Produced by running the benchmark on the sample set. Real measurements, not edited. Nothing in this table is filled in yet.

Metric Baseline Optimized
Latency per page (ms) (fill) (fill)
Throughput (pages/sec) (fill) (fill)
Character error rate (fill) (fill)
Peak memory (MB) (fill) (fill)
Biggest single win (fill: e.g. engine reuse, or adaptive threshold on low-contrast scans)

(attach: the profiler output showing the bottleneck before, and the same after)

Repository layout

ocr-pipeline-optimizer/
  ocr_opt/
    baseline.py        plain load -> preprocess -> OCR -> text
    optimized.py       tuned preprocessing + batching/parallel + engine reuse
    preprocess.py      deskew, denoise, threshold, DPI normalize
    profile.py         per-stage timing
    bench.py           baseline vs optimized table (latency, throughput, CER, memory)
    run.py             CLI over a folder
  samples/             public/synthetic images + ground-truth text
  requirements.txt
  tests/

Notes

  • Public and synthetic images only; the numbers come from real runs, and any real client work stays private.
  • The interesting parts are the profiler and the preprocessing; the rest is a thin CLI around them.
  • It is a method demo, not a product: it shows how I would find and prove the wins in an existing OCR program, which is the actual job.

MIT licensed.

About

Measure first, optimize what matters, prove it. Baseline vs optimized Tesseract/OpenCV pipeline with per-stage profiling, CER/WER, and per-change attribution.

Resources

Stars

Watchers

Forks

Contributors

Languages